0

我目前正在开发一款游戏,在该游戏中我将 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 项目之一。提前致谢!

4

1 回答 1

3

好吧,这就是我的做法:

  1. 在初始化时将 ghostSpawner 发送给 self(就像你现在所做的那样)。

  2. 在 ghostSpawner 中,获取你的精灵,重置它(将不透明度更改为 0,重置位置,无论如何),然后发送 2 个 runAction 消息:一个淡入,另一个序列移动到结束位置,然后发送 killGhost给自己的信息。

  3. 在killGhost中,检查分数是否小于50。如果是,则再次将ghostSpawner发送给自己,否则当他们达到50分时进行任何操作。

你的 sprite 创建代码应该在 init 中。您可以重复使用相同的精灵。此外,您可以使用 CCFadeIn 代替 CCTintTo。

于 2011-12-18T03:34:17.167 回答