Your first game! Part 2: Shooting StuffPosted on: March 10, 2015 at 1:01 am,
We have a bullet, now how do we shoot it? Let’s think through the logic. When we press a key on our keyboard, lets say the space bar, we want to create obj_bullet and make it move in the direction our ship is facing. Preferably at high speed!
So lets open up obj_player and add the press <Space> event! This event is different from our other keyboard events because we’re picking it from the “press” menu. While before we wanted to trigger our keyboard events every frame that the key was held, this time we only want to trigger the event on the frame the key is initially pressed down. Otherwise we’d shoot an infinite stream of bullets, 60 of them per second!
It’s easy to identify as our events list this event as “press <Space>” instead of simply <Space> like our other keyboard events show as <Left> etc.
Drag an “Execute Code” action into this event to open the code editor. Now the first thing we want to do is create a bullet whereever our player is.
Just as I described “instance_destroy()” earlier which destroys an instance, instance_create will create a new “instance” of an object! We need to pass along 3 arguments to this function. The x and y coordinates where we wish to spawn the object, and which object we are creating. The object we want to spawn an instance of is “obj_bullet” and we are spawning it AT our player, so we just use x and y!
If you run the game at this point you’ll find you’re able to fly around dropping little white pellets all over the screen. We’re half way there. We can create the bullets and now we just need to make them actually fly.
We need to adjust this line of code a little, to the following:
bullet = instance_create(x,y,obj_bullet);
What this does is stores the instance ID of the instance of obj_bullet we just created, into a variable named bullet. This is important because it allows us to reference that specific bullet we just made and make changes to it, without affecting other bullets that might be in the room!
You can make changes to objects and instances from actions inside other objects in various ways. One of which is to use that objects name followed by a “dot” like this:
obj_bullet.speed = 5;
This would change the speed of every “obj_bullet” to 5. Useful to know! but not so useful here. We want to affect a specific instance, which we can now do because we know the instance ID of our recently spawned bullet!
so go ahead and add this line:
bullet.speed = 15;
This will change the speed value of the bullet we just created. Now we just need to make the bullet’s direction match our player’s current facing. If you remember we turned our spaceship sprite around using image_angle, so we can use this to set the direction of movement for our bullet:
bullet.direction = image_angle;
Which should leave our code looking like this:
bullet = instance_create(x,y,obj_bullet); bullet.speed = 15; bullet.direction = image_angle;
We’ve made a bullet, set that bullet’s speed to 15 and pointed it in the right direction. Time to run the game!
In the next part we’ll look at building some actual targets for us to shoot at!