Now that you have collectable coins in your game, it's time to add some obstacles to create a challenge for the player.
This part of the guide builds on Part 5, which assumes you have a controllable character, collectable coins, and a basic understanding of adding objects to your scene, the behaviour system and collisions.
Your First Enemy
From the Asset library, navigate back to the "Platformer Starter Pack" that you downloaded in the previous step and select the Pink Slime, then add the "Move" animation to the dock.
Once the Pink Slime is added to your dock, drag it off the dock and place it into your scene.
Tap the slime you just added to the scene to open the Object Properties, then set it to a Physics Object.
Next from the Object Properties, tap the collisions icon to open the collision editor. We need to shrink the collision box around it slightly so our player doesn't get hit too early. With your finger move the green dot on the sides closer to the slime, then press the green checkmark button to accept the changes.
Now we need the enemy to patrol a path. To do this, go to the behaviors of the slime. From the Object category add the "Patrol" behaviour.
Then from the Behaviour Properties, you can set how far the slime should move, and how long it takes to reach the desired destination.
Under the "Movement" section, the X value is how much the enemy will move along the X Axis (left or right). A negative number indicates it will move left, a positive number indicates it will move to the right.
We'll leave the default value (-6) for now. But you can change this based on your scene and how far you want the enemy to move.
To slow down the enemies speed, we'll change the duration to 2 seconds. This means it will take 2 seconds to move -6 meters.
Destroying the Enemy
To destroy the enemy, we'll use the tag system in hyperPad. Tags allow you to reference multiple objects with out having extra behaviors for each object. In this situation, tags will help us by only having to add behaviors for one enemy, and it will automatically work for any new enemy with the same tag. You can learn more about tags here: Object Tags and Applying Behaviors to Tagged Objects.
So lets start by tagging our Pink Slime. First leave the behavior editor and go back to the main editor. Then select the Pink Slime to view the object properties.
From the bottom of the object properties select the "Tags" tab (It's the last tab on the bottom).
To add a tag, enter a name at the top search field. Make sure you choose tags that are easy to remember. For this tutorial we'll simply use the tag "Enemy". Then press the + button to add the tag.
Now we can move on to adding our behaviors. This time select the player character and NOT the enemy. Then open the behaviors for the player character.
From the Object category add a "Collided" behavior, then select it to view the behavior properties.
In the collided behavior properties, turn off all the toggles except "On Top". This is so the collided behavior will only trigger an event when our player touches the top of the enemy.
Next, at the bottom of the behavior properties, switch to the Tags tab. Then select the Enemy tag we added previously. A green dot next to the tag means it is selected and active. Now, instead of triggering an event when touching a specific enemy, it will trigger an event when touching the top of any object that has the enemy tag.
Now from the Object category add a "Disable Object" behavior and connect it to the collided, this will stop the enemy from patrolling and interacting.
Once again, change it to affect only the Enemy tag. Since both the collided and the Disable object have the same tag, only that specific collided object will be disabled, and not all other objects with that same tag.
Next to give the game a little extra polish, we'll make the enemy look different when it gets stomped on. Add a "Play Animation" behavior from the FX category and connect it to the Disable Object .
Then tap the area where it says "Select Animation". Navigate to the pink slime in the asset library, and choose the "Hit" animation.
Once again, change the behavior so it only affects objects with the Enemy tag.
Next add a wait behavior and connect it to the Disable Object behavior (beside the play animation) and set it to a very short time like 0.25 seconds.
Finally, add a "Destroy Object" behavior from the object category and connect it to the wait you previously added. Then once again set it to the Enemy tag. (note in the image below the Disable has been renamed to "8")
And that's it! You now have an an enemy you can destroy.
Making a challenge
Now it's time to make the enemy have the ability to destroy you and end the game.
While still on the players behaviors, add a new collided behavior. This time from the collided behavior properties only turn off the On Top toggle switch. And once again, make sure the enemy tag is selected.
Next, add a Destroy Object behavior and connect it to the collided. Since you're on the player behaviors it's automatically set to destroy the player.
Finally, add the "Load Overlay" behavior from the Scene category and connect it to the Destroy Object behavior you previously added.
An overlay is like a special scene, that can run ontop of your current scene. These are often used for menus, pause screens or game over screens. hyperPad comes included with 2 overlays already created. One for a pause menu, and another for a game over screen. You can modify these to suite your needs, from the Project Menu. You can read more about Scenes and Overlays by searching the topics in the manual.
From the Load Overlay behavior properties, tap "Select Overlay". Then from the list that appears, select the hyperPad provided "Game Over" overlay.
And that's it! Now when you run into your enemy, the player will disappear and the game over screen up will appear allowing you to restart the scene.
At this point, you can add more enemies to your level. Just make sure to tag them as an Enemy.