0

您好,我正在开发一款横向滚动的 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
4

1 回答 1

0

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]];

希望能帮助到你。

于 2013-08-12T21:11:10.990 回答