【问题标题】:Sprites are disappear after some time一段时间后精灵消失
【发布时间】: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


【解决方案1】:

我同意 LearnCocos2D 前面提到的声明。也有可能您正在添加多个对象并达到行中容量的末尾

_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];

如果您尝试生成比您为 kNumAsteroids 指定的任何数量更多的对象,它将删除最旧的对象并使用新的对象代替它。 IE。如果 kNumAsteroids 为 5,则屏幕上有 5,然后添加第六个,1 将变为 6,其位置将是您设置的任何位置。

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多