0

I have a strange issue.

In my game, I add a "target to the scene in the first level like so:

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

}

This is all fine and good, until I want to make that "target" have an animation as it is falling. So what I do is create the batchNodes and whatnot in the 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];

And then I tried to change my -AddTarget method to look like this:

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

}

I commented in where the Problem is by using NSLogs to find the line. I get the error

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

Not on startup, but when AddTarget is run (it runs on a CCScheduleTimer).

Any Ideas? Thanks

tyler53
  • 429
  • 1
  • 5
  • 16

1 Answers1

0

As cocos2d is open-source, you can see actual assert line in it's code. I assume that you tried to add to self sprite that is already added to another parent. In this case it is your CCSpriteBatchNode instance. Any CCNode's subclas will cause assertion failure if you try to add it to some parent while it already have parent.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • so then how would i fix the problem @Morion – tyler53 Nov 13 '12 at 00:00
  • I got rid of the [spriteSheet addChild: _Duckling]; in the init, and just did the [self addChild: _Duckling]; in the addTarget method, and it gets all the way through the method (checked by NSLogs) but crashes right after and doesn't add the target. @Morion – tyler53 Nov 13 '12 at 23:10
  • it is hard to say where is your problem without crash messages – Morion Nov 14 '12 at 04:12
  • that's my issue, it doesn't give me an error in the debugger it just crashes – tyler53 Nov 14 '12 at 16:17
  • if it really does not print any crash reason to console, but always crashes, add all-exceptions breakpoint. it will allow you to see the line that is caused crash. – Morion Nov 14 '12 at 16:35
  • I probably neglected to mention that I have already done that. It breaks to the CCActionInterval.m class at the line `if(! [sprite isFrameDisplayed: [frames objectAtIndex: idx]]) – tyler53 Nov 15 '12 at 01:07
  • I probably neglected to mention that I have already done that. It breaks to the CCActionInterval.m class at the line `if(! [sprite isFrameDisplayed: [frames objectAtIndex: idx]]) – tyler53 Nov 27 '12 at 04:15