1

こんにちは、横スクロールの Cocos2d ゲームを作成しています。ゲームに敵を実装しているので、1 つの敵が完了すると、別の敵が画面に表示されます。スケジュールされたメソッドとして敵を動かすメソッドがあるので、 performSelector メソッドを使用して他のメソッドを呼び出しています。

コードは次のとおりです。

#import "RedEnemy.h"

@implementation RedEnemy
+(id)createRedEnemy{
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;

xPoint = screenWidth - 50;
yPoint = screenHeight - 50;

    randomNumberRedEnemy = arc4random() % 18;

    [self schedule:@selector(redEnemyFlight:)interval:randomNumberRedEnemy + 18/1.0f];
}
return self;
}

-(void)redEnemyFlight:(ccTime)delta{
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 performSelector:@selector(yellowEnemyFloating:) withObject:nil afterDelay:2.0f];
}
@end

これは、最後のコード スニペットが呼び出すものです。

#import "YellowEnemy.h"

@implementation YellowEnemy

+(id)createYellowEnemy{
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];

    yellowEnemyFlameCounter = 1;

    randomNumberYellowEnemy = arc4random() % 8;

    //[self schedule:@selector(yellowEnemyFloating:)interval:randomNumberYellowEnemy +       8 /1.0f];

}
return self;
}

-(void)yellowEnemyFloating:(ccTime)delta{
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 performSelector:@selector(blueEnemyFlight:) withObject:nil afterDelay:2.0f];
}
@end

これは、最後のコード スニペットが呼び出すものです。

 #import "BlueEnemy.h"


 @implementation BlueEnemy
+(id)createBlueEnemy{
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];

    blueEnemyFlameCounter = 1;

    xPointBlueEnemy = screenWidth - 50;
    yPointBlueEnemy = screenHeight - 100;

    //randomNumberBlueEnemy = arc4random() % 18;

    //[self schedule:@selector(blueEnemyFlight:)interval:randomNumberBlueEnemy + 18/1.0f];
}
return self;
}

-(void)blueEnemyFlight:(ccTime)delta{
blueEnemy = [CCSprite spriteWithFile:@"blueenemy.png"];
blueEnemy.position = ccp(xPointBlueEnemy, yPointBlueEnemy);
[self addChild:blueEnemy z:-1];

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(shootTheWaterBullets:)interval:1.0f/2.0f];

[self schedule: @selector(blueEnemyFlame:)interval:1.0f/5.0f];

[self schedule: @selector(removeTheBlueEnemy:)interval:20.0f/1.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(70, 200)];
[blueEnemy runAction:moveUpBlueEnemy];
[blueEnemy removeChild:blueEnemy cleanup:YES];
[self unschedule:@selector(removeTheBlueEnemy:)];
[self performSelector:@selector(redEnemyFlight:) withObject:nil afterDelay:2.0f];
}

@end
4

1 に答える 1