【问题标题】:How do I limit the touch input?如何限制触摸输入?
【发布时间】:2014-10-17 00:39:40
【问题描述】:

我正在构建一个 Sprite Kit 游戏,玩家在按下屏幕时会发射一个粒子。如何限制触摸输入(假设每秒 2 个可识别的触摸输入),以使玩家不能只是快速点击屏幕来获得无限次射击?

【问题讨论】:

    标签: ios objective-c touch sprite-kit particles


    【解决方案1】:

    另一种解决方案: 创建一个 BOOL(我更喜欢自己使用属性,所以):

    @property (nonatomic, assign) BOOL touchEnabled;

    在场景的初始化中将其设置为 YES。那么从此就相当简单了:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         if (self.touchEnabled){
             self.touchEnabled = NO;
             [self shootParticle];
             [self performSelector:@selector(enableTouch) withObject:nil afterDelay:2.0];
         }
    ...
    
    - (void)shootParticle{
       // whatever...
    }
    
    - (void)enableTouch{
       self.touchEnabled = YES;
    }
    

    【讨论】:

    • 请注意,在 SpriteKit 中,如果可以,请避免使用“performSelector”。它不会成为 SK 更新循环的一部分(使用 SKAction)。如果您暂停游戏,它将触发。与 SK 一起使用 NSTimer(或 GCD)的原因与此相同。
    【解决方案2】:

    多种可能性之一:

    @interface YourSceneName (){
        int _amountBullets; //increase every time you shot and just shoot when _fireStop = NO 
        BOOL _fireStop; // init as NO at start
        BOOL _needStartTime; // init as YES at start
        CFTimeInterval _startTime;
    }
    @end
    
    -(void)update:(CFTimeInterval)currentTime {
    
    //set starttime
        if(_needStartTime){
            _startTime = currentTime;
            _needStartTime = NO;
        } 
    
    //timeinterval if 2 seconds, renew everything
        if(currentTime - _startTime > 2){
            _startTime = currentTime;
            _amountBullets = 0
            _fireStop = NO;
        }
    //set firestop to yes, method should be executed
        if(_amountBullets = 2){
            _fireStop = YES;
        } 
    }
    

    我是 SpriteKit 的新手,但这应该可以。我敢打赌还有更好的可能性。我也没有测试代码。它向您展示了如何做到这一点的逻辑,希望我能提供帮助。 这是一个很棒的Tutorial,用于在 SpriteKit 中处理时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2017-01-03
      • 1970-01-01
      相关资源
      最近更新 更多