1

I have a file where I move a square from one side to a point near the middle of the screen.

After that, I want to move it to another position, but I am unsure of how to do that.

This is my code so far. When you run it, it moves to one point and then stops.

What I want it to do is it moves to that one point and immediately goes to a point with the same x value but a y value touching the top of the screen.

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 )


}
*/
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Aurthur M.
  • 13
  • 3
  • 3
    http://stackoverflow.com/questions/36684255/how-to-move-node-in-the-direction-of-another-node?rq=1 –  May 02 '16 at 12:27

1 Answers1

1

Run a sequence for example.

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)
aignetti
  • 481
  • 1
  • 3
  • 14