gpt4 book ai didi

ios - SKSpriteKitNode 的旋转与 GKAgent2D 的旋转不匹配

转载 作者:行者123 更新时间:2023-12-01 22:22:47 45 4
gpt4 key购买 nike

您好,我正在尝试了解基本的游戏套件。但似乎在将我的代理的旋转与它应该作用的 spritekitnode 的旋转匹配时遇到了麻烦。我正在使用iOS中新游戏模板附带的宇宙飞船。我正试图在宇宙飞船上引发一种基本的漫游行为。飞船确实在游荡,但看起来飞船正在做这种奇怪的侧踏舞,因为它的旋转与特工的旋转不匹配。代码如下。

class Entity : GKEntity {

let sprite: SKSpriteNode

override init() {
let texture = SKTexture(imageNamed: "Spaceship")
sprite = SKSpriteNode(texture: texture, color: UIColor.white, size: texture.size())
sprite.position = CGPoint(x: 0, y: 0)
sprite.zRotation = CGFloat(-.pi / 2.0)
sprite.zPosition = 10
sprite.setScale(1.0 / 8.0)
super.init()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

class GameScene: SKScene , GKAgentDelegate {

var entities = [GKEntity]()
var graphs = [String : GKGraph]()
var anEntity = Entity()

private var lastUpdateTime : TimeInterval = 0

override func didMove(to view: SKView) {

self.lastUpdateTime = 0

let wanderGoal = GKGoal(toWander: 10)
let behavior = GKBehavior(goal: wanderGoal, weight: 100)
let agent = GKAgent2D()
agent.radius = 40.0
agent.position = vector_float2(Float(anEntity.sprite.position.x), Float(anEntity.sprite.position.y))
agent.rotation = Float(-.pi / 2.0)
print (agent.rotation)
agent.delegate = self
agent.maxSpeed = 100
agent.maxAcceleration = 50
agent.behavior = behavior
anEntity.addComponent(agent)
addChild(anEntity.sprite)
entities.append(anEntity)

}




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

}

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

}

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

}

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

}


override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered

// Initialize _lastUpdateTime if it has not already been
if (self.lastUpdateTime == 0) {
self.lastUpdateTime = currentTime
}

// Calculate time since last update
let dt = currentTime - self.lastUpdateTime

// Update entities
for entity in self.entities {
entity.update(deltaTime: dt)
}

self.lastUpdateTime = currentTime
}

func agentWillUpdate(_ agent: GKAgent) {

}

func agentDidUpdate(_ agent: GKAgent) {

guard let agent2d = agent as? GKAgent2D else {
return
}

anEntity.sprite.position = CGPoint(x: CGFloat(agent2d.position.x), y: CGFloat(agent2d.position.y))
//print (position)

anEntity.sprite.zRotation = CGFloat(agent2d.rotation)
}
}

最佳答案

您可以将 Sprite (png)的位图向右旋转 90 度。或者你可以写在 func agentDidUpdate(_ agent: GKAgent)

anEntity.sprite.zRotation = CGFloat(agent2d.rotation) - 0.5 * CGFloat.pi

关于ios - SKSpriteKitNode 的旋转与 GKAgent2D 的旋转不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43869363/

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