精灵没有添加到场景?

时间:2012-11-12 01:18:46

标签: ios cocos2d-iphone ccsprite addchild

我有一个奇怪的问题。

在我的游戏中,我在第一级添加了一个“目标”:

//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]];

}

这一切都很好,直到我想让“目标”有一个动画,因为它正在下降。所以我要做的是在init

中创建batchNodes和whatnot
[[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上运行)。

任何想法?感谢

1 个答案:

答案 0 :(得分:0)

由于cocos2d是开源的,你可以在它的代码中看到实际的断言行。我假设您尝试添加到已添加到另一个父级的self精灵。在这种情况下,它是您的CCSpriteBatchNode实例。任何CCNode的子类都会导致断言失败,如果你尝试将它添加到某个父级已经有父级的话。