2011-08-23 21 views
0

我正在將以下代碼寫入xcode中,並且它的大小未定:首先在此函數中使用如何解決此問題,以便我可以運行代碼?Xcode錯誤一直說大小未申報如何解決這個問題?

// on "init" you need to initialize your instance 
-(id) init 
{ CCSprite *spaceCargoShip = [CCSprite 
          spriteWithFile:@"spaceCargoShip.png"]; 
[spaceCargoShip  setPosition:ccp(size.width/2, size.height/2)]; 
[self addChild:spaceCargoShip]; 

回答

2

size變量未在該函數中聲明。你必須從其他地方得到它 - 也許self.size,但沒有看到其他代碼,我不知道它應該從哪裏來。

0

我猜你想把貨船放在屏幕的左下角。爲此,您需要使用您創建的sprite的大小,即spaceCargoShip.contentSize。

而不是

[spaceCargoShip setPosition:ccp(size.width/2, size.height/2)]; 

使用

[spaceCargoShip setPosition:ccp(spaceCargoShip.contentSize.width/2, 
           spaceCargoShip.contentSize.height/2)]; 

玩得開心!

+0

thatnks的答案,我已經設法修復它現在:) – sam

0

我遇到了同樣的問題。

代碼必須放在init函數中並在if語句中。

- (id)init { 
    if (self = [super init]) { 
    //Code for the spaceCargoShip 
    ... 
    // ask director the the window size 
    CGSize size = [[CCDirector sharedDirector] winSize]; //<-- This is the "size" variable that the code is looking for. 
    ... 
    //Enter the code for the spaceCargoShip in here. 
    CCSprite *spaceCargoShip = [CCSprite spriteWithFile:@"SpaceCargoShip.png"]; 
    [spaceCargoShip setPosition:ccp(size.width/2, size.height/2)]; 
    [self addChild:spaceCargoShip]; 

    } 
    return self; 
} 

的原因,它不工作是變量超出範圍,如果你不把它放在if statement內。

相關問題