Game Development Reference
In-Depth Information
Collectables
The next thing we need to do is make the collectable objects work. For this, we need to
make the collectables spawn in the game room when none already exist. We will make
them spawn in random positions. Then, we need to make it so the player is able to collect
them and score points. The collectable then needs to be destroyed and a new one created in
another place.
Let's start with creating the actual objects in the room.
To do this, we are going to use the player step event to check whether any collectable ob-
jects currently exist inside the room. If they do, we don't need to do anything; if not, then
we need to create one at a random position.
For this, we will use the instance_number function. This function returns the number
of instances you enter as an argument. In this case, we will be checking for
obj_Collect . We want to check if there is less than one instance in the room.
This is the code we need to put beneath the movement code:
if (instance_number(obj_Collect)<1){
}
The preceding code checks the amount of obj_collect instances in the room to see if
there is less than one. The <1 controls the amount we are checking for. If we want to check
to see whether there are more than five, for example, we will write >5 instead.
We need to tell the game what to do when there is less than one object left. We want to cre-
ate an instance of obj_Collect at a random position. For this, we will use the in-
stance_create function that creates objects, and the random_range function, which
returns a random number between the ranges that we set.
This is the code:
if (instance_number(obj_Collect)<1){
in-
stance_create(random_range(64,room_width-64),random_range(64,room_height-64),obj_Collect);
Search WWH ::




Custom Search