沿着路径的节点轨迹,如 hello world

问题描述 投票:0回答:1

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

问题是这条轨迹有某种偏移,我不知道它从哪里来。我曾尝试过补偿,但无济于事。我想知道这是否与行动有关。

我尝试查看此链接但我无法翻译

这是我到目前为止的代码
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
swift sprite-kit
1个回答
0
投票

在最后一行将

myMovingNode.addChild(n!)
更改为
addChild(n!)

© www.soinside.com 2019 - 2024. All rights reserved.