手指握在屏幕上时连续运行方法

时间:2012-10-26 05:10:16

标签: iphone objective-c ios xcode ipad

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

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

这适用于大多数枪支,但是对于机枪我需要它在枪射击时不断检查敌人的位置。我该怎么做?

2 个答案:

答案 0 :(得分:1)

检查UITouch的时间戳

答案 1 :(得分: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;   
}