2013-02-25 64 views
1

这里加雪碧仇敌一些代码后消失.....精灵是一些时间

_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穿越屏幕中间时自动消失
这个问题的原因是什么?

+0

任何机会,当精灵到达屏幕的中心线:[_robbers objectAtIndex:_nextRobber]给你相同的小行星对象,你重置位置?通过查看你的代码基本上看起来很好。还有你如何计算winSize?你是否在你的代码中的其他地方操纵小行星物体? – giorashc 2013-02-25 13:00:12

+0

我已经定义#define kNumAstroids 2如果我将它定义为15没有小行星会消失......为什么? – 2013-02-25 13:09:15

+1

我认为这是因为_nextRunemanSpawn太小,所以直到小行星到达屏幕中心足够的时间才会通过,这样你就可以从_robbers阵列获得相同的小行星,然后停止动作并重置位置(在你的if test后更新方法) – giorashc 2013-02-25 13:20:27

回答

0

我同意以前从LearnCocos2D提到的声明。也可能是您要添加多个对象,并达到容量末端,在该行

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

如果试图产卵比任何数字已为kNumAsteroids指定多个对象,将清除最久的对象和用途新的取代它。即如果kNumAsteroids为5,则屏幕上有5个,然后添加第六个,1将变为6,其位置将是您设置的任何位置。