4

我正在开发一款包含左轮手枪、步枪和霰弹枪等枪支的游戏。您选择一把枪并射击出现在屏幕上的外星人。我差不多完成了,但是我在用自动火枪(例如机关枪)射击外星人时遇到了问题。对于单发枪,我用它来检测外星人何时在十字准线中,如果是,则隐藏它:

CGPoint pos1 = enemyufoR.center;
if ((pos1.x > 254) && (pos1.x < 344) && (pos1.y > 130) && (pos1.y < 165 && _ammoCount != 0))
{
    enemyufoR.hidden = YES;
    [dangerBar setProgress:dangerBar.progress-0.10];
    _killCount = _killCount+3;
    [killCountField setText: [NSString stringWithFormat:@"%d", _killCount]];

    timer = [NSTimer scheduledTimerWithTimeInterval: 4.0
                                             target: self
                                           selector: @selector(showrUfo)
                                           userInfo: nil
                                            repeats: NO];
}

这对大多数枪来说都很好,但对于机枪,我需要它在枪开火时不断检查敌人的位置。我该怎么做?

4

2 回答 2

1

嗯,当你点击你的拍摄按钮时,你显然是在调用类似的东西:

// --------------------------------------
// PSEUDO-CODE
// --------------------------------------
-(void)shootButtonPressed
{
    [self checkEnemies];
}

从那开始,你为什么不直接声明一个 BOOL 变量并使用它来检查手指是否仍然被按下,如下所示:

// --------------------------------------
// PSEUDO-CODE
// --------------------------------------

@interface MyGameClass
{
    // defaults to FALSE
    BOOL isTouching;
}

-(void)shootButtonPressed
{
    // assumes isTouching is a instance variable declared somewhere
    isTouching = YES;
    [self checkEnemies];
}

-(void)checkEnemies
{
    // check enemy action

    // ---------------------------------------------
    // when finger lifts off the screen, this 
    // isTouching variable will be reset to FALSE
    // so as long as isTouching is TRUE, we call
    // this same method checkEnemies again
    // ---------------------------------------------
    if(isTouching)
    {
        [self checkEnemies];
    }
}

// reset the isTouching variable when user finger is taken off the screen
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouching = NO;   
}
于 2012-10-26T07:56:00.297 回答
1

从 UITouch 检查时间戳

于 2012-10-26T05:28:19.257 回答