The first thing we gonna use is the parent system that is available in GameMaker.
For that we gonna create a new object and call it "objPlayerParent".
Parents are used to add the same events, attributes and even sprites to various object and/or to combine those. I am gonna show you how to do this.
For objPlayerParent, do not add any sprite as image. Just leave it blank.
Run Forest, Run
Now, we gonna add movement to this object, so the player can move it around.
For the sake of this tutorial, we gonna keep it simple. Add the following events and create a script in the event, adding the code provided:Key Press "Left"
speed = 10;Key Press "Right"
direction = 180;
speed = -10;Key Release "Left" and "Right" (1 Event for each)
direction = 0;
speed = 0;Intersect Boundary
speed = 0;
Now what this code does, is to simply allow the player to move left and right and stops him when he goes outside the room area. Please note, if you use views, you might wanna use a different approach.
But this basic setup is enough for our example.
Now, add a "Create Event" and add a new variable called "weapon", via the drop action.
Leave it at 0.
Add a second variable, called "canShoot".
Change it to 1.
We need those variables for 2 things.
"weapon" is gonna tell our parent class which weapon the player is using. So this way we can have different behaviour and images for the weapons particles.
The "canShoot" variable is gonna be used to limit the players ability to shoot. Otherwise the player might just unleash thousands of shots each step.
To reset it, create a new event for Alarm 0 and change "canShoot" in this Event back to 1.
Do not restart or start the timer anywhere for now, we gonna code this section next.
Somebody shoot him
Create a new event for a key press of "space". Add a script.
We are gonna add the following code:
if ( canShoot == 1 )
// Disable shooting
canShoot = 0;
// The timer reenables the ability to shoot
alarm = floor(room_speed / 4);
// Create new shot and send it into the direction of the mouse
new_shot = instance_create( x, y, weapon);
new_shot.speed = 20;
new_shot.direction = direction;
( As you notice, there is a 0 missing in the code above inside the brackets of alarm. This is not due to my bad coding, but Steam messing it up when displaying the code.)
You should be able to understand this code. What it does is simply to look if shooting is enabled. If yes, it is blocking shooting and resetting the alarm to ~7 steps. This means, our player can shoot each 7 steps one bullet. It then creates and speeds up the projectile and sends it into the current direction of the player.
Notice how we use the variable "weapon" here. So far this script would crash, because weapon is nil right now. There is no object behind it. But we are gonna change this in our next steps.