-2

Ok, I know this should be simple ,but I am having a huge brain fart and can't figure this out all i want is the enemy to spawn once. Right now it is spawning every 180 seconds where i want it to spawn only once at the 180 seconds mark.

         [self schedule:@selector(gameLogicboss:) interval:180 ];        
          [self schedule:@selector(updateboss:)];                

     -(void)addTarget1 {

Boss *target1 = nil;    


if ((arc4random() % 2) == 0) {{
    target1 = [WeakAndFastBoss boss];
}}  else {
    target1 = [WeakAndFastBoss boss];
}                      

// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY = target1.contentSize.height/2;
int maxY = winSize.height - target1.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target1.position = ccp(winSize.width + (target1.contentSize.width/2), actualY);
[self addChild:target1 ];

// Determine speed of the target

int minDuration = target1.minMoveDuration;
int maxDuration = target1.maxMoveDuration;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target1.contentSize.width/2, actualY)];

id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                         selector:@selector(spriteMoveFinished:)];
[target1 runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
target1.tag = 1;
[_targets addObject:target1];   
     }

     -(void)gameLogicboss:(ccTime)dt {
     [self addTarget1];
       iterations_++;
   }

                    - (void)updateboss:(ccTime)dt {
            CGRect projectileRect = CGRectMake(projectile.position.x -                  (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2),                           projectile.contentSize.width,                                   projectile.contentSize.height);

    BOOL bossHit = FALSE;
    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *target1 in _targets) {
        CGRect target1Rect = CGRectMake(target1.position.x - (target1.contentSize.width/2),                                    target1.position.y - (target1.contentSize.height/2),                                    target1.contentSize.width,                                  target1.contentSize.height);

        if (CGRectIntersectsRect(projectileRect, target1Rect)) {

            //[targetsToDelete addObject:target];   
            bossHit = TRUE;
            Boss *boss = (Boss *)target1;
            boss.hp--;
            if (boss.hp <= 0) {
                _score ++;
                [targetsToDelete addObject:target1];
            }
            break;

        }                       
    }

    for (CCSprite *target in targetsToDelete) {
        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];                                  
        _projectilesDestroyed++;
        if (_projectilesDestroyed > 2) {

              } 
             }

    if (bossHit) {
        //[projectilesToDelete addObject:projectile];
        [[SimpleAudioEngine sharedEngine] playEffect:@"explosion.caf"];
    }
    [targetsToDelete release];
     }

-(void)spriteMoveFinishedboss:(id)sender {
    CCSprite *sprite = (CCSprite *)sender;
    [self removeChild:sprite cleanup:YES];
    GameOverScene *gameOverScene = [GameOverScene node];
    [gameOverScene.layer.label setString:@"You Lose"];
    [[CCDirector sharedDirector] replaceScene:gameOverScene];    

if (sprite.tag == 1) { // target
    [_targets removeObject:sprite];
} else if (sprite.tag == 2) { // projectile
    [_projectiles removeObject:sprite];
}
   }
thegrinner
  • 11,546
  • 5
  • 41
  • 64
Jhon Doe
  • 147
  • 10
  • 3
    There is nothing for us to go off of here. From your question and that tiny code sample, I can't tell what is happening here at all. Can you give us more code and a better explanation of your problem? – Void Star Jul 05 '12 at 19:34
  • sorry about that, I edited the code. – Jhon Doe Jul 05 '12 at 20:00
  • Maybe look at something like [this](http://stackoverflow.com/questions/5674375/calling-a-method-after-each-60-seconds-in-iphone) with NSTimer? Also, please check the indentation and whitespace of your code - it's hard to read as-is. – thegrinner Jul 05 '12 at 20:03
  • tried it didn't work at all. still spawns every 180 seconds instead of spawning only once. – Jhon Doe Jul 05 '12 at 20:15

2 Answers2

2

Going on a limb here. This piece of code will have the gameLogicBoss method execute every 180 seconds:

[self schedule:@selector(gameLogicboss:) interval:180];

If you want this to happen only once, you have to unschedule the selector when the method is executed:

-(void) gameLogicboss:(ccTime)delta
{
    [self unschedule:_cmd];

    // rest of the code here …
}

_cmd is shorthand for the selector of the current method. You can of course also use @selector(…) to unschedule a selector from a different method.

Cocos2D 2.0 also has a method named scheduleOnce which will call the selector only once without having to unschedule it manually.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
0

I've never developed with any of these languages or tools, so I don't really know an elegant solution, but I can provide you with a solution that works. Just create a boolean variable and initialize it to True. Put all of the code for spawning the boss in an if control so it only runs if that variable is set to True. After the boss spawns set the variable to False.

Void Star
  • 2,401
  • 4
  • 32
  • 57