2012-11-12 111 views
0

我有一個奇怪的問題。雪碧沒有添加到場景?

在我的比賽,我添加了一個「目標到現場的第一級,像這樣:

//Adds the "targets" or in this case falling objects, to the scene and spawns/moves them 
-(void)addTarget { 
CCSprite *target = [CCSprite spriteWithFile:@"bankercatch.png" 
             rect:CGRectMake(0, 0, 21, 40)]; 

target.tag = 1; 

[_targets addObject:target]; 

// Determine where to spawn the target along the X axis 
CGSize winSize = [[CCDirector sharedDirector] winSize]; 
int minX = target.contentSize.width/2; 
int maxX = winSize.width - target.contentSize.width/2; 
int rangeX = maxX - minX; 
int actualX = (arc4random() % rangeX) + minX;//Randomizes the place it will spawn on X-Axis 

// Create the target slightly off-screen along the top edge, 
// and along a random position along the X axis as calculated above 
target.position = ccp(actualX, winSize.height + (target.contentSize.height/2)); 
[self addChild:target]; 

// Determine speed of the target 
int minDuration = 3.0; 
int maxDuration = 5.0; 
int rangeDuration = maxDuration - minDuration; 
int actualDuration = (arc4random() % rangeDuration) + minDuration;//Speed is randomized between 2 and 5 

// Create the actions 
id actionMove = [CCMoveTo actionWithDuration:actualDuration 
            position:ccp(actualX, -target.contentSize.height/2)]; 
id actionMoveDone = [CCCallFuncN actionWithTarget:self 
             selector:@selector(spriteMoveFinished:)]; 
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

} 

這是一切優秀和良好的,直到我想拍的是‘目標’有一個動畫作爲它正在下降所以我要做的就是創建batchNodes和諸如此類的東西在init

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"DuckSpriteSheet_default.plist"]; 

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"DuckSpriteSheet_default.png"]; 
    [self addChild:spriteSheet]; 
NSMutableArray *fallAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <=4; ++i) { 
     [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"duckling%d.png", i]]]; 

    } 

    CCAnimation *fallAnim = [CCAnimation animationWithFrames:fallAnimFrames delay:0.1f]; 
    self.Duckling = [CCSprite spriteWithSpriteFrameName:@"duckling1.png"]; 
    _Duckling.position = ccp(winSize.width*1.5, winSize.height*1.5); 
    self.fallAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:fallAnim restoreOriginalFrame:NO]]; 
    //[_Banker runAction:_WalkAction]; 
    [spriteSheet addChild:_Duckling]; 

然後我試圖改變我的-AddTarget方法是這樣的:

//Adds the "targets" or in this case falling objects, to the scene and spawns/moves them 
-(void)addTarget { 
//CCSprite *target = [CCSprite spriteWithFile:@"bankercatch.png" 
//          rect:CGRectMake(0, 0, 21, 40)]; 
_Duckling.tag = 1; 

[_targets addObject:_Duckling]; 
[_Duckling runAction:_fallAction]; 
// Determine where to spawn the target along the X axis 
CGSize winSize = [[CCDirector sharedDirector] winSize]; 
int minX = _Duckling.contentSize.width/2; 
int maxX = winSize.width - _Duckling.contentSize.width/2; 
int rangeX = maxX - minX; 
int actualX = (arc4random() % rangeX) + minX;//Randomizes the place it will spawn on X-Axis 
NSLog(@"About to add Duckling to self"); 
// Create the target slightly off-screen along the top edge, 
// and along a random position along the X axis as calculated above 
_Duckling.position = ccp(actualX, winSize.height + (_Duckling.contentSize.height/2)); 
[self addChild:_Duckling];//PROBLEM HERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
NSLog(@"Added _Duckling to self"); 
// Determine speed of the target 
int minDuration = 3.0; 
int maxDuration = 5.0; 
int rangeDuration = maxDuration - minDuration; 
int actualDuration = (arc4random() % rangeDuration) + minDuration;//Speed is randomized between 2 and 5 

// Create the actions 
id actionMove = [CCMoveTo actionWithDuration:actualDuration 
            position:ccp(actualX, -_Duckling.contentSize.height/2)]; 
id actionMoveDone = [CCCallFuncN actionWithTarget:self 
             selector:@selector(spriteMoveFinished:)]; 
[_Duckling runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

} 

我通過使用NSLogs來查找問題的位置以找到該行。我得到的錯誤

Assertion failure in -[DuckL1Layer addChild:z:tag:], /Users/tyler_reynold/Desktop/Programming/Games/Catch It/Catch It/libs/cocos2d/CCNode.m:388 

在啓動時不,但運行時AddTarget(它運行在一個CCScheduleTimer)。

任何想法?謝謝

+0

顯示控制檯輸出,它會說斷言的類型。 –

回答

0

由於cocos2d是開源的,你可以在它的代碼中看到實際的斷言行。我假設你試圖添加到self已經添加到另一個家長的精靈。在這種情況下,它是您的CCSpriteBatchNode實例。任何CCNode的子控件都會導致斷言失敗,如果您嘗試將其添加到某個父母,而它已經有父母。

+0

那麼那麼我將如何解決這個問題@Morion – tyler53

+0

emmm ....怎麼樣不要添加節點給幾個父母? – Morion

+0

我擺脫了[spriteSheet addChild:_Duckling];在init中,只是做了[self addChild:_Duckling];在addTarget方法中,它會一路通過該方法(由NSLog檢查),但是會立即崩潰並且不會添加目標。 @Morion – tyler53