Game Development Reference
In-Depth Information
If you need more than one ship to have a cannon (for example, when enemies have gun
mounts as well), you'll notice a problem with the current setup: only one of them would carry all
cannons! This happens because Game Maker's actions address all instances of
obj_cannon
at
once, rather than the one we've created specifically for that ship. To fix this, we need to keep track
of the cannon instance each ship creates for itself.
To Make Each Spaceship Keep Its Own Cannon
1.
First, open
obj_cannon
, select the
End Step
event, and click
Delete
.
2.
Now, open
obj_spaceship
, select the
Create
event, and delete the
Create Instance
action. Include an
Execute Code
action and insert the following lines:
1:
{
2:
cannon = instance_create(0,0,obj_cannon);
3:
}
This script snippet stores the ID of the instance in a local variable called
cannon
. We
can now use this variable to reference only the cannon we've created for this ship.
3.
Because only the spaceship knows which cannon is its own, we'll make it responsible
for keeping it in the correct place. Add a
Step
,
End Step
event and include an
Execute
Code
action. Insert the following lines:
1:
{
2:
cannon.x = x;
3:
cannon.y = y;
4:
}
4.
Now, open the
Destroy
event and remove the
Destroy Instance
action. Include an
Execute Code
action and insert the following lines:
1:
{
2:
with ( cannon ) instance_destroy();
3:
}
5.
If you wish to test this, place a few more instances of
obj_spaceship
in the room. Keep
in mind they're all controlled by the same mouse and keys! Maybe you could make a
game of that…
Result:
Reference/Result/mouse_aim_and_fire2.gmk
Mouse Cursor
Do you think the Windows mouse cursor is rather dull? Or do you need a game where the mouse
cursor consists of crosshairs to shoot with? Here is how you can replace the default mouse cursor
with a sprite of your own.
Search WWH ::
Custom Search