gpt4 book ai didi

ios - 如何在不拖动 Sprite 的情况下轻拂/滑动 Sprite ?

转载 作者:行者123 更新时间:2023-11-28 06:31:26 24 4
gpt4 key购买 nike

好的,目前在我的游戏中,我可以拖动、轻扫和轻弹节点。但是,我希望用户只能滑动/轻弹节点,而不希望用户能够拖动节点。我该怎么做?

这是我当前的代码:

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

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

if ball.frame.contains(location) {
touchPoint = location
touching = true
}
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as UITouch!
let location = touch!.location(in: self)
touchPoint = location

for touch in touches {
let location = touch.location(in: self)
touchPoint = location
if touching {
if touchPoint != ball.position {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
ball.physicsBody!.velocity=velocity
}
}
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false

for touch in touches {
let location = touch.location(in: self)
touchPoint = location
let node = self.atPoint(location)
}
}

最佳答案

这就是 UIGestureRecognizers 存在的原因。在您的 View 上添加一个 UIPanGestureRecognizer,并使用 velocity(in:UIView) 方法让您知道轻弹的方向和速度。

请注意,您不能在节点上放置手势,只能在 View 上放置手势,因此如果您只使用手势,则需要将 View 坐标转换为场景坐标。

您还可以让手势的触摸渗透到场景中,以允许正常的触摸调用,并且只使用平移手势的速度。

https://developer.apple.com/reference/uikit/uipangesturerecognizer

关于ios - 如何在不拖动 Sprite 的情况下轻拂/滑动 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209170/

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