Your first game! Part 2: Shooting Stuff

Posted on: March 10, 2015 at 1:01 am,

a3

Part 1 | Part 2 | Part 3 | Part 4

Drifting to a stop

So now you hopefully have a spaceship that can drift endlessly through space. Poignant perhaps, but it’s time to add some more. Before we get into firing projectiles we’re going to make a quick change to our player, to make him slowly decrease in speed over time. This is just done as a subtle aid to the player who otherwise has no way of slowing down other than to perform a 180 and press forwards, which while also trying to shoot things can be difficult if you have no friction at all!

Doing this will also allow us to understand one of the most important object Events in GameMaker.

So open up obj_player by double clicking on it and add the Step event.

step

the step event triggers every frame or “step” of your game. Remember how in part one we discussed room speed and set our game to run at 60 frames per second? Every single one of those frames is a “step” inside of gamemaker and every object will trigger its step event. As you progress as a developer you will probably find yourself making a lot of use of the step event! You should be careful what actions you put in this event as with a room speed of 60 any actions here will be carried out 60 times every second!

There is also the begin step and end step event but those aren’t important to us right now, so make sure you selected the regular “Step” event.

What we’re going to do is reduce our overall speed every frame, while it is above zero.

In order to do this, with the step event selected, drag in an “Execute Code” action from the control tab as usual, and type the following:

speed = max(speed - 0.01, 0);

now we could have just written “speed = speed – 0.01” but with that happening every frame, it would eventually give us negative speed! We don’t want to end up going backwards. The “max” function allows us to pick the highest value from a list of values. So inside the brackets we give it the option of either “speed – 0.01” or “0”. If our current speed reduced by 0.01 is greater than zero, then that will become our new speed, otherwise our speed will simply become zero (which it probably was already.).

If you run the game now you’ll see our spaceship is able to cruise to a subtle, gentle stop.

stop

Lovely. Now lets get on to the whole projectile stuff!

Pages: 1 2 3