0

I have created a Test Scene to practice some basic Swift 3 and SpriteKit. I'm trying to learn by understanding the basics before moving on to more complex goals.

Here I have a SKLabelNode that is created and then moves to the left. I have created a sequence to repeat the action but it does not work. Please could you help me understand where it fails. NodeCount notes that there is only 1 node.

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var testShape = SKLabelNode()

override func didMove(to view: SKView) {

    func createShape() {

        testShape = SKLabelNode(text: "TEST")
        testShape.position = CGPoint(x: 0.5, y: 0.5)
        testShape.zPosition = 1
        addChild(testShape)

    }

    let moveTestShape = SKAction.moveBy(x: -500, y: 0, duration: 5)

    func repeater() {

        createShape()

        testShape.run(moveTestShape)

    }

    let delay = SKAction.wait(forDuration: 2)

    let repeatingAction = SKAction( repeater() )

    let sequence = SKAction.sequence([ delay, repeatingAction ] )

    run(SKAction.repeatForever(sequence))

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func update(_ currentTime: TimeInterval) {

}
}
Martin Jones
  • 123
  • 7
  • How is this even compiling? You should be getting yelled at here: ` let repeatingAction = SKAction( repeater() )` Anyway, You would want to use `SKAction.run(repeater)` since your function is an internal function. – Knight0fDragon Oct 21 '16 at 14:32

1 Answers1

1

Are you not getting compiler errors?

Why are you creating methods in didMoveToView?

Your code should look more like this

 class GameScene: SKScene {

       var testShape = SKLabelNode()

       override func didMove(to view: SKView) {

            let delay = SKAction.wait(forDuration: 2)
            let repeatingAction = SKAction.run(repeater)
            let sequence = SKAction.sequence([ delay, repeatingAction ] )
            run(SKAction.repeatForever(sequence))
       }

       func createShape() {

           testShape = SKLabelNode(text: "TEST")
           testShape.position = CGPoint(x: 0.5, y: 0.5)
           testShape.zPosition = 1
           addChild(testShape)
       }

       func repeater() {

            createShape()

            let moveTestShape = SKAction.moveBy(x: -500, y: 0, duration: 5)
            testShape.run(moveTestShape)
      }
 }

This is how you call functions/code blocks in SKActions.

let repeatingAction = SKAction.run(repeater)

or

let repeatingAction = SKAction.run { 
    repeater() 
}

Also remember our are only repeating the spawn action for new labels. The actual action to move the labels is non repeating. So what you should see is 1 label created and moved once, than 2 seconds later a new label gets created and moved once etc

Hope this helps

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Thanks for the feedback. Yes, it is compiling. I guess out of sheer luck. Appreciate the explanation on correct form for calling functions in SKActions. I will make your changes and see if it then runs. – Martin Jones Oct 21 '16 at 21:14
  • Also remember our are only repeating the spawn action for new labels. The actual action to move the labels is non repeating. So what you should see is label created and moved once, than 2 seconds later a new label gets created and moved once etc – crashoverride777 Oct 21 '16 at 21:21
  • I will bear that in mind, thanks. What are the implications of the didMove section of code? I'm beginning to understand what Sprite Kit enables me to do. But, how it all fits together is still a little vague. – Martin Jones Oct 21 '16 at 22:05
  • DidMoveToView is called once when you present a scene, in this case GameScene. Its like ViewDidLoad in UIViewControllers. Its a method where you would do your initial set up for your SKScene e.g loading the player, UI, preparing enemies etc – crashoverride777 Oct 21 '16 at 22:07
  • By the way, the changes you made to the code worked! That one has been bugging me for a while. – Martin Jones Oct 21 '16 at 22:08