2012-07-05 137 views
-2

好吧,我知道这应该很简单,但我有一个巨大的放屁,无法弄清楚这一切,我想要的是一次产卵的敌人。现在它每隔180秒产卵一次,我希望它在180秒时只产卵一次。只让敌人重生一次cocos2d?

  [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]; 
} 
    } 
+3

我们没有什么可以离开这里的。从你的问题和那个微小的代码示例中,我无法判断这里发生了什么。你能给我们更多的代码和更好的解释你的问题吗? – 2012-07-05 19:34:56

+0

对不起,我编辑了代码。 – 2012-07-05 20:00:53

+0

也许用NSTimer看看[this](http://stackoverflow.com/questions/5674375/calling-a-method-after-each-60-seconds-in-iphone)?另外,请检查代码的缩进和空白 - 现在很难阅读。 – thegrinner 2012-07-05 20:03:23

回答

2

在这里的一个肢体。这段代码将有gameLogicBoss方法执行每180秒:

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

如果您希望这仅发生一次,你必须取消预定时执行方法的选择:

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

    // rest of the code here … 
} 

_cmd是当前方法的选择器的简写。您当然也可以使用@selector(...)从其他方法中取消选择选择器。

Cocos2D 2.0还有一个名为scheduleOnce的方法,它只会调用选择器一次,而不必手动对其进行调度。

+1

谢谢你。如果我可以从你的监视器中弹出来,我会让你着迷的! – 2012-07-05 20:24:30

0

我从来没有与任何一种语言或工具的开发,所以我真的不知道一个优雅的解决方案,但我可以为您提供一个可行的解决方案。只需创建一个布尔变量并将其初始化为True。将所有用于产生boss的代码放在if控件中,以便只在该变量设置为True时才运行。在boss产生后,将变量设置为False。