gpt4 book ai didi

ios - 在 spritekit 游戏中获取屏幕的触摸位置

转载 作者:行者123 更新时间:2023-11-28 11:58:04 27 4
gpt4 key购买 nike

我有一个 sprite kit 游戏,我想知道用户是否触摸了屏幕的左侧、右侧或中间 (25/50/25)

当我触摸屏幕的最左侧时,它说我触摸了 x 轴上的 -450,而它应该为 0。我假设它获得了我相对于场景的触摸位置,并且当 anchor 开始时向右 450 像素,当我触摸 0 时给我 -450。

因为这是一个横向滚动条,移动 anchor 不起作用,我需要屏幕的触摸位置:

override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?){
var touchLeft : Bool = false
var touchRight : Bool = false
var touchMiddle : Bool = false

for touch in (touches) {
let location = touch.location(in: self)

if(location.x < self.size.width/4){
touchLeft = true
print("Left")
} else if(location.x > ((self.size.width/4) * 3)){
touchRight = true
print("Right")
} else {
touchMiddle = true
print("Middle")
}
}
}

最佳答案

您几乎已经成功了,只是将负数考虑在内。

如果您不知道,默认情况下 0 是 SKScene 的中心。这是因为默认 anchor 是0.5,0.5。

由于您使用相机来处理滚动,因此您想要使用touch.location(in: self.camera) 这样您就可以始终触摸相对于相机所在的位置,而不是场景所在的位置。

所以只需将您的代码更改为如下:

override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?){
var touchLeft : Bool = false
var touchRight : Bool = false
var touchMiddle : Bool = false

for touch in (touches) {
let location = touch.location(in: self.camera)

if(location.x < -self.size.width/4){
touchLeft = true
print("Left")
} else if(location.x > ((self.size.width/4))){
touchRight = true
print("Right")
} else { //x is between -width / 4 and width / 4
touchMiddle = true
print("Middle")
}
}
}

关于ios - 在 spritekit 游戏中获取屏幕的触摸位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50528616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com