在一个无休止的循环中,一次添加一个敌人到屏幕上,每个敌人之间都有延迟,但这不起作用



你好,我正在开发一款侧面滚动的cocos2d游戏,在这个游戏中,敌人会以无休止的循环一次一个地添加到屏幕上。我想知道如何在每个增加的敌人之间设置延迟。此外,当"blueEnemy"被添加时,第二个出现在屏幕上,而只有一个应该出现。

这是我到目前为止的代码:

#import "FlyingEnemy.h"
@implementation FlyingEnemy
+(id)createEnemies{
return [[[self alloc]init]autorelease];
}
-(id)init{
if((self = [super init])){
    CGSize size = [[CCDirector sharedDirector]winSize];
    screenWidth = size.width;
    screenHeight = size.height;
    screenBounds = [[UIScreen mainScreen] bounds];
    redEnemyFlameCounter = 1;
    randomNumberSpinningRockRepeat = arc4random() % 4;
    xPoint = screenWidth - 50;
    yPoint = screenHeight - 50;
    yellowEnemyFlameCounter = 1;
    blueEnemyFlameCounter = 1;
    xPointBlueEnemy = screenWidth - 50;
    yPointBlueEnemy = screenHeight - 50;
    [self redEnemyFlight];
}
    return self;
}  
-(void)redEnemyFlight{
[self unschedule:@selector(redEnemyStart:)];
redEnemy = [CCSprite spriteWithFile:@"redenemy.png"];
redEnemy.position = ccp(xPoint, yPoint);
[self addChild:redEnemy z:-1];
[self schedule:@selector(shootTheBullets:)interval:1.0f/2.0f];
CCMoveTo* redEnemyMoveDown  = [CCMoveTo actionWithDuration:3.0 position:ccp(xPoint, 70)];
CCMoveTo* redEnemyMoveUp = [CCMoveTo actionWithDuration:3.0 position:ccp(xPoint, yPoint - 60)];
CCSequence* redEnemyFloatingSequence = [CCSequence actions:redEnemyMoveDown, redEnemyMoveUp, nil];
CCRepeat* redEnemyFloatingRepeat = [CCRepeat actionWithAction:redEnemyFloatingSequence times:3];
[redEnemy runAction:redEnemyFloatingRepeat];
[self schedule: @selector(removeTheEnemy:)interval:18.0f/1.0f];
[self schedule: @selector(redEnemyFlame:)interval:1.0f/5.0f];
}
-(void)redEnemyFlame:(ccTime)delta{
redEnemyFlameCounter ++;
if (redEnemyFlameCounter % 2){
    [redEnemy setTexture:[[CCSprite spriteWithFile:@"redenemy2.png"]texture]];
}else{
    [redEnemy setTexture:[[CCSprite spriteWithFile:@"redenemy.png"]texture]];
}
}
-(void)removeTheEnemy:(ccTime)delta{
CCMoveBy* moveUp = [CCMoveBy actionWithDuration:0.5 position:ccp(70, 100)];
[redEnemy runAction:moveUp];
[self unschedule:@selector(removeTheEnemy:)];
[self schedule:@selector(yellowEnemyStart:)interval:1.0f/60.0f];
}
-(void)yellowEnemyStart:(ccTime)delta{
if (redEnemy.position.x > screenWidth || redEnemy.position.y > screenHeight) {
    [self yellowEnemyFloating];
}
}
-(void)yellowEnemyFloating{
[self unschedule:@selector(yellowEnemyStart:)];
yellowEnemy = [CCSprite spriteWithFile:@"yellowenemy.png"];
yellowEnemy.position = ccp(screenWidth - 50, 50);
[self addChild:yellowEnemy z:-1];
yellowEnemyMoveDown  = [CCMoveTo actionWithDuration:2.0 position:ccp(yellowEnemy.position.x, 50)];
yellowEnemyMoveUp = [CCMoveTo actionWithDuration:2.0 position:ccp(yellowEnemy.position.x, screenHeight/2)];
yellowEnemyFloatingSequnece = [CCSequence actions:yellowEnemyMoveUp, yellowEnemyMoveDown, nil];
yellowEnemyFloatingRepeat = [CCRepeat actionWithAction:yellowEnemyFloatingSequnece times:2];
[yellowEnemy runAction:yellowEnemyFloatingRepeat];
[self schedule: @selector(yellowEnemyFlame:)interval:1.0f/5.0f];
}
-(void)yellowEnemyFlame:(ccTime)delta{
yellowEnemyFlameCounter ++;
if (yellowEnemyFlameCounter % 2){
    [yellowEnemy setTexture:[[CCSprite spriteWithFile:@"yellowenemy2.png"]texture]];
}else{
    [yellowEnemy setTexture:[[CCSprite spriteWithFile:@"yellowenemy.png"]texture]];
}
[self schedule:@selector(yellowEnemyFlight:)interval:8.0f/1.0f];
}
-(void)yellowEnemyFlight:(ccTime)delta{
yellowEnemyMoveLeft = [CCMoveTo actionWithDuration:4.0 position:ccp(-100, screenHeight/2)];
[yellowEnemy runAction:yellowEnemyMoveLeft];
[self schedule:@selector(removeTheYellowEnemy:)interval:4.0f/1.0f];
}
-(void)removeTheYellowEnemy:(ccTime)delta{
CCMoveTo* removeYellowEnemy = [CCMoveTo actionWithDuration:1.0 position:ccp(-100, screenHeight/2)];
[yellowEnemy runAction:removeYellowEnemy];
[self unschedule:@selector(removeTheYellowEnemy:)];
[self schedule:@selector(blueEnemyStart:)interval:1.0f/60.0f];
}
-(void)blueEnemyStart:(ccTime)delta{
if (yellowEnemy.position.x < 0 || yellowEnemy.position.y < 0) {
    if (redEnemy.position.x > screenWidth || redEnemy.position.y > screenHeight) {
        [self blueEnemyFlight];
    }
}
}
-(void)blueEnemyFlight{
[self unschedule:@selector(blueEnemyStart:)];
blueEnemy = [CCSprite spriteWithFile:@"blueenemy.png"];
blueEnemy.position = ccp(xPointBlueEnemy, yPointBlueEnemy);
[self addChild:blueEnemy z:-1];
[self schedule:@selector(shootTheWaterBullets:)interval:1.0f/2.0f];
CCMoveTo* blueEnemyMoveDown  = [CCMoveTo actionWithDuration:3.0 position:ccp(xPointBlueEnemy, 70)];
CCMoveTo* blueEnemyMoveUp = [CCMoveTo actionWithDuration:3.0 position:ccp(xPointBlueEnemy, yPointBlueEnemy - 60)];
CCSequence* blueEnemyFloatingSequence = [CCSequence actions:blueEnemyMoveDown, blueEnemyMoveUp, nil];
CCRepeat* blueEnemyFloatingRepeat = [CCRepeat actionWithAction:blueEnemyFloatingSequence times:3];
[blueEnemy runAction:blueEnemyFloatingRepeat];
[self schedule: @selector(removeTheBlueEnemy:)interval:18.0f/1.0f];
[self schedule: @selector(blueEnemyFlame:)interval:1.0f/5.0f];
}
-(void)blueEnemyFlame:(ccTime)delta{
blueEnemyFlameCounter ++;
if (blueEnemyFlameCounter % 2){
    [blueEnemy setTexture:[[CCSprite spriteWithFile:@"blueenemy2.png"]texture]];
}else{
    [blueEnemy setTexture:[[CCSprite spriteWithFile:@"blueenemy.png"]texture]];
}
}
-(void)removeTheBlueEnemy:(ccTime)delta{
CCMoveBy* moveUpBlueEnemy = [CCMoveBy actionWithDuration:0.5 position:ccp(200, 400)];
[blueEnemy runAction:moveUpBlueEnemy];
[self unschedule:@selector(removeTheBlueEnemy:)];
[self performSelector:@selector(redEnemyStart:)];
}
-(void)redEnemyStart:(ccTime)delta{
if (blueEnemy.position.x > screenWidth || blueEnemy.position.y > screenHeight) {
    [self redEnemyFlight];
}
}
@end

CCRepeatForever应该能做到这一点。请以这个片段为例。-

CCDelayTime *delay = [CCDelayTime actionWithDuration:kDelayTime];
CCCallFunc *spawnEnemy = [CCCallFunc actionWithTarget:self selector:@selector(myFoeGeneratorSelector)];
CCRepeatForever *forever = [CCRepeatForever actionWithAction:[CCSequence actionOne:delay two:spawnEnemy]];
[self runAction:forever];

如果您希望该方法被调用一次,只需运行de CCSequence。-

[self runAction:[CCSequence actionOne:delay two:spawnEnemy]];

希望能有所帮助。

相关内容

最新更新