-1

I've placed a Color Sprite in GameScene.sks and named it line1LightLeft and gave it the texture named arrowLeftOff.

Now I want the image to change in GameScene.swift. So I first want to connect the node in the .sks to the .swift.

var line1LightLeft = SKSpriteNode()

override func didMove(to view: SKView) {

    line1LightLeft = self.childNode(withName: "arrowLeftOff") as! SKSpriteNode

}

I don't get any errors but when I build and run the game I get the message: fatal error: unexpectedly found nil unwrapping an Optional value

The line of code with self.childNode in it turns red and it says: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Off course I've tried Google but the answers I find don't change it or even give more errors. Does anyone see an error?

Grrrr, why can't I ctrl-click-drag just like in a single view application?

Delah
  • 41
  • 7

1 Answers1

0

If you've named the sprite 'line1LightLeft' then this line of code:

line1LightLeft = self.childNode(withName: "arrowLeftOff") as! SKSpriteNode

should read

line1LightLeft = self.childNode(withName: "line1LightLeft") as! SKSpriteNode

EDIT:

For clarification, the withNameparameter of the childNodefunction refers to the desired node's .name property and not it's .texture property.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55