沿着路径的节点跟踪,如在hello world

时间:2017-12-09 13:09:37

标签: swift ipad sprite-kit trail

嗨我有一个节点沿着路径来回走动。我正在尝试将移动的shapeNode集成到hello world示例中以跟踪我的移动节点。我正在使用帧更新的触发器来触发形状节点的复制。并使用行进节点的位置。

问题是这条路有某种偏移,我不知道它的来源。我试图补偿但无济于事。我想知道它是否与这些行动有关。谢谢你的阅读。

我试过看this link ,但我无法翻译

enter image description here

到目前为止,这是我的代码 spriteKit xcode 9 swift 4 iPad Air

//  GameScene.swift


import SpriteKit

    class GameScene: SKScene, SKPhysicsContactDelegate {
    var borderBody = SKPhysicsBody()
    var myMovingNode = SKSpriteNode()
    var trailNode : SKShapeNode?

override func didMove(to view: SKView) {
    //Define Border
    borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
    borderBody.friction = 0
    self.physicsBody = borderBody
    physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
    physicsWorld.contactDelegate = self

    //Define myMovingNode
    myMovingNode = SKSpriteNode(imageNamed: "greenball")
    myMovingNode.size.width = 50
    myMovingNode.size.height = 50
    let myMovingNodeBody:CGSize = CGSize(width: 50, height: 50)
    myMovingNode.physicsBody = SKPhysicsBody.init(rectangleOf: myMovingNodeBody)
    myMovingNode.zPosition = 2
    addChild(myMovingNode)

    //Make Path for myMovingNode to travel
    let myPath = CGMutablePath()
    myPath.move(to: CGPoint(x: 30, y: 30))

    myPath.addLine(to: CGPoint(x: 500, y: 500))

    /*This is another path to try*/
     // myPath.addCurve(to: CGPoint(x: 800, y: 700), control1: CGPoint(x: 500, y: 30), control2: CGPoint(x: 50, y: 500))

    /*This draws a line along myPath*/
    let myLine = SKShapeNode(path: myPath)
    myLine.lineWidth = 10
    myLine.strokeColor = .green
    myLine.glowWidth = 0.5
    myLine.zPosition = 2
    addChild(myLine)

    /*This sets myMovingNode running along myPath*/
    let actionForward = SKAction.follow(myPath, asOffset: false, orientToPath: true, duration: 10)
    let actionReverse = actionForward.reversed()
    let wait = SKAction.wait(forDuration: 1)
    let actionSequence = SKAction.sequence([actionForward, wait, actionReverse, wait])
    myMovingNode.run(SKAction.repeatForever(actionSequence))

    /*This defines TrailNode and its actions*/
    trailNode = SKShapeNode.init(rectOf: CGSize.init(width: 20, height: 20), cornerRadius: 10)
    if let trailNode = trailNode {
    trailNode.lineWidth = 1
    trailNode.fillColor = .cyan
    trailNode.run(SKAction.sequence([SKAction.wait(forDuration: 5),
                  SKAction.fadeOut(withDuration: 3),
                  SKAction.removeFromParent()]))
    }//Eo if Let

}//eo overdrive


func timeFunction (){/*This is controlled by func update*/
    let n = trailNode?.copy() as! SKShapeNode?

    /*this is where i'm trying to compensate*/
    let movingNodeX = myMovingNode.position.x
    let movingNodeY = myMovingNode.position.y
    let movingNodeOffSet = CGPoint(x: movingNodeX - 0, y: movingNodeY - 0)

    n?.position = movingNodeOffSet
    myMovingNode.addChild(n!)
}


var frameCounter = 0
override func update(_ currentTime: TimeInterval) {
    // print( "Called before each frame is rendered")
    if frameCounter == 10{frameCounter = 0; timeFunction()}
    frameCounter = frameCounter + 1
}

}//class GameScene

1 个答案:

答案 0 :(得分:0)

嗨回答我自己的问题。这是最简单的。在最后一行改变了 myMovingNode.addChild(n!)to addChild(n!)