按住时如何使按钮连续调用函数(SpriteKit) [英] How to make a Button continually call a function when held down (SpriteKit)

查看:21
本文介绍了按住时如何使按钮连续调用函数(SpriteKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用精灵套件制作游戏,我希望我的角色在您按住左/右移动按钮时在屏幕上移动.问题是他只有在点击按钮时才会移动,而不是按住.我到处寻找解决方案,但似乎没有任何效果!

I'm making a game using sprite kit and I want my Character to move across the screen when you hold down the left/right move button. The problem is that he only moves when the button is tapped, not held. I have looked everywhere for a solution but nothing seems to work!

这是我的代码;

class Button: SKNode
{
   var defaultButton: SKSpriteNode // defualt state
   var activeButton: SKSpriteNode  // active state

   var timer = Timer()

   var action: () -> Void

   //default constructor
   init(defaultButtonImage: String, activeButtonImage: String, buttonAction: @escaping () -> Void )
   {
      //get the images for both button states
      defaultButton = SKSpriteNode(imageNamed: defaultButtonImage)
      activeButton = SKSpriteNode(imageNamed: activeButtonImage)

      //hide it while not in use
      activeButton.isHidden = true 
      action = buttonAction

      super.init()

      isUserInteractionEnabled = true

      addChild(defaultButton)
      addChild(activeButton)    
   }

   //When user touches button
   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
   {
      action()

      //using timer to repeatedly call action, doesnt seem to work...
      self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(getter: Button.action), userInfo: nil, repeats: true)

      //swtich the image of our button
      activeButton.isHidden = false
      defaultButton.isHidden = true

   }

   code..........

在我的游戏场景中...

In my game scene...

// *** RIGHT MOVEMENT ***
      let rightMovementbutton = Button(defaultButtonImage: "arrow", activeButtonImage: "arrowActive", buttonAction:
      {

         let moveAction = SKAction.moveBy(x: 15, y: 0, duration: 0.1)
         self.player.run(moveAction)


      })

推荐答案

您知道按钮何时被触摸,因为调用了 touchesBegan.然后你必须设置一个标志来指示按钮被按下.

You know when the button is touched because touchesBegan is called. You then have to set a flag to indicate that the button is pressed.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first!
    if leftButton.containsPoint(touch.locationInNode(self)) {
        leftButtonIsPressed = true
    }
    if rightButton.containsPoint(touch.locationInNode(self)) {
        rightButtonIsPressed = true
    }
}

update() 中,调用标志为 true 的函数:

In update(), call your function that flag is true:

update() {
   if leftButtonIsPressed == true {
        moveLeft()
    }

   if rightButtonIsPressed == true {
        moveRight()
    }

}

当为该按钮调用 touchesEnded 时,您将标志设置为 false:

You set the flag to false when touchesEnded is called for that button:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first!
    if leftButton.containsPoint(touch.locationInNode(self)) {
        leftButtonIsPressed = false
    }
    if rightButton.containsPoint(touch.locationInNode(self)) {
        rightButtonIsPressed = flase
    }
}

正如 KoD 所指出的,一种更简洁的方法(用于移动按钮)是使用 SKAction,它消除了对标志的需要:

As pointed out by KoD, a cleaner way to do this (for movement buttons) is with SKAction which removes the need for the flag:

  1. moveTo x:0moveTo x:frame.width 定义 SKActionsdidMoveTo(查看:)
  2. touchesBegan 中,对正确的对象运行正确的 SKAction为 SKAction 指定一个键.
  3. touchesEnded中,移除相关的SKAction.
  1. Define SKActions for moveTo x:0 and moveTo x:frame.width in didMoveTo(View:)
  2. In touchesBegan, run the correct SKAction on the correct object specifying a key for the SKAction.
  3. In touchesEnded, remove the relevant SKAction.

您必须进行一些数学运算来计算您的对象必须移动多少点,然后根据此距离和移动速度(以每秒点数为单位)设置 SKAction 的持续时间.

You'll have to do some maths to calculate how many points your object will have to move and then set a duration for the SKAction based upon this distance and a movement speed (in points per second).

或者,(感谢 KnightOfDragons)创建一个 SKAction.MoveBy x: 移动一小段距离(根据您所需的移动速度),持续时间为 1/60 秒.当按钮被触摸时永远重复这个动作(SKAction.repeatForever),当按钮被释放时移除重复的SKAction.

Alternatively, (thanks to KnightOfDragons for this) create a SKAction.MoveBy x: which moves a small distance (based upon your desired movement speed) and with a duration of 1/60s. Repeat this action forever (SKAction.repeatForever) when the button is touched and remove the repeating SKAction when the button is released.

这篇关于按住时如何使按钮连续调用函数(SpriteKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆