Game Development Reference
In-Depth Information
}
As you can see in the preceding code, we use random_range for both the x and y argu-
ments of the instance_create function. The reason we are using a range is to make
sure that obj_Collect doesn't get created inside a wall on the edge. You may also no-
tice two variables named room_width and room_height . These return the width and
height of the current room in pixels. So, by entering room_width-64 , we are simply
entering a value that's 64 pixels back from the entire width of the room, which gives us
our 64 pixel border.
There is one more thing we need to do before moving on. Due to values in a computer
never being truly random, GameMaker uses what is called a seed to produce its numbers.
This seed is always the same when testing the game, which means it will produce the
same result in the same order every time the game is tested. To solve this, we can use the
randomize function, which will randomize the seed that GameMaker uses.
In the create event of the player, we just need to put randomize(); on a new line and
the problem is solved.
We can now run our game and a single collectable object should be created.
Currently, the player still can't actually collect the objects, so let's fix that now. Add a col-
lision event with obj_Collect and drag in a code block.
We want to make the game add one point to the score and destroy the collectable object.
To do this, we will be using a with statement and a built-in variable called other . The
other variable is one that holds the other instance's ID in it at all times. This can mean
different things at different points in time. When we use it in the collision event, other
will be equal to the instance's ID that we collide with. However, inside a with statement,
the other variable will be equal to the object's ID that is executing the with statement.
The with statement is used to essentially go inside another object and execute code from
that object. By inserting ( obj_Collect ), we are essentially telling the game to execute
code from within that object instead of the current one. Within the brackets of the with
statement, we are going to use the instance_destroy function to destroy the object
we have collided with. If we didn't use the with statement, then the in-
stance_destroy function will destroy the player object.
Here is the code:
Search WWH ::




Custom Search