Ios 精灵套件:检测左侧和右侧;右击
在我正在尝试构建的新游戏中,我想知道用户何时触摸了屏幕的左右两侧,以执行一些逻辑操作 我的代码:Ios 精灵套件:检测左侧和右侧;右击,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,在我正在尝试构建的新游戏中,我想知道用户何时触摸了屏幕的左右两侧,以执行一些逻辑操作 我的代码: override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { /* Called when a touch begins */ for touch in (touches as! Set<UITouch>) { let location = tou
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
var isRight : Bool = false
var isLeft : Bool = false
if(location.x < self.size.width/2){
isLeft = true
println("Left")
}
if(location.x > self.size.width/2){
//
isRight = true
println("Right")
}
if (isRight && isLeft){
println("Both touched")
// do something..
}
}
}
我的代码似乎不起作用。我做错了什么?有谁有更好的解决方案吗?这样做
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
var isRight : Bool = false
var isLeft : Bool = false
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if(location.x < self.size.width/2){
isLeft = true
println("Left")
}
if(location.x > self.size.width/2){
//
isRight = true
println("Right")
}
}
if (isRight && isLeft){
println("Both touched")
// do something..
}
}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
/*当触摸开始时调用*/
var isRight:Bool=false
var isLeft:Bool=false
用于触摸输入(触摸设置为){
let location=touch.locationInNode(自)
if(位置x<自身尺寸宽度/2){
isLeft=true
println(“左”)
}
if(location.x>self.size.width/2){
//
isRight=true
println(“右”)
}
}
如果(isRight和isLeft){
println(“两个都被触摸”)
//做点什么。。
}
}
更新:Swift 2.2
对于SKView:
class skView:SKView {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
var isRight : Bool = false
var isLeft : Bool = false
for touch in touches {
let location = touch.locationInView(self);
if(location.x < self.frame.size.width/2){
isLeft = true
print("Left")
}
if(location.x > self.frame.size.width/2){
//
isRight = true
print("Right")
}
}
if (isRight && isLeft){
print("Both touched")
// do something..
}
}
}
class-skView:skView{
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
var isRight:Bool=false
var isLeft:Bool=false
接触{
让位置=touch.locationInView(self);
if(位置x<自身框架尺寸宽度/2){
isLeft=true
打印(“左”)
}
if(location.x>self.frame.size.width/2){
//
isRight=true
打印(“右”)
}
}
如果(isRight和isLeft){
打印(“两次触摸”)
//做点什么。。
}
}
}
对于SKNode:
class skNode: SKNode {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
var isRight : Bool = false
var isLeft : Bool = false
for touch in touches {
let location = touch.locationInNode(self);
if(location.x < self.frame.size.width/2){
isLeft = true
print("Left")
}
if(location.x > self.frame.size.width/2){
//
isRight = true
print("Right")
}
}
if (isRight && isLeft){
print("Both touched")
// do something..
}
}
}
类skNode:skNode{
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
var isRight:Bool=false
var isLeft:Bool=false
接触{
让位置=touch.locationInNode(自身);
if(位置x<自身框架尺寸宽度/2){
isLeft=true
打印(“左”)
}
if(location.x>self.frame.size.width/2){
//
isRight=true
打印(“右”)
}
}
如果(isRight和isLeft){
打印(“两次触摸”)
//做点什么。。
}
}
}
此代码位于何处;什么是“自我”?
class skNode: SKNode {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
var isRight : Bool = false
var isLeft : Bool = false
for touch in touches {
let location = touch.locationInNode(self);
if(location.x < self.frame.size.width/2){
isLeft = true
print("Left")
}
if(location.x > self.frame.size.width/2){
//
isRight = true
print("Right")
}
}
if (isRight && isLeft){
print("Both touched")
// do something..
}
}
}