Skip to main content
 首页 » 操作系统

Swift Sprite 套件游戏 : detect touch and execute separate code for different parts of screen

2024年01月01日32insus

本质上,我需要知道如果用户触摸了屏幕左侧,则执行此代码,但如果他们触摸了屏幕右侧,则执行此代码。

事实上,我有在屏幕的任何地方被点击时执行的代码,因为我有它:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
        /* Called when a touch begins */ 
 
        for touch: AnyObject in touches { 
 
            let location = touch.locationInNode(self) 
} 
} 

但我需要知道如何使水龙头的位置更具体,即右侧或左侧。有没有办法通过更改代码来做到这一点,还是我必须使用按钮?我该怎么做?

请您参考如下方法:

你可以这样写一个条件

let location = touch.locationInNode(self) 
if location.x < self.size.width/2 { 
    // left code 
} 
else { 
   // right code 
}