Spawning Enemies in the Platform Games tvOS and iOS Starter Kit

Spawning Enemies in the Platform Games tvOS and iOS Starter Kit

Each level can generate an unlimited number of enemy characters using the Enemies array. Enemies can be considered anything that might damage the players. So that could be a fully animated robot or a laser beam projectile that is shot from the Robot. In terms of the kit, both would be considered “enemies”

In the screenshot below, you can see the Enemies array unfolded in the first level. Item 0 lists the properties for the Robot seen in the demo of the kit. Quite a few properties, right? We’ve tried to include as many as possible for the demo, so don’t be intimidated by what you see below. Your enemies can be relatively simple. And depending on how simple your needs, you can even create them in the scene file using the method described in this article.

Enemy setup in the tvOS Platform Games Starter Kit


Spawn Locations

Enemies are generated in a location set by either an Empty placeholder node (seen in the image below) or using the name of another enemy. To use an Empty node, all you need to do is drag it into the scene and give it a name, for example, LeftSpawn. Optionally, you can set the Parent property of the node to that of TheCamera (the camera in the scene). If you do this, the node will move with the camera, thus making the spawn location relative to the camera (assuming the camera follows the player). So for example, you might always spawn enemies from the top right or left of the screen.

Spawn locations for Enemies in the tvOS Platform Starter Kit 2

The only property you need to position your enemies is below…

  • SpawnNode – the name of a node or enemy

If you want to spawn an enemy in the same location as another enemy, like a laser from a robot, the SpawnNode property would be the name of another enemy. For example, in the screenshot below, you can see that Item 1 lists a SpawnNode value of Robot, which matches the Name property in Item 0.

Spawning Enemies on Enemies

In this example, every enemy named Robot in the scene would fire a Laser every 2 seconds.  You can offset where the Laser appears in relation to the Robot with the following property…

  • SpawnOffset – a value in {x, y} format to offset where an enemy is spawned in relation to it’s location. For example, a value of { 100, -40} would move the Laser 100 points to the right of the Robot (if the Robot was facing right) and 40 points below the center point of the Robot. If the Robot was facing left, the Laser would be 100 points to the left.

Offsetting the spawn location is important for projectiles so the two enemies don’t collide.

Essentials…

Aside from the SpawnNode property mentioned above, the following properties are what we would consider essential to creating an enemy via the property list…

  • BaseFrame – the name of the image to use for the enemy. Even if your enemy will be animated using the animation properties (described below), set this value to the first frame of the walk animation. This will be used code-wise for sizing the enemy.
  • InitialDelay – a value in seconds to wait before spawning the enemy. When a scene loads every enemy’s initial delay is started, but enemies are not spawned if they are too far offscreen. So this property might not have an affect if your spawn location is outside of the horizontal frame of the camera. If this were the case, the enemy would be spawned according to the next property…
  • SpawnFrequency – a value in seconds for how often the enemy spawn after the initial delay. For example, a value of 5 would spawn the enemy every 5 seconds, as long as the spawn location is within the frame of the camera.
  • TotalToSpawn – a value for the number of enemies to spawn. If this is set to 0, an infinite number will be spawned.
  • XScale – a value of -1 will make the enemy character face left initially. Artwork for both players and enemies should always be drawn facing the right. A value of 1 will make the enemy face right, but this is the default anyway, so the property can be excluded if the enemy faces right initially.
  • Speed – the speed the enemy moves. Enter 0 if the enemy should go nowhere (or just exclude this property).
  • Score – a number value for how much score this enemy awards when it is killed.
  • Depth – a positive or negative number value for the z position (visual depth of the enemy)
  • Name  – the name of the enemy. This property is only required if the enemy will be used to spawn other enemies (as described above). For example, a Laser might shoot from all Robots every few seconds. In which case the Robot would need it’s Name property would need an identifier like Robot.

Physics

  • BodyType  – this property can have 3 possible values: AlphaCircle or Rectangle.  Alpha will use the alpha data in the base image of the enemy to create a very exact physics body. Circle will create a circular physics body, and Rectangle will create a rectangular body.
  • RadiusDivider – If you choose Circle for BodyType, you can adjust how large or small the circular body is by adjusting this value. By default it is 2, which creates a circle which fits inside the base image exactly. A value of 3 would make the body smaller, and a lower number would increase the size.
  • BodySize – If you choose Rectangle for BodyType, you can adjust the size of that rectangular shape to any size you wish by entering a value in {width, height} format. For example, {150, 250} would create a physics body 150 points wide, and 250 tall.
  • BodyOffset – The placement of either a Circle or Rectangle body can be offset from the base image by entering a value for this property in {x, y} format. For example, a value of {0, -10} wouldn’t move the body on the x axis (rarely would ever need to) but it would move the body lower 10 points on the y axis. Don’t forget you can preview your game with all physics bodies showing by finding the GameViewController.swift file and searching for “showsPhysics” and setting that to true. 
  • AffectedByGravity – a YES or NO value to toggle on or off gravity pulling down the enemy. By default, this value is YES.
  • AllowsRotation – a YES or NO value to toggle on or off whether the enemy can rotate based on physics interactions. By default, this value is NO. Most likely, you want this to be NO so the enemy always stays upright. But in cases, where for example, the enemy is a boulder rolling down a hill, you would want this set to YES.

Dying / Removing the Enemy

  • ReviveCount  – a number value for how many times the enemy will be revived. You could also think of this like the amount of damage an enemy can take. For example, a value of 0 would destroy the enemy right away. With value of 1, the enemy would need to be hit again to be destroyed.
  • ReviveTime  – a number value for how long the enemy takes to “come back” from being damaged. After being damaged, the enemy can’t be hurt again and can’t hurt the player until this time has passed. It could be a small decimal value or many seconds long.
  • SpeedAfterRevive  – an optional speed value to switch to after the enemy has come back from being hurt. In terms of the kit, the enemy is in “angry” mode after being hit once (you can also have animations for angry-walking, shooting and attacking).
  • RemoveOutsideBoundary  – a YES or NO value to set whether the enemy should be removed if they move outside the view of the camera.
  • BlinkToDeath  – a YES or NO value for whether or not the enemy “blinks” away when dying. If YES, before the enemy is removed from the scene, they will flash quickly between visible and invisible states, emphasizing they are dead.
  • JumpOnToKill  – a YES or NO value for whether or not the players can jump on the enemy to hurt  / kill it.
  • JumpOnBounceBack  – a number value for how much of a bounce back effect the player gets when jumping on the enemy. The default is 500.
  • TreatLikeBullet  – a YES or NO value to treat the enemy like a bullet or projectile. If YES, the enemy will be destroyed when it hits a platform.
  • ExplodeAsBullet – a YES or NO use the DeadFrames (animation frame array) as an “explosion” type effect if the TreatLikeBullet property is set to YES.
  • JumpThreshold  – This is how far above the player needs to be over the enemy to jump on them to do damage.  If this number is too low, a player and enemy might simply bump each other on a platform and the enemy is hurt. If this number is too high, the enemy won’t get hurt at all when the player jumps on them. By default this number is 50, which generally seems about right, but the number might need to fluctuate some depending on specific physics bodies. Raise or lower as needed.

Animation

Each enemy can have any of the following animations you want to include (all of them are optional)…

  • Walking – In terms of the kit, this could also be considered the enemy’s main animation. So for example, if the character was a bat, well obviously bats don’t walk, they fly, but their flying animation frames would be considered their “walk”. If the enemy doesn’t walk at all, and you just want to include frames of the enemy idling, include them here as well.
  • Angry Walking  an optional animation state that would run after the enemy has been damaged once. For example, you might simply export your same walking animation frames, but tint them red.
  • Attacking – run when contact between an enemy and player occurs, these animation frames will run automatically if included.
  • Angry Attacking – run when contact between an enemy and player occurs and the enemy has already been damaged once, these animation frames will run automatically if included. If not, the enemy would fall back to their regular attacking animation (if that was included)
  • Shooting – In terms of the kit, a shooting animation is anything that occurs when the enemy spawns another enemy. Keep in mind, another “enemy” might by any projectile. So for example, a Rock Monster might throw another rock. A Warlock enemy might cast a spell and project out an orb. So “shooting” is just a loose term.
  • Angry Shooting – same as above, but run if they enemy has been damaged once.
  • Hurt – run when the enemy is hurt, but will revive again.
  • Dead – run when the enemy is completely dead and about to be removed from the scene.

The following properties set your enemy animations…

  • Atlas  – the .atlas folder containing the enemy characters animated frames. Do not include “.atlas” in the value. 
  • WalkFrames  – an array of image names for this state, minus any file extensions. For example, see the image below.
  • AngryWalkFrames  – an array of image names for this state, minus any file extensions.
  • AttackFrames  – an array of image names for this state, minus any file extensions.
  • AngryAttackFrames  – an array of image names for this state, minus any file extensions.
  • ShootFrames  – an array of image names for this state, minus any file extensions.
  • AngryShootFrames  – an array of image names for this state, minus any file extensions.
  • HurtFrames  – an array of image names for this state, minus any file extensions.
  • DeadFrames  – an array of image names for this state, minus any file extensions.
  • WalkFPS  – the frame rate for this set of frames (use a range of 1-60)
  • AngryWalkFPS  –  the frame rate for this set of frames (use a range of 1-60)
  • AttackFPS  –  the frame rate for this set of frames (use a range of 1-60)
  • AngryAttackFPS  –  the frame rate for this set of frames (use a range of 1-60)
  • ShootFPS  –  the frame rate for this set of frames (use a range of 1-60)
  • AngryShootFPS  – the frame rate for this set of frames (use a range of 1-60)
  • HurtFPS  – the frame rate for this set of frames (use a range of 1-60)
  • DeadFPS – the frame rate for this set of frames (use a range of 1-60)

animations in the tvos ios platform games kit

You can also hover an enemy using the two properties below. This is useful for ghost like characters that you want to appear floating. If you set this property, the enemy’s gravity affect is turned off (it is turned back on though when they are hurt or dead).

  • MoveUpAndDownAmount – a number for the amount the enemy goes up and down as they float.
  • MoveUpAndDownTime – a number for how long it takes them to go up and back down again.

 

Sounds

  • SoundEnemyEnter – a sound file to player when the enemy is spawned. Include the file extension, such as .caf or .mp3.
  • SoundEnemyHurt– a sound file to player when the enemy is hurt. Include the file extension, such as .caf or .mp3.
  • SoundEnemyDie – a sound file to player when the enemy is dead. Include the file extension, such as .caf or .mp3.
  • SoundEnemyAttack – a sound file to player when the enemy is attack. Include the file extension, such as .caf or .mp3.