你好,我正在尝试了解基本的游戏工具包。但似乎在将我的代理的轮换与它应该操作的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)
}
}发布于 2020-01-28 23:54:28
您可以将sprite ( png)的位图向右旋转90度。或者您可以在函数代理(_agentDidUpdate: GKAgent)中编写
anEntity.sprite.zRotation = CGFloat(agent2d.rotation) - 0.5 * CGFloat.pihttps://stackoverflow.com/questions/43869363
复制相似问题