2011-08-23 3 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

나는 당신이 화물칸을 화면의 왼쪽 아래 모퉁이에 위치시키고 자한다고 생각합니다. 이를 위해 작성한 스프라이트의 크기 인 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 안에 넣어하지 않는 경우 변수가 범위를 벗어날 것입니다.

관련 문제