开发者

Xcode error keeps saying size undeclared how do i fix this?

I am writing the below code into xcode and it keeps coming up size undeclared: first use in this function how do I fix this so I can run the code?

// 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];


The size variable isn't declared in that function. You have to get it from somewhere else — perhaps self.size, but without seeing the rest of the code I don't know where it should be coming from.


I'm guessing that you want to position your cargo ship at the lower left corner of the screen. For that, you want to use the size of the sprite you created, which is spaceCargoShip.contentSize.

Instead of

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

use

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

Have fun!


I was having the same issue.

The code has to be put in the init function and inside the if statement.

- (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;
}

The reason that it is not working is that the variable falls out of scope if you don't put it within the if statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜