-2

您好,我正在制作一个 cocos2d 侧滚轴应用程序,我希望敌人随机飞向角色。我是 Objective c 和 cocos2d 的初学者,所以这似乎是一个简单的问题,但任何帮助都是有价值的。arc4random 会有帮助吗?

到目前为止,这是我的代码。我希望这四种方法之一:redEnemyFlight、yellowEnemyFloating、blueEnemyFlight 和 spinTheRock 在一个接一个的无限循环中被随机调用。

#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 spinTheRock];
}
return self;
}

-(void)spinTheRock{
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];
}

-(void)removeTheSpinningRock:(ccTime)delta{
[self unschedule:@selector(removeTheSpinningRock:)];

[self stopAllActions];

[self createFlyingEnemy];
}

-(void)redEnemyFlight{
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];
}

-(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:)];
}

-(void)yellowEnemyFloating{
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, 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:)];
}

-(void)blueEnemyFlight{
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, yPointBlueEnemy - 10)];
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];
}

-(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:)];
}

-(void) createFlyingEnemy{
// I am assuming you have an Enemy class or equivalent,
// and that it subclasses from CCNode somehow

NSUInteger randomEnemy = arc4random() % 3;  // will return 0-3
return [self createEnemyOfType:randomEnemy];

}

-(void) createEnemyOfType:(NSUInteger) enemyType {
switch(enemyType) {
    case 0: return [self redEnemyFlight];
    case 1: return [self yellowEnemyFloating];
    case 2: return [self blueEnemyFlight];
    default : return [self spinTheRock];
}
}

-(void)checkForOverlap{
if (isRedEnemyOnTheScreen == YES) {
    [self removeChild:yellowEnemy cleanup:YES];
    [self removeChild:blueEnemy cleanup:YES];
    [self removeChild:spinningRock cleanup:YES];
}

if (isYellowEnemyOnTheScreen == YES) {
    [self removeChild:redEnemy cleanup:YES];
    [self removeChild:blueEnemy cleanup:YES];
    [self removeChild:spinningRock cleanup:YES];
}

if (isBlueEnemyOnTheScreen == YES) {
    [self removeChild:redEnemy cleanup:YES];
    [self removeChild:yellowEnemy cleanup:YES];
    [self removeChild:spinningRock cleanup:YES];
}

if (isSpinningRockOnTheScreen == YES) {
    [self removeChild:redEnemy cleanup:YES];
    [self removeChild:yellowEnemy cleanup:YES];
    [self removeChild:blueEnemy cleanup:YES];
}
}
@end
4

1 回答 1

0

我会在你的控制器中添加这样的东西:

// in your .h add a few iVars

float minDelay,maxDelay;
BOOL spawnEnabled;

// in your init setup a couple of iVars

minDelay = 0.5;                      // seconds
maxDelay = 2.0;                      // seconds
spawnEnabled = NO;

// where you are ready to spawn

spawnEnabled = YES;
[self scheduleOnce:@selector(spawnOneEnemy) delay:0.];

// add the spawning method 

-(void) spawnOneEnemy {

   if(!spawnEnabled) return;                // prevent spawning if this was called
                                            // after a gameOver or Win was declared

    // here, compute the random x,y coords where you want to spawn

    CGPoint spawnCoords = [self randomSpawnCoordinates];

    // create and add an enemy at the random coordinate

    Enemy * enemy = [self createEnemy];
    enemy.position = spawnCoords;
    [self addChild enemy];                 // assumes 'self' is a CCNode subclass

    // compute the next spawn time 

    u_int32_t delta = (u_int32_t) (ABS(maxDelay-minDelay)*1000);  // ms resolution
    float randomDelta = arc4random_uniform(delta)/1000.;          // now in seconds
    [self scheduleOnce:@selector(spawnOneEnemy) delay:randomDelta];

}


// where you win 

    spawnEnabled = NO;

// where you lose

    spawnEnabled = NO;

// left as an exercise to the reader :

-(CGPoint) randomSpawnCoordinates {
}

-(Enemy *) createEnemy {
    // I am assuming you have an Enemy class or equivalent, 
    // and that it subclasses from CCNode somehow

    NSUInteger randomEnemy = arc4random_uniform(4);  // will return 0-3
    return [self createEnemyOfType:randomEnemy];

}

-(Enemy *) createEnemyOfType:(NSUInteger) enemyType {

    // assumes all your enemy types subclass Enemy

    switch(enemyType) {
       case 0: return [self createEnemyOfType0];
       case 1: return [self createEnemyOfType1];
       case 2: return [self createEnemyOfType2];
       case 3: return [self createEnemyOfType3];
       default : return [self createEnemyOfType1];
    }
}

编辑:澄清了 iVars 声明。编辑:对随机敌人提出一些想法

于 2013-08-13T15:46:55.377 回答