Group Group Group Group Group Group Group Group Group

Chapter 13 (Apple games by tutorials) fatal error

I wrote that I won’t be posting anymore errors on this forum, but I changed my mind. I am stuck on chapter 13 page 367. I build the game to see the bugs animate with the array of animation in Bug.swift.

I copied the code exactly how it is written in the book and am getting a fatal error PestControl[2710:687445] Fatal error: Index out of range

this line is getting the RED bar with the words Thread 1:Fatal error: Index out of range :
— run(animations[direction.rawValue], withKey: “animation”) –
when I comment out that line, I see the bug animations after I build it, but just for the front walk animation for the bug, and not for the back or sideway walk animation.

Hi @game_gecko - I checked out the sample code in final, and that works. Does it work for you?
Perhaps you could check your code against the code in the final directory?

I started with the starter file from chapter 13, and I always check the final version when I run into a problem, for each chapter.

That’s odd then. Please could you zip up your project and post it here? And also tell me the specifications of all the devices that you’re running it on - macOS version, Mac specs, Xcode version, simulator/device version etc.

I am using a MacBook Pro 2017, 2.3 GHz Dual-Core Intel Core i5 processor,8 GB of ram, Intel Iris Plus Graphics, 120 GB hd. The operating system is Catalina 10.15.3 and Xcode 11.3.1 (11C504).

I am using a iPad Pro 10.5" 64GB drive for testing.

I have a MacBook Pro, but it has a separate GPU as well as the Intel. I wonder if that’s the problem. Are you able to zip up the whole project and upload it here and see if I can run it? I can’t work out how to test SpriteKit using only the Intel. (402.6 KB)

I forgot that I started with chapter 12 starter file and worked on it since. I also converted the file to swift 5 version when prompted. So, I don’t know if that had anything to do with the problem I am having?

Sorry about the late reply - I didn’t see the notification.

In Chapter 12, Beginning Tile Maps, you create a method called createAnimations. You appear to have left the final line out, which appends a fourth animation.

Add this to the end of createAnimations(character:):


I debugged this by adding code before the crash to print out properties to see why it was crashing. I added this code just before the crash:


From that I could see that direction.rawValue was 3, meaning it was looking for the fourth element of animations. But animations only had three elements. So then I tracked down why animations only had three elements instead of four.

The reason why the fourth one is repeated, is that left and right animations are the same, only right is a mirror of left. That’s taken care of with changing the sign of xScale when setting up the direction in move().

Thanks for your help! I will change the code as soon as I get back to working on the chapter. Also, you mentioned about moving the character , I remember that when I moved him to the right the game crashed.

Hopefully, I will have no problems with your solution.:+1:

I hope so! I’m really glad you’re progressing through the book. SpriteKit is a great way to get into making games :slightly_smiling_face: