【问题标题】:Making the enemy spawn only once cocos2d? [closed]让敌人只产生一次cocos2d? [关闭]
【发布时间】:2012-07-05 19:18:57
【问题描述】:

好的,我知道这应该很简单,但我脑子里放了一个大屁,我想不通我想要的只是敌人产卵一次。现在它每 180 秒产生一次,我希望它在 180 秒标记处只产生一次。

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

【问题讨论】:

  • 我们没有什么可以离开这里的。从您的问题和那个小代码示例中,我根本无法判断这里发生了什么。你能给我们更多的代码和更好的解释你的问题吗?
  • 对不起,我编辑了代码。
  • 也许用 NSTimer 看看类似this 的东西?另外,请检查代码的缩进和空格 - 很难按原样阅读。
  • 试过了,根本没用。仍然每 180 秒生成一次,而不是只生成一次。

标签: iphone ios xcode4 cocos2d-iphone


【解决方案1】:

在这里走弯路。这段代码将让 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 的方法,它只调用一次选择器,而无需手动取消调度。

【讨论】:

  • 谢谢你,天哪。如果我能跳出你的显示器,我会拥抱你!!!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多