【问题标题】:How to implement touch and hold for sprite kit?如何实现精灵套件的触摸和保持?
【发布时间】:2014-02-12 02:46:55
【问题描述】:

我最近开始使用sprite-kit。我知道touchesBegan 只需轻按一下即可,但我可以使用什么东西来识别按住的触摸吗?

【问题讨论】:

    标签: ios iphone ipad sprite-kit


    【解决方案1】:

    如果你想实现射击之类的东西,那么你需要在touchesBegan方法中开始射击并在touchesEnded方法中停止射击:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self startShooting];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [self stopShooting];
    }
    

    对于其他用途,您可以将UILongPressGestureRecognizer 添加到SKScene

    【讨论】:

    • 谢谢安德烈,还有一件事。我正在开发的游戏让你点击屏幕让角色向上移动,重力让它回到下方。最终会有物体从你必须尽量避免的一侧进入。现在,当我点击屏幕时,我的角色会做我想做的事情,即向上移动 200 点,但如果我在它回到底部之前再次点击,它不会再上升 200 点,因为似乎重力有已经开始了。有没有办法在点击屏幕后重置影响角色的重力?
    • 我看不懂你,你能发布你的代码吗?为此创建另一个问题
    【解决方案2】:

    您也可以在更新方法中使用布尔值:

    bool isTouching = false;
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        isTouching = true;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        isTouching = false;
    }
    
    -(void)update:(CFTimeInterval)currentTime {
        if(isTouching){
            //shooting! 
        }
    }
    

    如果您愿意,您可以轻松地将您的方法组合到 isTouching 块中,并使用 touchesBegan 来同时瞄准子弹。

    您无需担心计时器,因为只要 isTouching == true,update 方法就会继续执行代码块

    【讨论】:

      【解决方案3】:
      var isTouching = false
      
      override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
          handleTouches(touches) 
          isTouching = true;
      }
      
      override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
          handleTouches(touches)
          isTouching = false;
      }
      
      override func update(currentTime: NSTimeInterval) {
          if isTouching{
              //Shoot CODE!
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多