Since many questions in the Game Maker Forum are asked multiple times, I decided to start writing them down in a FAQ. If you have any questions that need to be answered, mail me and I will try to answer them and add them to this FAQ.
Please search through the FAQ before asking any question.
FAQ started 2002-05-06
How do I make an object follow another, e.g. like a lock-on missile?
(Answer by Chuck)
I am going to refer to the three objects as 'player', 'missle', 'enemy'. First you put a create event in the 'enemy' object. (In here Ihave a set path for it to move so you can successfully test the missle). Then, in the actions window, you create a variable, I call it 'homing', and set it to 0. Back to the events window you create a left click event. In the actions you create an 'if a variable has a value' and type 'homing' for the variable, '0' to the value, and the operation is 'EQUAL'. Put a start block and then a 'create an instance of an object'. Make it so it applies to 'player'. The object is 'missle' and the x and y coordinates are "x: player.x" and "y: player.y". Then set an alarm clock for one step and alarm 0. Then place a 'set the value of a variable' and have homing's new value be 1. This makes sure that only one missile can be shot at a time until homing is set back to 0 (which will be done when the enemy is destroyed). Put an 'end block' in there. Create alarm 0. Put in a 'move towards a point'. Make it apply to 'missle' and have the x and y coordinates be enemy.x and enemy.y. If the enemy sprite is a little bigger, adjust the coordinate so the missile heads towards the center. For example, enemy.x+10 and enemy.y+15. Also set the speed, at least one more than the enemy's moving speed. Set an alarm clock still in the actions window and set it for 1 step to go to alarm 0. This makes a continuous loop for this alarm. This is the main thing that you dont want to mess up. If it didnt loop the missle would go to the last known coordinates of the enemy. This way it continually updates the enemy's coordinates and follows it.
The 'player' object doesn't need anything special for this idea. Maybe just some actions to move it around.
In the 'missle' object all you need to do is put a collision event with 'enemy'. In the actions you set the value of a variable, homing, back to 0. then put in to destroy instances. Destroy yourself and destroy 'enemy'. Then it's probably a good idea to create an instace of an object which is 'enemy' so you can try it again. The room you create the only objects you put in is one 'enemy' and one 'player'. That's it. Run the game and left click on the enemy and the missile sprite will come out of the player and "lock" onto enemy and destroy it.
There is also an example of this in examples.
How do I make my energy bar / health bar follow the player as the view scrolls?
As the view moves, its coordinates in the room are updated
in the variables view_left and view_top.
The zero inside the brackets stands for view number 0. If you use other views, change the number to the view you are using.
When drawing your object, you need to add the view coordinates to the coordinates that the object should have on the screen. An example:
I want to draw the sprite sprEnergy at location (20, 50) all the time, but it should follow view 0 around:
draw_sprite(sprEnergy, -1, 20 + view_left, 50 + view_top);
How to make an object rotate in a circle?
Here is a suggestion on how to solve this.
First, place the object in the room, in the center of the circle you want it to rotate around.
Then, in the CREATE event of the object, enter:
centerX = x;
centerY = y;
radius = 80; // The radius of the rotation
angularSpeed = 20; // The rotation speed. Make it negative to rotate clockwise.
currentAngle = 0; // Starting angle is straight to the right.
Now, in the STEP event, enter:
currentAngle += angularSpeed;
currentAngle = currentAngle mod 360;
if (currentAngle < 0) currentAngle += 360;
x = centerX + radius * cos(degtorad(currentAngle));
y = centerY - radius * sin(degtorad(currentAngle));
And that is it. Now the object should rotate around its original location in the room.
How can I place an object (B) at a certain distance (dist) and direction (angle) from another object (A)?
I would solve it with trigonometry. Like this:
B.x = A.x + dist * cos(degtorad(angle));
B.y = A.y - dist * sin(degtorad(angle));
Note: degtorad (Degrees-to-radians) converts angles from degrees to radians, which is necessary for cos and sin.
How do I make an object or other graphic follow the screen when a view scrolls?
The coordinates of the views are stored in the variables
view_left[n] and view_top[n], where n is the number of
the view. Usually, if only one view is used, it is view 0 that is used, and
then the variables view_left and view_top should be used.
So, in order to draw a sprite that follows the view, just add the view coordinates to the sprite drawing command.
draw_sprite(sprite_index, -1, x + view_left, y + view_top);
How do I make the game scroll along as my player moves?
GM 4.1: In the room window there is a button called "Change the Veiws". This will open up the views definition window. Click the "Enable the use of Views" checkbox. Now you should see some settings for the views.
Make sure "View 0" is selected. Check "Visible when room starts". Enter the requested size of the view.
The "Hor border" and "Vert border" settings determine how close to the border of the screen the player object can move before the view scrolls with it.
Set the "Object to follow" to the object that the view should follow. Game Maker then makes sure that the selected object never moves out of view (unless it is moved out of the room).
GM 4.2: In the room window there is a tab called "Views". Select this tab. Here you will find the same settings as in GM 4.1 (see above).
How do I play MP3 audio files in Game Maker?
Just add them as any other sound resource. When adding MP3
files to games in Game Maker versions prior to 4.2, you will have to select
the file filter "All files" in the Load Sound dialog in order to be
able to select MP3 files.
In GM version 4.2 the MP3 files are added to the normal sound files filter.
NOTE: There are problams playing MP3 files on some systems. I do not know why.
Last updated: 2003-02-05