How To Stop Sprite Animation In Game Maker Dnd
Overview
This tutorial expands on the Hero'south Trail projection provided with GameMaker. We'll make an blitheness for the baddie and create a brand new AI enemy from scratch. We'll besides build a hearts system for the player and add knockback!
This tutorial has the following sections:
-
Editing Paths
-
Creating Animations
-
Using Sequences
-
Adding Assets to the Sequence
-
Animative the Defeated Enemy
-
Playing the Sequence
-
-
Edifice a New Enemy
-
Calculation the Object
-
Following the Player
-
Enemy-Wall Collisions
-
Idle Flight Behaviour
-
Turning Left/Right
-
Defeating the Enemy
-
-
Shooting Projectiles
-
Calculation the Object
-
Projectile Behaviour
-
Projectile Destroy Animation
-
Spawning the Projectile
-
Managing the Alarm
-
-
Player Hearts
-
Adding the Variable
-
Drawing Hearts on HUD
-
Hurt Knockback
-
Defeating the Player
-
Editing Paths
The enemies in our game (called "baddies") follow predefined paths that are designed in the Room Editor. You can easily edit these to alter where they walk.
The "Instances" layer in the game room contains all our enemy instances. You can double-click on an enemy, open its Variables and see its assigned path asset in the path_to_follow variable:
For the enemy shown in a higher place, the assigned path is path_enemy_1. You tin can find it in the "Paths" group in the Asset Browser, open up it and edit information technology:
However, yous cannot see where the path is placed as it's simply shown on a blank canvass. This is precisely why the Room Editor allows you to create Path Layers, and so you can edit a path asset right inside the room!
You lot can read more most Path Layers under the "Paths" department on this transmission folio .
Modifying Enemy Paths
Let's do the post-obit to change where our first enemy walks:
-
Go to the game room and find the layer called "EnemyPath1".
-
If y'all click on the layer, nether the "Layer Backdrop" window you will encounter the path asset proper name:path_enemy_1.
This is the path that was assigned to the enemy previously shown, so editing this layer will change that enemy's path.
c
-
Y'all can click on the eye icon to plow this layer invisible and and then visible, so you lot tin find where the selected path is in the room.
-
You tin can now move any of the points around to modify the path. Right-clicking on any point volition open a card allowing you to delete it.
-
If you click anywhere else inside the room, a new point will be created on the path, right afterward the terminal point.
-
You can now run the game and encounter that the enemy follows your modified path!
Under the Layer Backdrop, you volition see that the "Closed" property is enabled for this path. This means that the last and the get-go points in this path will stay connected, forming a loop. You tin disable this if you want your path to accept separate starting and ending points.
The "Showtime Post-obit Path" action has an "On End" statement to control what happens when the instance reaches the terminate of its path. Our enemy object simply continues following the path (which you can run across in the Create event of obj_baddie), however if yous adopt to use a non-closed path with divide starting and catastrophe points, you can use the "Opposite" option then it starts post-obit the path in reverse when it reaches an ending point.
Creating New Enemy Paths
If you desire to add together a new enemy to the room and create a new path for it, follow these steps:
-
Place a new baddie case in the room.
-
In the Layers panel, select the "EnemyPaths" group and click on the post-obit button to create a new Path Layer inside it:
Name this layer "EnemyPath4" (as this is our quaternary enemy path in this room). You lot now need to create an bodily path nugget for this layer. -
Under the Layer Properties, click on "Select Path…" and then select "Create New".
-
This will create a new path asset that volition be assigned to this layer. You can now click anywhere in the room to start adding points to your new path!
-
Under the Layer Backdrop, yous can change the "Connection Kind" to "Smooth Curve" to make the enemy'south movements expect realistic. You can too brand it closed to form a loop.
Nosotros've just created the path itself though, then we need to tell the enemy example to use it.
Using the Path
You can at present assign this path to your new enemy example:
-
First of all, select your new Path Layer and look under Layer Properties to discover the proper name of the assigned path asset. Information technology may be something like "PathX" where 10 is a number.
-
This step is non required but is recommended for good organization: find the path asset in the Nugget Browser (it volition usually exist at the very bottom of the listing) and motion it into the "Level1" grouping under "Paths".
-
Double click on your enemy instance and open its Variables.
-
Edit the path_to_follow variable (by clicking on the pencil icon) and set its value to the new path asset (Path6). This is the path that this enemy will now follow.
If you run the game, you lot will now see your new enemy following your new path!
Using Sequences
Sequences are assets that permit you to create your own animations; you lot can use them to animate sprites, objects and sounds and so play the Sequence anywhere in-game!
Nosotros'll create a Sequence blitheness for the baddie being defeated, and play that animation when it'due south destroyed.
How Will This Work?
We'll create a Sequence asset, which is simply an animation.
This animation will evidence the baddie falling downwards.
When a baddie instance is hit with the sword, nosotros'll remove the instance and play the Sequence.
Permit'due south do the post-obit to create a new Sequence asset:
-
In the Nugget Browser, select the "Sequences" group.
-
Go to the Create Nugget bill of fare at the elevation and create a Sequence nugget.
-
Name this asset seq_baddie_defeat. The "seq_" in the get-go stands for "sequence".
Sequence Editor
This will open the Sequence Editor in your workspace, which has the following parts:
-
The Canvas is where yous create and run across your animation
-
The Track Console is a list of your tracks (which are simply your sprites/objects/sounds)
-
The Dope Sheet is where you can edit the timings of your animations (when they move, rotate, etc.)
We will now start creating our enemy's defeat animation stride-by-footstep, but for a detailed introduction to Sequences, you may check out the following resources first:
-
Manual folio for Sequences
-
Official video tutorials for Sequences
Adding Assets to the Sequence
Permit'southward add together the baddie's defeated sprite into our Sequence:
-
In the Asset Browser, go under "Sprites" and open the "Baddie" grouping.
-
Drag and drop the spr_baddie_defeat sprite anywhere into the Sequence:
-
If you placed information technology somewhere inside the Canvas, move information technology then that it's exactly in the centre.
-
If yous striking Play in the Dope Sheet, you will see the sprite for a couple frames, and it will then disappear. This is not ideal as we want it to exist present throughout the whole Sequence.
Tracks & Dope Sheet
-
On the left, the Track Panel contains your spr_baddie_defeat rails.
On the correct, the Dope Sheet contains the "asset key" for the track.
-
Yous tin can move and resize this asset key to change when and for how long the track is visible. Nosotros'll extend this to last for the entirety of the Sequence.
-
You can use the "playhead" in the Dope Sail to see different frames. By default, whatever edits fabricated to a rail volition exist recorded at the current playhead frame.
Currently there is no difference between any of the Sequence'southward frames, notwithstanding as we beginning creating our blitheness, the playhead will get more useful.
Animating the Defeated Enemy
We'll create a unproblematic blitheness of the enemy falling down and disappearing. For this, we'll need to create ii "keyframes".
A keyframe is a special frame that records the position, rotation, etc. of the sprite. If yous have two keyframes, say keyframe A and keyframe B, your sprite will be blithe so it gradually goes from its state in keyframe A to its new land in keyframe B.
Our enemy'south defeat Sequence will have two keyframes:
- The first keyframe will be its default state, as it currently appears in the Sequence (where the enemy is continuing upright).
- The 2d keyframe will be the enemy rotated and shrunken so information technology appears fallen down.
The Sequence will then automatically interpolate all frames between those two keyframes to create an animation, so it looks like the enemy actually fell down!
Creating the First Keyframe
The beginning keyframe will exist at the offset frame of the animation.
Brand sure that the playhead is on frame 0, and and so click on the "Record A New Primal" push button. This will record the current position, rotation and scale of the sprite and create a keyframe at the electric current frame.
One time you accept clicked on that button, your offset keyframe is ready (as no other changes are required to information technology).
Creating the Second Keyframe
The second keyframe will be of the enemy fallen down. We'll do the following to create it (the steps listed below are also shown in a GIF under it):
- Motility your playhead to around frame 15 (or an earlier one if you wish to have a shorter blitheness).
- Rotate the enemy so it faces downward.
- Move information technology downwards and to the right and so it appears fallen down.
This will tape our second keyframe at the selected frame!
Finishing the Animation
The enemy now appears to fall down, but information technology does not disappear. We'll now arrive compress (only on 1 axis) and shorten its nugget key so information technology completely disappears.
Let's do the following (the steps listed below are besides shown in a GIF under it):
- Go on the playhead on the second keyframe (y'all will meet it on your asset key with a diamond icon).
- Calibration the sprite downward vertically and move it upwards to correct its position.
- Shorten the asset primal so the enemy disappears after its second keyframe.
The enemy's defeat animation is now fix!
Keep in mind that you can shorten your animation easily if yous experience information technology is too long: in the Track Panel, expand your track, and in the Dope Sheet move the 2d keyframe(south) so the animation is shorter. Besides make sure to adjust the total asset key length.
Playing the Sequence
Nosotros'll now play our new Sequence when the enemy is defeated, and destroy the enemy case then only the Sequence is visible. Nevertheless, at that place is something important to keep in listen.
If you play the defeat Sequence when the sword hits the enemy, the animation volition only appear after 1 frame. If you lot destroy the example at that moment and so y'all will have 1 blank frame where neither the Sequence nor the enemy is visible.
To piece of work around this, we'll exercise the post-obit:
-
Play the Sequence
-
Wait 1 frame
-
Destroy the enemy instance
How Will This Work?
Nosotros'll create an "Warning" in our enemy object.
An Alarm is an event that yous tin run yourself.
What's special about it is that you lot can run it later.
So we'll run an Alarm event one frame after the Sequence has played, and in that Alarm result, destroy the enemy example.
To achieve that, let'due south follow these steps:
-
Go to obj_baddie. This object has a collision event with the sword object, still it'due south greyed out because it's inherited from the enemy's parent object.
-
The parent object is what currently controls how the baddie loses, which is why information technology inherits the parent event and just uses that.
-
-
We desire to redefine this consequence for this particular enemy, so right-click on the issue and select "Override Upshot".
-
You volition now have a new, empty event.
-
Search for the "Gear up Alert Countdown" action and drop it into the result.
-
Search for the "Create Sequence" action and drib it into the effect.
Use the following settings for the new actions:
Alarm Events
GameMaker's Alarm events tin can exist told to run later a specific number of frames. In the higher up event, we are telling Alarm 0 to run after 1 frame.
Permit'south program this Alert to destroy the enemy instance:
-
Now in the aforementioned object (obj_baddie) add together a new event: get nether "Warning" and select "Alarm 0":
-
This issue runs ane frame after the enemy'due south collision with a sword, considering we told it to.
-
Add the "Destroy Instance" action in this event.
Now run the game, assault an enemy and you lot will see your Sequence animation!
(Optional) Sequence Depth-Sorting
Note: This section is optional; keep reading to larn how to implement Sequence depth-sorting, or skip to "Building a New Enemy".
You may discover that the defeated enemy Sequence sometimes appears above the histrion even when it should be behind it:
This happens because the Sequence is simply displayed on the "Instances" layer and is not depth-sorted like object instances.
What is Depth-Sorting?
Depth-sorting is how the game figures out whether an example should announced behind or in front of another instance.
It's already implemented in the base project, nevertheless it just applies to object instances, non Sequences (as Sequences don't run lawmaking like objects do).
We now have to acquit the baddie instance'due south depth over to its defeat Sequence.
As a elementary workaround, we can create a new layer that uses the depth value of the baddie instance and display the Sequence there.
Here's how you can implement that:
-
Go to obj_baddie and open its collision event with obj_sword_attack.
-
Before "Create Sequence", add the "Role Call" action.
-
Use this action to telephone call the layer_create part. Enterdepth as the argument. Set the "Target" to seq_layer so the layer ID is stored in that variable (go far temporary).
-
This volition create a new layer at that depth, and then anything in this layer will appear at the aforementioned depth as the baddie.
-
In the "Create Sequence" action, laissez passer in the seq_layer variable so the Sequence is created in that layer.
Your consequence should at present look like this, with the new "Function Call" activity in the center:
The baddie defeat Sequence will at present ever announced at the correct depth; due east.yard., if the enemy is behind the thespian, the Sequence will as well appear behind it, and if the enemy is in forepart, the Sequence will as well be in the front:
Building a New Enemy
We'll create a new bat enemy that flies around and follows the player. Information technology will also shoot projectiles at the player.
How Volition This Work?
We'll create a new object for the bat, and it will be a child of the enemy parent.
The bat will create an imaginary rectangle around itself, and cheque whether the role player resides inside that rectangle.
If the thespian is in the rectangle, the bat will get-go moving towards the player.
Later, we volition use Alarm events so the bat tin repeatedly fire projectiles towards the thespian.
Let's kickoff by creating its object!
-
In the Asset Browser, open "Objects", and then "Game" and select the "Enemies" group.
-
Here, create a new object and name this obj_bat.
-
Assign the spr_bat_fly sprite to it, which can be found in the "Bat" group under "Sprites".
-
Open its Parent menu and assign obj_enemy_parent equally its parent.
Since nosotros've made this a child of obj_enemy_parent, it will automatically hurt the player on collision, and the role player's sword volition besides be able to defeat information technology!
Go alee and place it anywhere in the room (make certain to use the "Instances" layer):
Proceed in mind that information technology's completely up to you how you want to organise your layers, and so if you lot similar, you can create a new Instance layer for your enemies and put the baddie and bats in there!
Following the Histrion
The bat should follow the player when they're close to each other. For this, nosotros'll create an imaginary rectangle around the bat that spreads 200 pixels in each direction, and check if the thespian is simply touching that rectangle:
If the player overlaps that rectangular area, the bat will start going after it!
Let's program that:
-
Add the Step consequence to obj_bat. This event runs every frame, so we tin can program our AI behaviour hither.
-
Add the "If Collision Shape" activity to the event. This is a condition that allows you to check for collisions within a rectangular surface area.
-
Attach the following deportment to this condition:
-
"Set Speed" (not to be confused with "Set Animation Speed")
-
"Set Point Direction"
-
Apply the following settings for these actions:
The bat volition now constantly cheque for the player beingness in that rectangular area, and if it's plant, the bat volition kickoff following it!
At the end of that GIF, you will detect a trouble: the bat can move through walls! We need to give it collisions, just like the histrion.
Enemy-Wall Collisions
This is very elementary every bit nosotros can copy the actor's standoff event, and paste it into the enemy parent object (and so all enemies get collisions!):
-
Get to obj_player. Right-click on theobj_collision_parent event and select "Re-create Issue".
-
Go to obj_enemy_parent. Right-click anywhere in the Events window and select "Paste Consequence".
Voila! All enemies at present take collisions, merely similar the histrion! This means that bats will at present exist stopped by walls:
If you are curious to know how the collision effect was implemented, read this department of the Hero'south Trail breakdown page.
Idle Flying Behaviour
Before the bat starts following the player, information technology stands all the same. Nosotros want it to fly around in a circle when the player is out of its range, so let'southward practise the following:
-
Get to obj_bat and open its Pace event.
-
Search for the "Else" activeness and drop it below the "If Collision Shape" action.
-
The "Else" action always comes afterwards a condition. Y'all can attach deportment to it, and they will run when the original condition is not true.
-
This ways that whatever actions fastened to this Else will run when the bat doesn't discover the player near it.
-
More detailed caption nearly the Else activity is given in the next department.
-
-
Adhere the post-obit actions to the "Else" activity:
-
"Set up Speed"
-
"Set Direction Variable"
-
Utilize the post-obit settings for the new deportment:
What is Else?
Else is always used afterwards a condition, and other actions can be attached to Else, just like in a condition.
-
When the previous condition is truthful, only the actions attached to that status are executed.
-
When the previous condition is false, only the actions fastened to the Else activity are executed.
This way nosotros tin control which actions run and don't run depending on 1 condition!
So what does the event practice?
If the bat finds the thespian near it, it starts following it. Otherwise, it merely keeps calculation +2 to its direction value, then that it keeps moving in a circle.
Run the game now, keep away from the bat and you will see it keeps flying in a circumvolve!
When the player runs away, the bat starts flying in a circle over again. Nosotros've got our "idle" behaviour fix!
You will detect that the bat doesn't look in the correct direction - it's always looking to the right. Let's change that!
Turning Left/Right
Nosotros're basically going to flip the bat'south sprite depending on whether it's moving left or correct.
By default, the bat's sprite faces right. This means that we demand to flip it in order to make information technology face left.
Permit's do the following:
-
Open obj_bat and add together the End Footstep outcome.
-
This event runs after the Step event (which is when the example moves). We're using Finish Footstep to run actions later on the example has moved.
-
Add the "Declare Temp" activeness to the effect. This is used to declare (create) a temporary variable, which is just usable inside the same event.
-
And then add the following deportment:
-
"If Variable"
-
(Adhere) "Set Instance Calibration"
-
-
"Else"
-
(Adhere) "Ready Case Calibration"
-
-
Use the following settings for the added deportment:
Moving to the left requires a negative 10 speed, which is how we know that the bat is moving left when its 10 speed (that nosotros calculated) is less than 0.
Run the game at present, and you will see that the bat now correctly faces left or correct depending on where it's going!
Defeating the Enemy
Currently you can defeat the bat, however it uses the generic dust effect that was implemented in the enemy parent. We're going to change it up for this bat specifically.
Let'south do the following:
-
Go to obj_bat. This volition have a collision event with obj_sword_attack, but it'll be greyed out because information technology'south inherited from the parent object.
-
We want to redefine this for the bat specifically, so correct-click on it and select "Override Consequence". (This is the same thing with did in obj_baddie earlier!)
-
In the effect, destroy the instance, and and so create a "Smoke Up" particle issue as shown in the image below -- you tin set up its colour to match the look of the bat itself:
This is the same thing the parent's event did; we've only changed upwardly what particle is shown when the bat is destroyed. Looks much nicer than before!
Tip: You don't take to stick to just 1 particle outcome! You can place more than than one"Do Effect" deportment in the event and set up up a combination of different particle effects!
You can also follow the same steps equally the baddie and create a Sequence animation for the bat being defeated.
Shooting Projectiles
We're going to requite extra powers to the bat: it'll shoot projectiles at the thespian. More difficulty, yay!
Let's start by adding an object for the projectile:
-
In the Asset Browser, open "Objects" -> "Game" and select the "Enemies" group.
-
Hither, create a new object and name this obj_bat_projectile.
-
Assign the spr_bat_projectile sprite to information technology, which can be found in the "Bat" group under "Sprites".
-
Open its Parent menu and assign obj_enemy_parent as its parent.
Since this projectile is also a child of the enemy parent, it means that it can hurt the player and it tin exist defeated with a sword too.
Meet how piece of cake it is to create new objects that reuse the same features? That's the power of object parenting!
Projectile Behaviour
We want this projectile to move towards the role player. To implement that, let's exercise the post-obit:
-
Add the Create event to this object.
-
Add together the "Gear up Speed" activity to this event. Set up the speed to 4 (or any other speed you prefer). This is the speed the projectile moves at.
-
Add the "If Example Exists" action to make sure that an obj_player instance exists in the room.
-
To this condition, adhere the "Set Betoken Direction" activeness and make it point towards obj_player.10 and obj_player.y.
-
And so attach a"Fix Instance Rotation" action, which is used to rotate the instance'southward sprite (every bit opposed to its motion direction). Set this to direction so it faces in the same management it'south moving in.
-
direction is a variable that stores the angle of the direction that the instance is moving in.
-
-
Whenever the projectile instance is created, it will automatically move and face towards the histrion.
Particle Trail
The projectile should leave a trail of particles behind information technology every bit information technology shoots towards the player. We can do the following to implement that:
-
Add the Step issue to this object (obj_bat_projectile).
-
Each frame we'll create a new particle, but information technology should be placed randomly around the projectile; then nosotros demand to calculate random values for the particle'south X and Y.
-
Add together the "Become Random Number" action to calculate a random value betwixt -5 and v (you can modify this if yous want the particle to movement more or less); store the result in a temporary variable called relative_x.
-
Add the same action once again (you tin also copy/paste it); this fourth dimension, store the event in a temporary variable chosen relative_y. Now we accept dissever random values for X and for Y!
-
Add the "Do Effect" activeness to this event and create a small-sized "Spark" particle with a green colour.
Ready "Ten" and "Y" to the previously calculated relative_x and relative_y variables, and make sure to enable the "Relative" checkbox for both of them.
Since this particle is being created every stride (or, every frame) it will appear like a trail within the game; information technology volition too be somewhat random and not completely straight, cheers to our "Get Random" deportment:
Wall Collision
The projectile is a kid of the enemy parent, and and then it volition have inherited the obj_collision_parent Collision event. This will stop the projectile from moving through a wall, withal nosotros merely desire it to be destroyed equally soon as it comes in contact with a wall.
Let'southward do the following:
-
Get to the obj_bat_projectile object and open its Events window.
-
Right-click on theobj_collision_parent event and select "Override Upshot".
-
Add the "Destroy Instance" action in this event.
Sword Collision
Now we need to prepare upwards the effect that is shown when the player'south sword hits the projectile:
-
In the same object, override the Standoff event with obj_sword_attack.
-
In this event, destroy the instance and create a small "band" event.
Spawning the Projectile
The bat should shoot a projectile every 1.5 seconds while information technology'southward near the player. This will be achieved using an Alarm event, so permit'south create it:
-
Become to obj_bat and add the Alarm 0 event.
-
Add the "Create Instance" activity to create an case of the bat projectile.
-
Add the "Set Alarm Countdown" action to run this warning over again after 1.5 seconds. Since one 2d has threescore frames, we'll laissez passer in the value 90 for 1.5 seconds.
-
Feel gratuitous to change this value if you desire the bat to shoot more or less frequently!
-
Managing the Alarm
Our Alarm 0 event creates a projectile, then permit's make sure that the event runs when the histrion is in the bat's range.
-
Open obj_bat's Footstep upshot.
-
The actions that we're going to add together below should be attached to the "If Standoff Shape" activity.
-
Add the "If Variable" action to check if Alert 0's value (alarm[0]) is less than 0, significant information technology's inactive.
-
Add the "Prepare Alarm Countdown" action and attach information technology to the new"If Variable" action. Set Alert 0 to 1.
This will tell Alarm 0 to run afterwards 1 frame, as long every bit it is inactive. Nosotros're checking that using a condition to make certain that the alarm is non constantly being called even while it'due south active, as that would crusade information technology to never run.
Stopping the Alarm
The actions we've added above would tell Alarm 0 to run when the bat has found the player, however later on that it would never stop running. We have to brand sure that it stops shooting when the player goes out of the bat's range, and then we'll do the following:
-
Search for the "Set Warning Countdown" action and attach it to the Else action.
-
Set Alarm 0 to -1 to brand it inactive. This makes sure that it stops running.
Run the game at present, and yous will come across that bats beginning shooting projectiles as presently as y'all become near it!
Y'all are also able to defeat the projectile itself using your sword. Should make for some interesting gameplay!
You can change the interval between each shot by going into Alarm 0 and modifying the value in the "Gear up Alarm Countdown" action (which is currently ready to 90 frames, or i.v seconds). If you remember the projectile is likewise fast or as well tiresome, you can modify its speed in its Create effect.
Projectile Destroy Animation
Whenever the bat's projectile is destroyed, whether it's from touching a wall or beingness striking past the thespian, it should play the spr_bat_projectile_splatter animation:
We'll create an object for this that but plays once and then destroys itself:
-
Create an object called obj_bat_projectile_splatter, and then assign the spr_bat_projectile_splatter sprite to information technology.
-
Add together the Animation End issue to it.
-
In this outcome, add the "Destroy Instance" action. This will make sure that the instance is destroyed in one case its blitheness ends playing.
We're now going to create this splatter case when the projectile itself is destroyed, which should be straightforward to implement:
-
Go to obj_bat_projectile.
-
Add the Destroy event -- this runs when the example is destroyed using the "Destroy Instance" activeness.
-
Hither, add a "Create Instance" action to create an instance of obj_bat_projectile_splatter. Create it at the current instance's relative position, inside the "Instances" layer.
The splatter blitheness will now play whenever a projectile is destroyed!
Thespian Hearts
With all these powerful enemies, information technology'south unfair that the player goes down with just one hit. We should let the player to have multiple hearts so they tin strike back!
Adding the Variables
The player needs a variable to store the number of maximum hearts that it tin can take, and the number of hearts it currently has. Let's do the following:
-
Go to obj_player and open up its Create event.
-
Add the "Assign Variable" action, which we'll employ to create new variables.
-
Create a variable with the proper name max_hearts and a value of iv.
-
Then click on the plus sign to add together a new variable. Proper noun this hearts and set its value to max_hearts.
We're setting hearts to max_hearts, which means that the hearts variable will start with the same value that was assigned to max_hearts (which is four). Every bit a result the role player will showtime with four hearts, but you can change that value to make the game easier or harder.
Drawing Hearts on HUD
We'll now draw the hearts on the HUD. This is done using the Draw GUI event, which is used to draw sprites/text on the "GUI Layer". This layer is automatically drawn above the game and stays on-screen, no matter where the in-game camera moves.
Existing Describe GUI
-
In the Asset Browser, open "Objects" and expand the "Game" group.
-
Open the obj_manager object. This object "manages" our game in the background, and it too draws the HUD.
-
Open up the Draw GUI event.
-
This upshot volition already have some actions, which handle cartoon the histrion's coins. The existing deportment do the post-obit in the given order:
-
If the thespian exists in the room,
-
Drawsthe coin HUD sprite
-
Setsthe font
-
Drawsthe thespian's coins value (past writing obj_player.coins)
-
-
Nosotros'll at present add together another action to this consequence and so it draws the player'south hearts.
Drawing Hearts
Let's exercise the post-obit to draw the player's hearts:
-
Add the "Draw Stacked Sprites" action to the Depict GUI result. Brand sure it is attached to the existing "If Instance Exists" condition.
-
This action draws the same sprite a specified number of times, either horizontally or vertically (as specified in the options).
-
Set the "Sprite" to spr_hud_heart. You lot will find this sprite in the "HUD" group nether "Sprites".
Utilize the following settings for the balance of the arguments:
You can change the X and Y values if y'all want to position the hearts differently.
If you run the game, you will meet the number of hearts on-screen that you specified in the max_hearts variable:
The player still goes down with one hitting, so permit'southward make it and so the hearts get down on each hit.
Injure Knockback
When the player touches an enemy, information technology should go into a "injure" state and go knocked back. It should also lose a heart.
Permit'southward practice the following:
-
Get to obj_player and open its collision consequence with obj_enemy_parent.
-
This event runs when the player touches an enemy. Currently information technology stops the player, changes its color to blood-red and sets Alarm 0, which restarts the room after some frames.
-
You will find an "Assign Variable" action hither which sets the actor'southward move_speed to 0, so that information technology can't motility.
-
We don't need this action anymore, so click on the X push button to delete it.
Let'south now utilise knockback motilityto the player and reduce the hearts value.
-
Make certain that all the new deportment are attached to the "If Variable" activity, just like the existing actions in this event.
-
We'll add the post-obit actions to create a knockback effect:
-
"Ready Betoken Management": This will be used to set up the thespian's direction towards the colliding enemy.
-
"Fix Management Variable": This will be used to contrary the direction so the actor ends upward facing away from the enemy.
-
"Ready Speed": This will set the speed of the player and then it moves in the given direction.
-
-
Finally, nosotros'll add the "Assign Variable" action to reduce the hearts value past adding -1 to it.
Now whenever the player comes in contact with an enemy, it volition receive a knockback and its hearts value will become downwardly. However, this consequence calls Alarm 0 which currently restarts the game, and so nosotros need to brand sure that that only happens when the histrion is fully defeated.
Defeating the Player
Let's now take care of what happens when the player runs out of all hearts.
Defeated Object
Nosotros'll create a new object for the player's "defeated" land. Let's practise the post-obit:
-
Under "Objects" -> "Game", create a new object called obj_player_defeated.
-
Assign the spr_player_defeated sprite to it, which can be found in the "Player" group under "Sprites".
-
Add the "Animation Terminate" event to this object (this event tin be found nether "Other").
-
In the event, add the "Restart Room" action to restart the room when the animation ends.
We now need to create an instance of this object, which will play the defeat animation and then automatically restart the room.
Transition to Defeat
We'll now handle stopping the player after its knockback, and restarting the game if the hearts are all gone.
-
Open up the player'southward Alarm 0 event and remove any existing actions so that it'due south empty.
-
It used to restart the room subsequently existence hit simply at present it should only practise that if the histrion has run out of hearts.
-
-
Add the "Prepare Speed" activeness to reset the speed dorsum to 0. This will stop the thespian's knockback.
-
Add an "If Variable" status to bank check if the hearts variable is greater than 0, significant that the player still has some hearts remaining.
-
If that is true, ready the case's colour back to white past attaching the "Gear up Instance Colour" action to it.
-
Add an "Else" activity which volition run when the player has run out of hearts.
-
Attach "Create Example" to "Else" to create an instance of obj_player_defeated, and then destroy the player instance itself.
Hither is what your result should await like at present:
This will effectively "transition" the player into its defeated state once the hearts value is at 0.
If yous run the game at present and run into an enemy, you will come across the role player getting knocked back and losing a heart. Once all hearts are gone, the defeat animation plays and the level restarts.
Summary
Our baddie now has a slap-up defeat animation cheers to Sequences, nosotros have a make new enemy that flies in circles and spits acid at the role player and a solid health and knockback system for the player. Our game's on the side by side level already!
Here are some of the things we learned in this part:
- You tin assign path assets to Path Layers in a room, and assign the path asset to an case for use in its events
- You lot can create your own animations in Sequences, and play them using the "Create Sequence" activeness
- Yous can use Alarms to perform actions at a later time, and to run repeated deportment (similar shooting projectiles)
- You can utilize "If Collision Shape" to cheque if an object is intersecting with a shape
- You can create particle trails in the Stride event
Enemies take gotten too much attention though! In the side by side function, we'll piece of work on player upgrades and power ups, and as well give it a rechargeable free energy shield!
-> Continue to next office
-> Go dorsum to index page
Source: https://gamemaker.io/en/tutorials/heros-trail-dnd-4
Posted by: moradoyoulty.blogspot.com
0 Response to "How To Stop Sprite Animation In Game Maker Dnd"
Post a Comment