Enemy Entity
Last updated
Last updated
This tutorial creates an Enemy entity which is used in the remainder of the tutorials. This enemy is similar to the Player entity - it has collision and uses Platformer physics, but it does not use input from a keyboard or gamepad - instead its movement is controlled purely in code.
To create the Enemy entity:
Click the Quick Actions tab
Click the Add Entity button
Enter the name Enemy
Check AxisAlignedRectangle
Check Platformer for the Input Movement Type
Leave the rest of the defaults and click OK
You can optionally change the color of your Enemy if you would like by selecting the newly-created AxisAlignedRectangle and setting its color to Red.
Entities such as Enemy are usually added directly to levels such as Level1. Note that it is possible to add Entities in more ways including through Tiled and directly in code, but we will be adding an instance directly in the FlatRedBall Editor.
Note that the EnemyList object is defined in GameScreen, but it is also accessible in all levels, such as Level1.
The EnemyList must be accessible in both screens. It is used in GameScreen to create collision relationships, which should always be the same across all levels. It is used in each level to define which enemies should appear in a particular level. This setup is enabled by the EnemyList having its ExposedInDerived property set to true, which is set up by default from when we created our Enemy entity.
To add an enemy to Level 1, drag+drop the Enemy entity onto the Level1 Screen.
The newly created entity is automatically added to Level1's EnemyList.
Next the Enemy should be positioned so it is standing on the ground. You can position this through trial-and-error by running the game and adjusting the position of the entity, but the fastest way to accomplish this is in Live Edit mode. For information about setting up Live Edit, see the Live Edit page.
Now we have a fully-functional enemy, but it falls through the solid collision since we haven't yet set up an EnemyList vs SolidCollision relationship. To do this:
Expand GameScreen Objects folder
Drag+drop the EnemyList onto SolidCollision. Notice that we are doing this in the GameScreen rather than Level1 because we want all enemies to collide with the SolidCollision regardless of level. By doing this in the GameScreen, this new Collision Relationship will apply to all levels, including Level1.
Glue automatically sets the Collision Physics in the new Collision Relationship to Platformer Solid Collision since the Enemy entity is marked as a Platformer.
Now we have an Enemy entity and an instance of this Enemy in Level1. This instance collides with the game's SolidCollision and has full support for platformer physics. Currently both the Player and Enemy are controlled by the keyboard (or gamepad if one is plugged in). We will remove this input control from the Enemy and replace it with logic-based movement in the next tutorial.