1

I'm a newbie with Swift - after searching so much hours, I need help from you professionals :D

Planing a small IOS App with Swift, this is the structure:

  • GameViewcontroller
  • GameScene

In GameScene, I placed a little image:

class GameScene: SKScene {
private var tanzschuhR: SKSpriteNode!
override func didMove(to view: SKView) {

    self.size = CGSize(width: 1024, height: 1024)
    self.backgroundColor = .gray
    self.scaleMode = .aspectFill

    let startPt = CGPoint(x: 500, y: 400)
    tanzschuhR = SKSpriteNode(imageNamed: `SchuhR`)
    tanzschuhR.position = startPt
    tanzschuhR.zPosition = 1

    self.addChild(tanzschuhR)

    func swingPattern1() {
        // move it
        let negDelta = CGVector(dx: -50, dy: -50)
        let action = SKAction.move(by: negDelta, duration: 4)
        tanzschuhR.run(action)
    }
}
}

For now, I just want to start the function swingPattern1() from a IBAction Button in the GameViewController, in order to start the movement of the image synchron to the audio is playing.

This is the part of the button:

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()
// no idea to call the function swingPattern1()
// always get an error
}

So, could anyone help me?

How to call this function, if the button is tapped?

Or is there another, better way to start audio an the movement by tapping a button?

Many thanks for your help...


UPDATE:

I placed the var in the IBAction:

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()
    let gameScene = GameScene()
    gameScene.swingPattern1()

}

Don't know,if it's the right syntax - sorry... :D

After BUILD&RUN, I get this error:

enter image description here

The app stopps...

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Joe
  • 21
  • 1
  • 3
  • `tanzschuhR` is `nil`- where is it being assigned? – Ashley Mills Mar 22 '17 at 15:01
  • In the class at the beginning... – Joe Mar 22 '17 at 15:17
  • So, it appears `swingPattern1()` is called before `didMove(to view: SKView)`, so `tanzschuhR` hasn't been assigned a value. – Ashley Mills Mar 22 '17 at 15:23
  • Ok, sounds logical. Do I have to put all in the function or do you have any other solution for me? – Joe Mar 22 '17 at 15:25
  • Maybe my swift structure is wrong from the beginning... I simply want to start an animation (move a spritekit image) when starting an audio (dancing shoes, when music starts). The audio starts when IBAction Button is pressed... everything is in a GameViewcontroller with a separated GameScene. Are there any other solutions? – Joe Mar 22 '17 at 21:47
  • It sounds like you really need to do more reading on spritekit basics - try working through this RayWenderlich tutorial - https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners – Ashley Mills Mar 23 '17 at 08:16
  • You are right :D - but the link is broken... – Joe Mar 23 '17 at 09:40
  • Hmm - just clicked on it and worked OK for me. Google "ray wenderlich spritekit" – Ashley Mills Mar 23 '17 at 09:42

1 Answers1

0

Firstly, move swingPattern1 outside didMove(to view: SKView)

class GameScene: SKScene {
    private var tanzschuhR: SKSpriteNode!
    override func didMove(to view: SKView) {

        self.size = CGSize(width: 1024, height: 1024)
        self.backgroundColor = .gray
        self.scaleMode = .aspectFill

        let startPt = CGPoint(x: 500, y: 400)
        tanzschuhR = SKSpriteNode(imageNamed: `SchuhR`)
        tanzschuhR.position = startPt
        tanzschuhR.zPosition = 1

        self.addChild(tanzschuhR)
    }

    func swingPattern1() {
        // move it
        let negDelta = CGVector(dx: -50, dy: -50)
        let action = SKAction.move(by: negDelta, duration: 4)
        tanzschuhR.run(action)
    }
}

Then in your view controller…

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()

    // Assuming you already have a var gameScene: GameScene
    gameScene.swingPattern1()
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Many thanks for your quick help, but: how do I have to assume this var, at which place? Like that: let gameScene = GameScene() gameScene.swingPattern1() ?!? I've tried this way, but get another error in the function: -> tanzschuhR.run(action) --> AQDefaultDevice (173): skipping input stream 0 0 0x0 fatal error: unexpectedly found nil while unwrapping an Optional value – Joe Mar 22 '17 at 13:53