你好我正在制作一个侧面滚动cocos2d应用程序,敌人攻击一个角色。我使用arc4random和switch语句一次一个地生成敌人。这应该发生但它反而会产生两个。我是cocos2d和Objective c的初学者,所以我可能会遗漏一些简单的东西。任何帮助都是有价值的。
以下是代码:
#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;
xPoint = screenWidth - 50;
yPoint = screenHeight/2;
yellowEnemyFlameCounter = 1;
blueEnemyFlameCounter = 1;
xPointBlueEnemy = screenWidth - 50;
yPointBlueEnemy = screenHeight - 50;
[self createEnemyOfType];
}
return self;
}
-(void) createEnemyOfType{
randomEnemy = arc4random_uniform(4);
CCLOG(@"the number is %i",randomEnemy);
switch(randomEnemy) {
case 0: isRedEnemyOnTheScreen = YES;
redEnemy = [CCSprite spriteWithFile:@"redenemy.png"];
redEnemy.position = ccp(xPoint, yPoint);
[self addChild:redEnemy z:-1];
[self schedule:@selector(shootTheBullets:)interval:1.0f/2.0f];
[self schedule: @selector(removeTheEnemy:)interval:18.0f/1.0f];
[self schedule: @selector(redEnemyFlame:)interval:1.0f/5.0f];
CCLOG(@"number = %i",randomEnemy);
break;
case 1: isYellowEnemyOnTheScreen = YES;
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];
CCLOG(@"number = %i",randomEnemy);
break;
case 2: isBlueEnemyOnTheScreen = YES;
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, screenHeight - 100)];
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];
CCLOG(@"number = %i",randomEnemy);
break;
case 3 : isSpinningRockOnTheScreen = YES;
spinningRock = [CCSprite spriteWithFile:@"rocks.png"];
spinningRock.position = ccp(screenWidth * 1.5, screenHeight/2);
[self addChild:spinningRock z:-1];
[spinningRock runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:2.0 angle:360]]];
moveTheRock = [CCMoveTo actionWithDuration:39.0 position:ccp(-500, spinningRock.position.y)];
[spinningRock runAction:moveTheRock];
[self schedule:@selector(removeTheSpinningRock:)interval:10.0f/1.0f];
CCLOG(@"number = %i",randomEnemy);
break;
}
}
-(void)removeTheSpinningRock:(ccTime)delta{
[self unschedule:@selector(removeTheSpinningRock:)];
[self stopAllActions];
[self createEnemyOfType];
isSpinningRockOnTheScreen = NO;
}
-(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:2.0 position:ccp(100, screenHeight/2)];
[redEnemy runAction:moveUp];
[self unschedule:@selector(removeTheEnemy:)];
[self createEnemyOfType];
isRedEnemyOnTheScreen = NO;
}
-(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, bulletY)];
[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 createEnemyOfType];
isYellowEnemyOnTheScreen = NO;
}
-(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 createEnemyOfType];
isBlueEnemyOnTheScreen = NO;
}
@end
答案 0 :(得分:2)
当您调用removeEnemy方法时,不会调用相应的方法从场景中删除子sprite。因此,每次调用该方法时,都会添加另一个CCSprite,而不会删除它,这就是您看到重复的原因。确保调用removeChild:删除CCSprite。
答案 1 :(得分:1)
如果您的访问权限不正确,则意味着您的对象是nill,或者如果您从场景中删除了您的对象,之后您正在尝试对此对象执行某些操作,例如对其执行操作,则访问权限不佳。