这里有一些为敌人添加精灵的代码......
_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];
}
在Update方法中........
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 :(得分:0)
我同意LearnCocos2D之前提到的声明。您也可以添加多个对象并在行
中达到容量的末尾_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];
如果您尝试生成的对象多于您为kNumAsteroids指定的任何数字,它将删除最旧的对象并在其位置使用新对象。即如果kNumAsteroids是5,你在屏幕上有5,然后加上第六,1将变为6,它的位置将是你设置的任何位置。