Reputation: 353
Background: I am a high school computer science teacher and I wanted to come up with an engaging/fun assignment that tests my students' programming knowledge. I have very little experience programming iOS games with SpriteKit, but I thought it would be an interesting way to integrate many of the curriculum concepts in a competitive environment.
The Game: Each student will play one another in a head-to-head battle where they are expected to program the artificial intelligence of a spaceship that can move and fire missiles towards their enemy. Each player starts with 20 points and every time they get hit by a missile their total decreases by one, until a player hits zero and the game is over. Each player gets only 10 missiles so they must use them wisely. The missiles replenish if they go outside of the view boundaries or they hit the other opponent.
The Problem: I've done this assignment for several consecutive years with only minor changes each year. However, this year during testing the game drops to around 9fps once the game begins. Obviously this takes away much of the fun of the game and makes debugging painful because of the lag.
Code Structure
Nodes There are only a couple of SKLabelNodes that are used to display the player name and score. The spaceships are both nodes and they each contain an array of 10 missiles which are also Nodes. Both the spaceship and missiles have their own individual classes that define their properties and behaviour. There is also a background image used. In total, there are only 24 objects that are added to the GameScene and this never changes.
Classes
When I build and test the game on my personal MacBook Pro the game runs as is expected to (without lag). However, each student is working on a Mac with the following specs:
It seems to me that even though the computers are getting a bit older, they should still be able to handle this simplistic of a game with only 20+ nodes.
Would changing the simulator make a difference (i.e. iPad Pro 12.9 inch 3rd generation to a 6th generation iPad)?
I appreciate any help you can give me in order to solve this lag issue. Thank you.
Here is a link to the game if you are interested in seeing the code and how the classes interact with one another. The GAME It isn't the most recent edition, but it is extremely similar to what the students are currently using.
Upvotes: 0
Views: 198
Reputation: 20088
Start by running the game on a device to narrow down the problem and make sure the problem is with the Simulator.
Profile your game with Instruments on one of the iMacs. That would let you see what is causing the frame rate to be so low.
Choose Product > Profile in Xcode to profile your project in Instruments. Choose the Time Profiler instrument from the list of templates. The following article shows how to use the Time Profiler instrument:
Finding the Slow Spots in Your Code with the Time Profiler Instrument
Since your students have to test the game on their Macs, an alternative is to make a Mac version of the game. The game would run better natively on a Mac than the iOS Simulator. The SpriteKit code is the same on both Mac and iOS. The main difference is handling mouse events on Mac instead of touch events on iOS. Xcode has a cross-platform game project template that lets you make a game that runs on both Mac and iOS.
Upvotes: 1