iosswiftsprite-kitskaction

SKAction.moveTo not working at all


I'm facing a strange problem with Sprite-kit and Swift 1.2. I have the following code. The fingerSprite appears correctly in the middle of the screen.

When I apply the moveTo action however, nothing happens. I'm quite new to Sprite-Kit and Swift so I might be doing something totally stupid but please bear with me.

What am I doing wrong?

   override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
tutorialStepReset()

var bounceLabels = SKAction.sequence([
    SKAction.scaleTo(1.3, duration: 0.5),
    SKAction.scaleTo(1.0, duration: 0.5)
    ])

var fingerPoint = CGPoint(x: self.size.width/2, y: self.size.height/2)
var fingerSprite = SKSpriteNode(imageNamed: "finger.png")

switch tutorialGuideIndex {
    case 0: self.runAction( SKAction.runBlock({
        self.labelScore.runAction(SKAction.repeatActionForever(bounceLabels))

    }))
    case 1: self.runAction( SKAction.runBlock({
        self.labelUntilNextLevel.runAction(SKAction.repeatActionForever(bounceLabels))

    }))
    case 2: self.runAction( SKAction.runBlock({
        self.labelCurrentLevel.runAction(SKAction.repeatActionForever(bounceLabels))

    }))
    case 3:


        fingerSprite.position = fingerPoint
        fingerSprite.zPosition = labelZPosition
        self.addChild(fingerSprite)
    case 4:
        //move finger & draw line
        fingerPoint.x = fingerPoint.x+20
        fingerPoint.y = fingerPoint.y+20
        fingerSprite.runAction(SKAction.scaleTo(2.0, duration: 4.0))
        fingerSprite.runAction(SKAction.moveTo(fingerPoint, duration: 4.0))


   tutorialGuideIndex++

}

Solution

  • On each touch you create a new sprite and only in case 3 you add it to the node without running the move action.

    When reaching case 4 you just run the action on the new sprite instance without adding it to the node so it will never be shown.

    Use the move action right after adding the sprite to the node

       case 3:       
            fingerSprite.position = fingerPoint
            fingerSprite.zPosition = labelZPosition
            self.addChild(fingerSprite)
    
            fingerSprite.runAction(SKAction.moveTo(fingerPoint, duration: 4.0))
    

    I also notice you try to run two actions on the same sprite:

        fingerSprite.runAction(SKAction.scaleTo(2.0, duration: 4.0))
        fingerSprite.runAction(SKAction.moveTo(fingerPoint, duration: 4.0))
    

    in order to run actions one after another you should you the sequence action:

    fingerSprite.runAction(SKAction.sequence([
       SKAction.scaleTo(2.0, duration: 4.0),
       SKAction.moveTo(fingerPoint, duration: 4.0)]));
    

    if you need these actions to run in parallel use the group action instead of sequence