2017-12-09 15 views
0

안녕하세요, 경로를 따라 앞뒤로 이동하는 노드가 있습니다. 안녕 움직이는 노드를 흔드는 hello world 예제와 같이 moving shapeNode를 통합하려고합니다. 셰이프 노드의 복사를 트리거하기 위해 프레임 업데이트의 트리거를 사용하고 있습니다. 이동 노드의 위치를 ​​사용합니다.hello world와 같은 경로를 따르는 노드 흔적

문제는 트레일에 일종의 오프셋이 있으며 어디에서 오는 지 알 수 없다는 것입니다. 나는 보상하려했지만 아무 소용이 없다. 나는 그것이 그 행동들과 관련이 있을지 궁금해. 읽어 주셔서 감사합니다.

내가 this link보고 시도하지만 난 여기에 내 코드 지금까지
spriteKit의 엑스 코드 (9) 신속가

enter image description here

을 변환 할 수 없습니다 4 아이 패드 에어

// 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 

답변

0

안녕하세요. 내 질문에. 그것은 가장 단순했습니다. 마지막 줄에서
myMovingNode.addChild (n!)에서 addChild (n!)

까지 변경됩니다.