Xcode错误一直说大小未申报如何解决这个问题?
问题描述:
我正在将以下代码写入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];
答
size
变量未在该函数中声明。你必须从其他地方得到它 - 也许self.size
,但没有看到其他代码,我不知道它应该从哪里来。
答
我猜你想把货船放在屏幕的左下角。为此,您需要使用您创建的sprite的大小,即spaceCargoShip.contentSize。
而不是
[spaceCargoShip setPosition:ccp(size.width/2, size.height/2)];
使用
[spaceCargoShip setPosition:ccp(spaceCargoShip.contentSize.width/2,
spaceCargoShip.contentSize.height/2)];
玩得开心!
答
我遇到了同样的问题。
代码必须放在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
内。
thatnks的答案,我已经设法修复它现在:) – sam