如何将一个节点从一个位置移动到另一个位置?

时间:2016-05-02 12:22:04

标签: ios swift sprite-kit

我有一个文件,我从一侧移动一个正方形到靠近屏幕中间的点。

在那之后,我想把它移到另一个位置,但我不确定如何做到这一点。

到目前为止,这是我的代码。当你运行它时,它会移动到一个点然后停止。

我想要它做的是移动到那一点并立即转到具有相同x值但触摸屏幕顶部的y值的点。

override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
    createEnemies()
}

deinit{
    print("deinit called")
}

func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{
    return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
//Helper method for spawning a point along the screen borders. This will not work for diagonal lines.
func randomPointBetween(start:CGPoint, end:CGPoint)->CGPoint{

    return CGPoint(x: randomBetweenNumbers(start.x, secondNum: end.x), y: randomBetweenNumbers(start.y, secondNum: end.y))

}

func createEnemies(){
    //Randomize spawning time.
    //This will create a node every 0.5 +/- 0.1 seconds, means between 0.4 and 0.6 sec
    let wait = SKAction .waitForDuration(0.5, withRange: 0.2)

    weak var  weakSelf = self //Use weakSelf to break a possible strong reference cycle

    let spawn = SKAction.runBlock({

        var random = arc4random() % 4 +  1
        var position = CGPoint()
        var moveTo = CGPoint()

        var offset:CGFloat = 40

        switch random {
            //Left
        case 1:
            position = weakSelf!.randomPointBetween(CGPoint(x: 0, y: weakSelf!.frame.height/2), end: CGPoint(x: 0, y: weakSelf!.frame.height/2))

            //Move to opposite side
            moveTo = CGPoint(x: weakSelf!.frame.width/4, y: weakSelf!.frame.height/2)
            //moveTo2 = CGPoint(x: weakSelf!.frame.width/4, y: weakSelf!.frame.height)

            break            

        default:
            break

        }

        weakSelf!.spawnEnemyAtPosition(position, moveTo: moveTo)

    })

    let spawning = SKAction.sequence([wait,spawn])

    self.runAction(SKAction.repeatActionForever(spawning), withKey:"spawning")


}


func spawnEnemyAtPosition(position:CGPoint, moveTo:CGPoint){

    let enemy = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 40, height: 40))


    enemy.position = position
    enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
    enemy.physicsBody?.affectedByGravity = false
    enemy.physicsBody?.dynamic = true
    enemy.physicsBody?.collisionBitMask = 0 // no collisions
    //Here you can randomize the value of duration parameter to change the speed of a node
    let move = SKAction.moveTo(moveTo,duration: 2.5)
    //let remove = SKAction.removeFromParent()
    enemy.runAction(SKAction.sequence([move]))
    /*if enemy.position = CGPoint(x: weakSelf!.frame.width/4, y: weakSelf!.frame.height/2) {
        let move2 = SKAction.moveTo(moveTo2,duration: 2.5)
        enemy.runAction(SKAction.sequence([move2]))
    }*/




    self.addChild(enemy)
    print("\(enemy.position)")

}


func didBeginContact(contact: SKPhysicsContact) {


}


/*

Added for debugging purposes

override func touchesBegan(touches: NSSet, withEvent event: UIEvent?) {

//Just make a transition to the other scene, in order to check if deinit is called
//You have to make a new scene ... I named it WelcomeScene
var scene:WelcomeScene = WelcomeScene(fileNamed: "WelcomeScene.sks")

scene.scaleMode = .AspectFill

self.view?.presentScene(scene )


}
*/

1 个答案:

答案 0 :(得分:1)

例如运行一个序列。

moveTo = CGPoint(x: weakSelf!.frame.width/4, y: weakSelf!.frame.height/2)
moveTo2 = CGPoint(x: weakSelf!.frame.width/4, y: weakSelf!.frame.height)

let move = SKAction.moveTo(moveTo,duration: 2.5)
let move2 = SKAction.moveTo(moveTo2,duration: 2.5)

let moveToSequence = SKAction.sequence([move, move2])

enemy.runAction(moveToSequence)