Featured Post

Dodgeball Game Maker Tutorial

This will be a series of posts that will teach you step by step how to make a simple arcade game using Game Maker Studio 2. The final game w...

Thursday, September 17, 2020

Dodgeball Tutorial: Creating the enemy

Subfolders for creating enemy object and enemy sprite

Just as we did for our player, we need to create an object and a sprite for our enemy, each one, in his folder. As we will have more than one enemy in our game, we can create a subfolder called “enemies” under the “sprites” folder and our “objects” folder. To do this we have to right click were we want to create the subfolder and then select the “create group” option.

Once the subfolders are created, we can create the enemy object and the enemy sprite under that folders. We will name them “oEnemy” and “sEnemyMedium”. Import the sprite for the enemy medium called "enemy39av3" (under the characters/enemies folder) and then assign the sprite to the object, just as we did for our player.

Adding functionality to our enemy

Now we can Place our enemy inside the room, but it still does nothing, so first let's add some code to our enemy. To start things we will create a "create" and "step" events for our enemy obect just as we did for the player. In the create event, we will add two object variables, this variables are visible and shared for all events of this object and they will store the movement information.


_direction = random(360);
_speed = 3;

We are going to use this variables for the enemy movement. The direction variable represents where the enemy is moving and the speed how fast is it moving. Direction it's a number between 0 and 360 as it's in degrees. The speed tells us how many pixels the enemy moves every frame in a given direction.

Now it's time to code the step event using the variables defined in the create event. We want that our ball move in a random direction that's calculated just when the object is created in the create event. After that, the step event will be using that information to actually move the enemy. But we have to control if the ball is going outside the room, because we want it to bounce and stay in the room. So the code to achieve this look like this:


var speed_x = lengthdir_x(_speed, _direction);
var speed_y = lengthdir_y(_speed, _direction);
	
if x + speed_x < 0 or x + speed_x > room_width {
	speed_x *= -1;	
}
	
if y + speed_y < 0 or y + speed_y > room_height {
	speed_y *= -1;
}
	
x += speed_x;
y += speed_y;

_direction = point_direction(0, 0, speed_x, speed_y);

The previous bloc of code in the step event of our enemy does the following:

  1. First we get how many pixels in every axis (x and y) we have to move given this direction and speed. Game Maker has a built in function that calculates this for us, so we can use lengthdir_x(); and lengthdir_y(); to calculate that.
  2. We have to check that the enemy don't go outside the room, so if this happens we multiply the speed x or y by -1 to move in the oposite direction. It's like making the enemy bounce once it arrived at the end of the room.
  3. We update the current position, adding the value of the speeds.
  4. We update the direction because if we have bounced, the direction has changed and we need to update it for the next step.

At this point our Enemy object should look something like this:

Now if we run the game we will see our enemy moving and bouncing when it arrives to the edge of the screen.

Player interaction with enemy

Let's make the player die if it touches the enemy. We are going to do this with a "collision" event. Open the player object and create a new collision event. When selection the collision event, we have to say the collision object which is the oEnemy on this case:

For now, we will only put one line of code on this event:

instance_destroy();

Calling this function will destroy the object, so when the collision event is triggered because the engine has detected that the player object and enemy object are collisioning, the code inside the event will run and the player will be destroyed.

If we run the game now, we will see our enemy moving and we can move our player with the mouse. If the enemy touches us, we will see our player disappear as we make the instance_destroy(); for the player on the collision event.

On the next chapter we will create the rest of the enemies.

No comments:

Post a Comment