Gamemaker Code - Useful Things: Game Play
Gamemaker Code - Useful Things: Game Play
Rather than drag and drop actions, you can use the more powerful code. The “execute a
piece of code” action looks like this:
You can type code into this window or paste to it from help or elsewhere. Note how the
colour changes when Gamemaker recognises something, for example:
If you don’t get the colour you expect, you have made a typing error
Game play
x Its x-position.
y Its y-position.
Example:
In a keypress<space> event
x=100
y=100 jump to position 100 100 when you press space
instance_change
instance_change (obj,perf) Changes the instance into obj. perf indicates whether to
perform the destroy and creation events.
Example:
instance_change (object0,false) change into object0 without performing the destroy and
creation events
instance_destroy()
Destroys the current instance.
User interaction
mouse_x X-coordinate of the mouse.
mouse_y Y-coordinate of the mouse.
Example:
In the step event
x= mouse_x
y= mouse_y the object moves with the mouse
Game graphics
image_scale A scale factor to make larger or smaller images.
example :
image_scale =1.8 makes the sprite 1.8 times bigger
draw_sprite
draw_sprite (n,img,x,y) Draws the sprite.
Example:
In the draw event
draw_sprite(sprite0,-1,x,y) just draws the sprite as normal
Language overview
&& ||
logical and, or
example:
if(x<0 && y<0) means if x is less than zero and y is less than zero
for
does something a number of times
example:
for (i=0 ; i<10 ; i=i+1) instance_create(x,y,object0) creates 10 object0’s
with
this allows you to do something with all instances of an object
example:
with (object0) instance_destroy() destroys all object0’s
//
// this is a comment, the line following // does nothing
Computing things
random(x)
random(x) Returns a random real number between 0 and x. The number is always
smaller than x.
point_distance
point_distance (x1,y1,x2,y2) Returns the distance between point (x1,y1) and point
(x2,y2).
point_direction
point_direction (x1,y1,x2,y2) Returns the direction from point (x1,y1) toward point
(x2,y2) in degrees.
in a keypress event
for (i=0 ; i<10 ; i=i+1) instance_create(x+10*i,y+10*i,object0)
In a keypress event:
instance_create(random(400),random(400),object0)
In the step event:
if (x<0) hspeed=5
if (x>300) hspeed=-5
More reading
Go to gamemaker help contents and look at The Gamemaker Language, there are heaps
of useful things there.