Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当同时按下两个按钮时,如何执行操作?_Ios_Sprite Kit_Game Engine - Fatal编程技术网

Ios 当同时按下两个按钮时,如何执行操作?

Ios 当同时按下两个按钮时,如何执行操作?,ios,sprite-kit,game-engine,Ios,Sprite Kit,Game Engine,每当我同时按下两个按钮时,我希望我的角色跳转。我已经试过了: if rightButton.contains(location) && leftButton.contains(location) { character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50)) } 一种办法是: 在检测与按钮交互的函数中,使用布尔值进行准备 然后在更新功能中,使用计时器添加一个时间范围,我们可以说两个按钮同时按下(例如100毫

每当我同时按下两个按钮时,我希望我的角色跳转。我已经试过了:

if rightButton.contains(location) && leftButton.contains(location) {
    character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}
一种办法是:

  • 在检测与按钮交互的函数中,使用布尔值进行准备
  • 然后在更新功能中,使用计时器添加一个时间范围,我们可以说两个按钮同时按下(例如100毫秒)
我会给你一些伪代码,希望对你有所帮助

    func RightBtnClick()->Void{
        rightBtnPressed = true
    }
    func LeftBtnClick()->Void{
        leftBtnPressed = true
    }

    func Start()->Void{
        rightBtnTimer = 0
        leftBtnTimer = 0
    }

    func Update(deltatime ms:float)->Void{
        if(rightBtnPressed){
            rightBtnTimer += ms;
            if(rightBtnTimer>100){
                rightBtnTimer = 0
                rightBtnPressed=false
            }
        }

        if(leftBtnPressed){
            leftBtnTimer += ms;
            if(leftBtnTimer>100){
                leftBtnTimer = 0
                leftBtnPressed=false
            }
        }

    // Lastly let's check if both are pressed.
        if(leftBtnPressed && rightBtnPressed){
            DoStuff()
        }
    }

首先,确保在
GameViewController.swift
中启用了多点触摸

class GameViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // ...

        if let view = self.view as! SKView?
        {
            // ...

            view.isMultipleTouchEnabled = true
        }
    }
}
游戏场景中
为按钮命名。点击后,我们将创建一个列表,列出您手指触摸到的每个节点的名称。如果列表中同时包含右键和左键,则表示他同时按下了这两个按钮

class GameScene: SKScene
{
    override func didMove(to view: SKView)
    {
        // add name to each button

        left_button.name  = "left_button"
        right_button.name = "right_button"
    }

    func buttons_touched(_ touches: Set<UITouch>) -> [String]
    {
        // get the list of buttons we touched on each tap

        var button_list : [String] = []

        for touch in touches
        {
            let positionInScene = touch.location(in: self)
            let touchedNode = self.nodes(at: positionInScene)

            let buttons = touchedNode.compactMap { (node) -> String in
                node.name ?? ""
            }.filter({$0 != ""})

            button_list += buttons
        }

        return button_list
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
    {
        let buttons_tapped = buttons_touched(touches)

        if buttons_tapped.contains("right_button") && buttons_tapped.contains("left_button")
        {
            // jump code
        }
    }
}
class游戏场景:SKScene
{
覆盖func didMove(到视图:SKView)
{
//为每个按钮添加名称
left\u button.name=“left\u button”
right\u button.name=“right\u button”
}
触摸功能按钮(触摸:设置)->[字符串]
{
//获取我们在每次点击时触摸的按钮列表
变量按钮列表:[字符串]=[]
接触
{
let positionInScene=触摸位置(in:self)
让touchedNode=self.nodes(在:位置InScene)
让buttons=touchedNode.compactMap{(节点)->输入字符串
node.name??“”
}.filter({$0!=“”})
按钮列表+=按钮
}
返回按钮列表
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?)
{
让按钮点击=按钮点击(触摸)
如果按钮包含(“右按钮”)和按钮包含(“左按钮”)
{
//跳转码
}
}
}

通过按住
选项
按钮,您可以在
模拟器
内模拟多点触控。

我会避免在更新函数内进行检查,您希望尽可能将其最小化。@Knight0fDragon您是对的,我会在PreUpdate函数中创建一个只处理此类交互的函数,以获得更可读的代码