Enemies can be created either through the LevelData property list, which provides you with many options for animating the enemy, moving them, reviving them, spawning them from other enemies, and so on. But you don’t have to create enemies that way at all if they will be somewhat simple (which is why the class name for these is SimpleEnemy).
To get started drag a Color Sprite to your scene, just like you would to add anything else to your scene. Texture it with your enemy’s main graphic. Then follow these three steps…
- Name it as “Enemy” plus a score value. In the example below, I named it Enemy500, so the point value is 500.
2. Give it a physics body. You can choose either Alpha Mask, Bounding Circle or Bounding Rectangle. Alpha draws the body from the alpha data in the artwork, so this is the most exact shape (but that’s not always great). Circle is obviously a good body if the enemy would roll down a hill. You can set any other physics properties you want. Dynamic means the object is included in the physics simulations of the world. So if something bumps it, it will move. Allows Rotation is pretty obvious, as is Affected By Gravity. Friction is how slippery the object is (1 being, not slippery, 0 being very), and Restitution is how bouncy the enemy is (1 is very bouncy).
3. Finally, switch to the Custom Class tab, and give it a Custom Class of “SimpleEnemy”.
Giving the Simple Enemy some Life
Your enemy is a bit lame at this point. It isn’t moving or animating. But it can. Xcode 7 has some fantastic options now for moving, scaling, hiding, rotating and even animating textures, all done via a Timeline. You can watch a video on how to set this up, or get the highlights below…
- The green arrow is pointing a small icon to unfold the scene’s timeline. From here, scroll down to find the enemy name you created, for example Enemy500.
- The purple arrow is pointing to the Object Library where you can find Actions to drag to the enemy’s timeline. Experiment with Move Action initially as thats a very simple one.
- The red arrow is pointing to where you can set the parameters for any of the actions you’ve added. In the example above, we are moving the enemy 25 points (to the right). In a separate move action, we are moving the enemy -25 (back to the left), to where it started initially.
- The blue arrow is pointing to where an icon appears to Loop your actions. Select all actions you want to loop, then click this icon, then the infinity symbol. Afterwards you will see the actions repeated on the timeline.
- Finally, the orange arrow is pointing to the text that says “Animate”. Click here and the scene will simulate your actions and any physics that have been applied to your scene items.
We said they were simple…
You’ll notice if you play the game now, you can shoot the enemy, and it dies. It doesn’t revive itself or get angry after being shot, or speed up. It just dies. You can hop on it to kill it or hit it with a weapon.
If you want to get a bit more complex, it’s time to generate enemies using the Property List.