1

您好,我正在制作一个横向滚动的 Cocos2d 游戏,并且我正在将敌人添加到游戏中,因此当一个敌人完成时,另一个敌人会出现在屏幕上。我有使敌人按照预定方法移动的方法,所以我使用 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

2

原因是 scheduleSelector 和 performSelector 的预期方法声明不同:

// scheduleSelector sends a delta time (float)
-(void) method:(ccTime)delta;

// performSelector sends no parameters
-(void) method;

// performSelector:withObject: sends an id parameter
-(void) method:(id)object;

所以你不能这样做:

[self performSelector:@selector(redEnemyFlight:) withObject:nil afterDelay:2.0f];

因为要执行的方法有错误的签名:

-(void)redEnemyFlight:(ccTime)delta;

您应该scheduleSelector在这种情况下使用。此外,如果您在 中运行上述 performSelector 代码,这将在对象(即)BlueEnemy中执行选择器。要实际对红色敌人执行选择器,您必须引用红色敌人并执行以下操作:BlueEnemyself

[redEnemy performSelector: and so on ];

PS:避免在外部对象中调度选择器,而是向外部对象发送消息并在该方法中启动调度。每个对象都应该管理它自己的选择器。

于 2013-08-11T22:35:20.680 回答