我目前是在一个侧面游戏中工作,在这个游戏中,跳高取决于玩家按下屏幕右半部分的时间。所有操作都很好,除非用户快速触摸屏幕。这使得跳跃尽可能大。
我是不是做错了什么,还是SpriteKit的工作方式出了问题?我该如何解决这个问题?
编辑:以下是我游戏中处理触摸的所有方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{
swiped = false
let location = touch.location(in: cameraNode)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.065)
{
if self.swiped == false
{
if location.x < 0
{
self.changeColor()
}
else
{
self.jump()
}
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches {
let location = touch.location(in: cameraNode)
if location.x > 0
{
// Right
thePlayer.endJump()
}
}
}此外,还有一个手势识别器,它负责左右滑动,有以下处理程序:
@objc func swipedRight()
{
if walkstate != .walkingRight
{
walkstate = .walkingRight
}
else
{
boost(direction: 0)
}
swiped = true
}
@objc func swipedLeft()
{
if walkstate != .walkingLeft
{
walkstate = .walkingLeft
}
else
{
boost(direction: 1)
}
swiped = true
}希望这足以描述这些问题。上面的代码是我为处理触摸所做的一切。
发布于 2018-02-03 22:48:07
问题是,我正在使用DispatchQueue命令在短时间延迟后调用跳转方法,以防用户滑动而不是点击。因此,touchesEnded方法在跳转开始之前就会被调用,因此不能再停止。
为了解决这个问题,我添加了一个布尔变量,当玩家触摸屏幕时,这个变量被设置为true,并在用户手指离开屏幕时设置为false。为了跳转,必须将该变量设置为true,因此字符将不再在快速触摸后跳转。
https://stackoverflow.com/questions/48489371
复制相似问题