M. Black
M. Black

Reputation: 353

Spritekit game experiencing significant lag on iPad simulator

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

  1. Initial View Controller (PlayerPickerViewController) - this allows the user to select who is Player 1 & Player 2. It then chooses the name of the (student) class to use as well as the image for their spaceship. There is a struct that has a static object for each spaceship so that they can be accessed in different classes.
  2. Game's View Controller (GameViewController) - this takes the GameScene and makes it show up inside this view controller
  3. Game Scene (GameScene) - handles all of the initial set up for the game including creation of variables and adding nodes to the view.
  4. Spaceship and Missile classes - these handle the methods and properties that define the behaviour of a spaceship and the missiles (i.e. movement, firing, size, etc.). Students may not modify this code so that they are working all within the same game conditions.
  5. Student Classes ("their last name".swift) Inside this class, there are two functions that students must program (one for player1 and one for player2). This is where students define the strategy for moving and shooting. Every frame, the appropriate function is called to move the player. When the players are chosen in the PlayerPickerViewController the game understands which of the student classes needs to be called each frame (based on their name and the name of the class which are named exactly the same).

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

Answers (1)

Swift Dev Journal
Swift Dev Journal

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

Related Questions