0

你好,我正在制作一个侧面滚动 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
4

2 回答 2

2

当您调用您的 removeEnemy 方法时,您不会调用相应的方法来从场景中移除您的子精灵。因此,每次您调用该方法时,都会添加另一个 CCSprite 而不会删除一个,这就是您看到重复项的原因。确保调用 removeChild: 来删除 CCSprite。

于 2013-08-14T03:27:45.880 回答
1

如果您的访问权限不正确,则意味着您的对象为零,或者如果您从场景中删除了您的对象,并且在此之后您尝试对该对象执行某些操作,例如对其运行操作,则访问权限不正确。

于 2013-08-14T05:36:46.337 回答