ios - 如何将此游戏代码转换为 Swift 4?

标签 ios swift sprite-kit skspritenode gamekit

我从很久以前就是一名老程序员,但已经有几 (24) 年没有做过任何事情了。 :).情况发生了一些变化。

我发现了一些我正在尝试使用的代码,但这些代码似乎是用早期版本的 swift 编写的。我已尽我所能将其转换,程序编译无误。但是 Sprite 节点根本不动。

最终,当我在屏幕上拖动它时,我试图让 Sprite 节点(玩家)跟随我的触摸。当我停止触摸时停止。

如果有人有不同的方法来实现相同的目标,那也很好。

游戏场景.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))
}
}

代理节点.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)
}
}

最佳答案

最后两个函数不应该是私有(private)的。并更改为代表签名。它现在正在工作。玩得开心。

   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)
}

关于ios - 如何将此游戏代码转换为 Swift 4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433613/

相关文章:

ios - 具有潜在泄漏的Objective-C Zlib方法

ios - 过滤器在 GPUImageBulgeDistortionFilter 中应用不正确

swift - 如何停止屏幕上的节点被触摸? SpriteKit

ios - 在 Alamofire 4.0、iOS 10 和 Swift 3 中使用 Google Places API

ios - 如何让这个动画逆时针旋转?

ios - UIView animateWithDuration 在 ios8 中没有动画

ios - 如何检测子节点是否被触摸 : Sprite Kit

ios - 如何segue UIImage?

ios - 创建一个由变量标识的类

ios - 如何在 SWIFT 中从 SKSpriteNode 创建 CGPath