我目前正在开发一款游戏,在该游戏中我将 CCSprite 添加到场景(动画幽灵)中,并让它从随机生成的点移动到另一个随机生成的点,淡出,然后从场景中移除。当玩家点击幽灵时,分数会加 1。
一切都很顺利,但我希望重复此操作,直到满足条件(特别是当分数达到 50 时)。我想不出最好的方法来做到这一点。每次我尝试运行循环时,它都会无限重复并导致游戏崩溃。
实现这种循环的最佳方法是什么?我听说过为精灵创建数组,但一次在场景中只会出现一次幽灵的迭代。有没有其他方法可以实现这一目标?
理想情况下,我想要实现的是:运行动作,等待它们完成,然后再次运行它们,直到得分为 50。
这是我的代码:
-(void) ghostSpawner {
CGPoint endPoint = [self randomPointGenerator];
[self birthGhost];
CCSprite * Ghost = (CCSprite *) [self getChildByTag:2];
//...then fade him in
CCFiniteTimeAction *ghostBegin = [CCSequence actions:[CCDelayTime actionWithDuration:1],[CCTintTo actionWithDuration:0 red:0 green:0 blue:0],[CCTintTo actionWithDuration:.5 red:255 green:255 blue:255], nil];
//...move him over a given amount of time, then fade him out and DIE
CCFiniteTimeAction *ghostEnd = [CCSequence actions:[CCMoveTo actionWithDuration:2 position:endPoint],[CCTintTo actionWithDuration:1 red:0 green:0 blue:0],[CCCallFunc actionWithTarget:self selector:@selector(killGhost)], nil] ;
[Ghost runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ghostBegin,ghostEnd, nil]]];
}
用于生成精灵:
-(void) birthGhost {
CGPoint spawnPoint = [self randomPointGenerator];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ghostidle.plist"];
CCSprite * Ghost = [CCSprite spriteWithSpriteFrameName:@"Ghost0001.png"];
[Ghost setColor:ccc3(0,0,0)];
Ghost.anchorPoint = ccp(.5, .5);
Ghost.position = spawnPoint;
Ghost.anchorPoint = ccp(0.5f,0.5f);
Ghost.scale = .4;
Ghost.tag = 2;
[Ghost runAction:ghostIdleAnimation()];
[self addChild:Ghost z:0];
}
'ghostSpawner' 函数在我的场景中的'init' 中被调用
[self ghostSpawner];
我希望这已经足够了。任何反馈都会很棒,因为这是我第一个真正的 iPhone 项目之一。提前致谢!