首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >旋转过程中改变SKSpriteNode的角度

旋转过程中改变SKSpriteNode的角度
EN

Stack Overflow用户
提问于 2022-05-16 21:16:13
回答 1查看 43关注 0票数 2

我在试着玩漂流游戏。为了使汽车漂移,汽车(转弯时)需要有一个角度。我已经尝试了旋转,但是这与我已经拥有的代码相冲突,我已经转向汽车。这是我的密码,有什么帮助吗?

代码语言:javascript
运行
复制
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first {
                let location = touch.previousLocation(in: self)
                let position = touch.location(in: self)
                let node = self.nodes(at: location).first

if position.x < 0 {
                    let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 0.8))
                    car.run(rotate, withKey: "rotating")
         
            } else {
                    let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(-M_PI), duration: 0.8))
                    car.run(rotate, withKey: "rotating")
                
               
                
                }
            
        }
            
    }





    override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    car.position = CGPoint(x:car.position.x + cos(car.zRotation) * 3.0,y:car.position.y + sin(car.zRotation) * 3.0)

    }

}

此代码目前没有增加任何角度,或“漂移”效果的汽车旋转。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-31 19:02:56

一种方法是,您可以将您的车嵌套在另一个SKNode中作为一个容器。将您的转向旋转应用于汽车节点,就像您现在所做的那样。然后将漂移旋转应用于容器节点。其结果将是两者之和。

代码语言:javascript
运行
复制
//embed car inside a SKNode container so you can apply different rotations to each
let car = SKShapeNode(ellipseOf: CGSize(width: 20, height: 40)) //change to how you draw your car
let drift_container = SKNode()
drift_container.addChild(car) //embed car inside the container
self.addChild(drift_container) //`self` here is a SKScene

//apply angle rotation to the container
func drift(byAngle angle:CGFloat) {
    let rotate = SKAction.rotate(toAngle: angle, duration: 0.2)
    drift_container.run(rotate)
}

然后在update中,确保更新drift_container.position而不是汽车的位置

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72265723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档