0

I’m currently working on a sprite kit - I have a first level complete. My game scene calls from encounterManager for the scenes I have prepared.

I am curious as if it is possible to duplicate the game scene in order to set-up a secondary level? I have tried this in which the build is successful in reference to when selected ‘game scene2’ runs.

My problem is that now I have done this as a trial the game scene appears but is completely still. I then get an error directing me to one statement in code which is as follows :

    If self.physicsBody!.velocity!.dy > 300 {
    self.physicsBody!.velocity.dy = 300
    }

I get the error :

Thread 1: Fatal error: unexpectedly found nil while unwrapping an optional value.

This seemed to run fine operating at one game scene and all of a sudden has referred this error message..

I am fairly new to Xcode and have tried to get my head around optionals but I can’t understand how to overcome this or being able to re-tailor the statement?

halfer
  • 19,824
  • 17
  • 99
  • 186
AJ James
  • 11
  • 5

1 Answers1

0

You can try to unwrap the optional whit if let statement to avoid

Thread 1: Fatal error: unexpectedly found nil while unwrapping an optional value.

Some like:

if let physicsBody = self.physicsBody {
    if physicsBody.velocity.dy > 300 {
        physicsBody.velocity.dy = 300
    }
}

But probably the root cause is your node or body is deinitialized at the moment of execution.

Maetschl
  • 1,330
  • 14
  • 23
  • Hello - thank you for the feedback, I have now eliminated the error and run the simulation as per your suggestion, good news is I do not see this error - it does however remain the same in gameplay where I can see my character but does not move forward. I get a caution error showing under 'if let physicsBody = self.physicsBody { – AJ James May 13 '20 at 14:52
  • which says - Value 'physicsBody' was defined but never used; consider replacing with boolean test' Underneath it gives me the option 'Replace 'let physicsBody = self.physicsBody' with 'self.physicsBody != nil' I have tried this swell as suggested and still get no movement with the player. :/ I assume you may be right with your end statement. If you could advise further it would be hugely appreciated – AJ James May 13 '20 at 14:57