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とその他のものを作成することです

[[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 で実行されます)。

何か案は?ありがとう

4

1 に答える 1

0

cocos2d はオープンソースであるため、実際の assert 行をコードで確認できます。self別の親に既に追加されているスプライトに追加しようとしたと思います。この場合、それはあなたの CCSpriteBatchNodeインスタンスです。CCNode のサブクラスは、すでに親を持っているときに親に追加しようとすると、アサーション エラーが発生します。

于 2012-11-12T05:08:43.693 に答える