0

こんにちは、敵がキャラクターを攻撃する横スクロール cocos2d アプリを作成しています。arc4random と switch ステートメントを使用して、一度に 1 つずつ敵をスポーンしています。これは発生するはずですが、一度に 2 つ生成されます。私は cocos2d と目的の 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

1

アクセスが悪い場合は、オブジェクトがゼロであることを意味します。または、オブジェクトをシーンから削除した後、このオブジェクトで何かをしようとしている場合、たとえば、オブジェクトに対してアクションを実行しようとしている場合は、アクセスが悪いことを意味します。

于 2013-08-14T05:36:46.337 に答える