У меня была такая же проблема.
Код должен быть помещен в функцию 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
.