0

嗨,我有一个节点沿着一条路径来回移动。我正在尝试像在 hello world 示例中那样集成移动 shapeNode 来跟踪我的移动节点。我正在使用来自帧更新的触发器来触发形状节点的复制。并使用移动节点的位置。

问题是路径有某种偏移,我不知道它来自哪里。我曾试图弥补,但无济于事。我想知道它是否与这些行为有关。谢谢阅读。

我试过查看这个链接,但我无法翻译

在此处输入图像描述

到目前为止,这是我的代码
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
4

1 回答 1

0

嗨,回答我自己的问题。这是最简单的。在最后一行从
myMovingNode.addChild(n!) 更改为 addChild(n!)

于 2017-12-09T18:46:58.497 回答