Your first game! Part 3: CollisionsPosted on: March 26, 2015 at 7:27 pm,
Now that we have the sprite, as usual we go on to make the object! So make a new object called obj_asteroid and assign it to spr_asteroid just as we’ve done with our bullet and player previously. Now currently if we we’re to place these in the game and run, they would animate as the sprite contains multiple frames. We don’t want this, so in order to prevent it we will set our object to animate at speed 0 when it gets created.
So we want to do something to an object when it gets made, therefore let’s add the create event!
drag in an execute code action and add the following
image_speed = 0; image_index = irandom(2);
image_speed refers to the number of frames animated per step. So if your room speed is 60 and an object’s image_speed is 1 (which it is by default) it will animate at… 60 frames a second. If you were to set image_speed to 0.5 or 1/2 it would animate at 30 frames per second. At an image speed of 2 it would skip every other frame, etc.
By setting it to zero, the sprite never moves from its current frame unless we specifically tell it to. So… we specifically tell it to, with the second line of code. image_index stores the sprite frame that the object is currently on. We want this to be a random number between 0 and 2 (since the first frame of a sprite is technically frame zero.) Luckily GameMaker has plenty of useful functions for generating random numbers. “irandom()” is a function you can use to generate a random whole integer (hence the “i”) between 0 and whatever number you provide in brackets. So irandom(2) gives us either 0, 1 or 2 at random.
now open up the room editor and using the objects tab, place a bunch of obj_asteroid into the room. Giving you something like this:
The first frame of spr_asteroid is shown by default when placing the objects into the room. If you were to run the game h0wever (and feel free to do so!) you will see that each sprite is selected randomly during the game. They won’t move however so let’s change that.
Open up the execute code action we just placed into the create event again, and add the following, just underneath our previous code:
direction = irandom(360); speed = 0.5 + random(2);
just as before we’re using functions to generate random numbers. This time we’re generating a random direction and speed for the asteroid as it is created. Our direction is set to a random value between 0 and 360 degrees. Our speed will be set to a minimum of 0.5 plus a random value between 0 and 2. Notice that we’re using random() for this line and not irandom(). irandom generates a random whole integer, whereas random generates a random real number which can include fractions. So instead of being either 0, 1 or 2, we generate a much larger number of possibilities such as 1.75452, 0.895 and so on but still between 0 and 2
So now our asteroids will fly around, we just need to make them wrap around the screen by adding the same action we used for our player. Add the Outside Room event and Wrap action to obj_asteroid just as we did for obj_player.
Now to actually deal with collisions!