【发布时间】:2013-02-25 12:39:53
【问题描述】:
这里有一些为敌人添加精灵的代码.....
_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];
for (int i = 0; i < kNumAstroids; ++i) {
CCSprite *asteroid = [CCSprite spriteWithSpriteFrameName:@"robber.png"];
asteroid.visible = NO;
[_batchNode addChild:asteroid];
[_robbers addObject:asteroid];
}
在更新方法中............
double curTime = CACurrentMediaTime();
if (curTime > _nextRunemanSpawn) {
float randSecs = [self randomValueBetween:0.20 andValue:1.0];
_nextRunemanSpawn = randSecs + curTime;
float randY = [self randomValueBetween:80 andValue:80];
float randDuration = [self randomValueBetween:4.5 andValue:4.5];
float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];
CCSprite *asteroid = [_robbers objectAtIndex:_nextRobber];
_nextRobber++;
if (_nextRobber >= _robbers.count) {
_nextRobber = 1;
}
[asteroid stopAllActions];
asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY);
asteroid.visible = YES;
[asteroid runAction:[CCSequence actions:[CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width-asteroid.contentSize.width, 0)],
[CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],nil]];
所有精灵在屏幕中从右到左移动
当 Sprite 穿过屏幕中间时,它会自动消失
这个问题的原因是什么??
【问题讨论】:
-
当精灵到达屏幕中心时,有没有机会: [_robbers objectAtIndex:_nextRobber] 会为您提供与您重置位置相同的小行星对象?通过查看您的代码基本上看起来不错。另外你如何计算 winSize ?您是否在代码中的其他地方操作小行星物体?
-
我已经定义了#define kNumAstroids 2,如果我将它定义为 15 没有小行星会消失......为什么?
-
我认为这是因为 _nextRunemanSpawn 太小,以至于在小行星到达屏幕中心之前将经过足够的时间,以便您从 _robbers 数组中获得相同的小行星,然后停止操作并重置位置(在 if 测试后的更新方法中)
-
我的预感也是移动动画在前一个结束之前再次运行。尝试在运行序列之前停止所有动作,您可能会看到精灵再次从右侧移回。
标签: iphone ios cocos2d-iphone sprite