ios - 如何将此游戏代码转换为 Swift 4?
问题描述
我是一名老程序员,但在几 (24) 年内什么都没做。:)。事情发生了一些变化。
我发现了一些我正在尝试使用的代码,但似乎这些代码是用早期版本的 swift 编写的。我已尽我所能将其转换,并且程序编译时没有错误。但是精灵节点根本不动。
最终,当我在屏幕上拖动它时,我试图让一个精灵节点(播放器)跟随我的触摸。当我停止触摸时停止。
如果某人有不同的方式来实现相同的目标,那也很好。
GameScene.swift
import SpriteKit
import GameplayKit
class GameScene: SKScene {
let trackingAgent = GKAgent2D()
var player = AgentNode()
var seekGoal : GKGoal = GKGoal()
let stopGoal = GKGoal(toReachTargetSpeed: 0)
var seeking : Bool = false {
willSet {
if newValue {
self.player.agent.behavior?.setWeight(1, for: seekGoal)
self.player.agent.behavior?.setWeight(0, for: stopGoal)
}else{
self.player.agent.behavior?.setWeight(0, for: seekGoal)
self.player.agent.behavior?.setWeight(1, for: stopGoal)
}
}
}
var agentSystem = GKComponentSystem()
var lastUpdateTime : TimeInterval = 0
override func didMove(to view: SKView) {
super.didMove(to: view)
self.trackingAgent.position = vector_float2(Float(self.frame.midX), Float(self.frame.midY))
self.agentSystem = GKComponentSystem(componentClass: GKAgent2D.self)
self.player = AgentNode(scene: self, radius: Float(40.0), position: CGPoint(x: self.frame.midX, y: self.frame.midY))
self.player.agent.behavior = GKBehavior()
self.agentSystem.addComponent(self.player.agent)
self.seekGoal = GKGoal(toSeekAgent: self.trackingAgent)
}
override func update(_ currentTime: CFTimeInterval) {
if lastUpdateTime == 0 {
lastUpdateTime = currentTime
}
let delta = currentTime - lastUpdateTime
lastUpdateTime = currentTime
self.agentSystem.update(deltaTime: delta)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.seeking = true
handleTouch(touches: touches)
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
self.seeking = false
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.seeking = false
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
handleTouch(touches: touches)
}
func handleTouch(touches:Set<UITouch>) {
guard let touch = touches.first else {
return
}
let location = touch.location(in: self)
self.trackingAgent.position = vector_float2(Float(location.x), Float(location.y))
}
}
AgentNode.swift
import GameplayKit
import SpriteKit
class AgentNode : SKNode, GKAgentDelegate {
var agent = GKAgent2D()
var triangleShape = SKShapeNode()
override init(){
super.init()
}
init(scene:SKScene, radius:Float, position:CGPoint) {
super.init()
self.position = position
self.zPosition = 10;
scene.addChild(self)
agent.radius = radius
agent.position = vector_float2(Float(position.x), Float(position.y))
agent.delegate = self
agent.maxSpeed = 100 * 4
agent.maxAcceleration = 50 * 4
let ship = SKSpriteNode(imageNamed:"Spaceship")
ship.setScale(1.0 / 8.0)
ship.zRotation = CGFloat(-Double.pi / 2.0)
self.addChild(ship)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func agentWillUpdate(agent: GKAgent) {
}
private func agentDidUpdate(agent: GKAgent) {
guard let agent2D = agent as? GKAgent2D else {
return
}
self.position = CGPoint(x: CGFloat(agent2D.position.x), y: CGFloat(agent2D.position.y))
self.zRotation = CGFloat(agent2D.rotation)
}
}
解决方案
最后两个函数不应该是私有的。并更改为代表签名。它现在正在工作。玩得开心。
func agentWillUpdate(_ agent: GKAgent) {
}
func agentDidUpdate(_ agent: GKAgent) {
guard let agent2D = agent as? GKAgent2D else {
return
}
self.position = CGPoint(x: CGFloat(agent2D.position.x), y: CGFloat(agent2D.position.y))
self.zRotation = CGFloat(agent2D.rotation)
}
推荐阅读
- python - 如何使用 ctypes 和 python3 多次构建导入 DLL?
- shell - 将 YYYYMMDD 转换为 KSH 中的星期几
- python - 将 SQLAlchemy 会话与 HTTPServer 一起使用。线程错误
- ios - 如何创建垂直进度条 - IOS - Swift
- android - 滚动 RecyclerView 时出现“打开的文件太多”
- python - 如何在课程中使用 tkinter PhotoImage?
- momentjs - 使用 moment-timezone 获得跨时区的时差
- c# - SMTP 服务器需要安全连接或客户端未通过身份验证。必须先发出 必须发出 STARTTLS 命令
- python - 找到一个多项式的逆(倒数)模另一个具有有限域系数的多项式
- javascript - 用于选择 CheckBoxFor 生成的所有复选框的复选框