如何在向左或向右移动时水平翻转精灵?

发布于 2025-01-19 05:24:02 字数 509 浏览 1 评论 0原文

在我的 SpriteKit 项目中,我有一艘船,我可以左右拖动来捕捉从天上掉下来的物品,我希望船能够朝它移动的方向看,我想我必须将 xScale 从 CGFloat(1) 更改为 CGFloat( -1)但我不知道如何,任何建议都表示赞赏,这就是我处理船运动的方式:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let location = touch?.location(in: self){
            node.run(SKAction.move(to: CGPoint(x:  nodePosition.x + location.x - startTouch.x, y: nodePosition.y + location.y - startTouch.y), duration: 0.1))
        }
    }

In my SpriteKit project I have a boat that I drag left and right to catch items falling from the sky, I would like the boat to look in the direction it's moving, I think I have to change xScale from CGFloat(1) to CGFloat(-1) but I don't know how, any suggestion is appreciated, this is how I handle the boat movement:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let location = touch?.location(in: self){
            node.run(SKAction.move(to: CGPoint(x:  nodePosition.x + location.x - startTouch.x, y: nodePosition.y + location.y - startTouch.y), duration: 0.1))
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2025-01-26 05:24:02

内部touchesmaid您可以使用touch.location.locationtouch.previouslocation确定x位置的更改。然后,如果Delta X为正或负数,则可以相应地翻转XScale。

//grab current and previous locations
let touchLoc = touch.location(in: self)
let prevTouchLoc = touch.previousLocation(in: self)

//get deltas and update node position
let deltaX = touchLoc.x - prevTouchLoc.x
let deltaY = touchLoc.y - prevTouchLoc.y
node.position.x += deltaX
node.position.y += deltaY

//set x flip based on delta
node.xScale = deltaX < 0 ? 1 : -1

inside touchesMoved you can use touch.location and touch.previousLocation to determine a change in the x position. then if the delta x is positive or negative you can flip your xScale accordingly.

//grab current and previous locations
let touchLoc = touch.location(in: self)
let prevTouchLoc = touch.previousLocation(in: self)

//get deltas and update node position
let deltaX = touchLoc.x - prevTouchLoc.x
let deltaY = touchLoc.y - prevTouchLoc.y
node.position.x += deltaX
node.position.y += deltaY

//set x flip based on delta
node.xScale = deltaX < 0 ? 1 : -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文