2013-03-31 4 views
2

나는 다음과 같은 현상이 발생하는 이유를 알지 못합니다.자식에서 부모의 CCSpriteBatchNode에 스프라이트를 추가 할 때만 Cocos2D 업데이트가 실행되는 이유는 무엇입니까?

저는 GameLayer (CCLayer) 클래스와 Food (CCNode) 클래스를 가지고 있습니다.

게이 레이어 클래스에서 나는 스프라이트를 속성으로 가지는 많은 음식 개체를 만듭니다. 그리고이 스프라이트를 CCSpriteBatchNode에 추가하고 싶습니다.

spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"bol.png"]; 
[self addChild:spriteBatch]; 
for (int i = 0; i < 1000; i++) { 

    Food * tempfood = [Food foodWithParentNode:self]; 

    [spriteBatch addChild:[tempfood mySprite]]; 

    [self addChild:tempfood]; 

} 

위의 코드를 사용하면 스프라이트가 모두 화면에 나타나지만 이동하지는 않습니다. (그들은 내가는 식품 클래스 (아래 참조의 업데이트를 계획해야하기 때문에)와이 업데이트에 식품 객체의 위치 변경)

-(id)initWithParentNode:(CCNode *)parentNode{ 

if((self = [super init])) 
{ 
    mySprite = [CCSprite spriteWithFile:@"bol.png"]; 

    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 

    [[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)]; 

    [self scheduleUpdate]; 

} 

return self; 
} 
-(void) update:(ccTime)delta 
{ 
    ... DO SOME position calculations ... 

[[self mySprite] setPosition:CGPointMake(newX, newY)]; 
} 

을하지만 난에서 배치에 스프라이트를 추가하는 코드를 이동하는 경우 Game 클래스를 음식 클래스로 변경하면 위치가 변경되고 음식이 화면에서 움직입니다. 왜?

그래서이 제공 :

-(id)initWithParentNode:(CCNode *)parentNode{ 

if((self = [super init])) 
{ 
    mySprite = [CCSprite spriteWithFile:@"bol.png"]; 

    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 

    [[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)]; 

    [[ (GameLayer*) parentNode spriteBatch] addChild:mySprite]; 

    [self scheduleUpdate]; 

} 

return self; 
} 

정말 음식 클래스

[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite]; 

또는 호출 사이의 차이를 볼 수 없습니다 다음 '부모'에서

[spriteBatch addChild:[tempfood mySprite]]; 

을 게임 플레이어

+0

당신이 있는지 확인 그 mySprite는 인스턴스 mySprite는 속성 포인트? 어쩌면 당신이 당신의 게임 레이어에서 그것을 추가하고 싶은 순간에 아무래도 무의미한 것일 수도 있습니다. – Morion

+0

화면에 나타나기 때문에 스케줄링 된 업데이트가 실행되지 않기 때문에 분명히 0이 아닙니다 ... –

+0

당신은 그것을 말할 수 없습니다 이것이 단지 재산 일 뿐이므로 "확실히 없다"는 뜻입니다. 스프라이트 (Food 클래스의 mySprite 변수)를 만들 수 있지만 property를 약간 잘못 선언하면 foodInstance.mySprite가 0이 될 수 있습니다. – Morion

답변

1

Ruben, mySprite는 속성을 보유하고있는 속성입니까? Food 클래스가이 속성의 메모리 참조를 잃어 버릴 수 있습니다.

초기화시 self.mySprite를 사용하여 mySprite를 설정하려고 시도합니다.

하는 .m 또는 .H에 넣어 :

@property (nonatomic, retain) CCSprite *mySprite 

및 초기화에를 사용 :

self.mySprite = [CCSprite spriteWithFile:@"bol.png"];