Your first game! Part 4: Parent objects

Posted on: March 31, 2015 at 6:39 pm,

a5

Part 1 | Part 2 | Part 3 | Part 4

…More rocks!

In the original Asteroids game, when you shoot a larger asteroid it breaks down into smaller asteroids. In this part we’re going to look at replicating that and at the same time learn about how parent and child objects can be used to save you a lot of work in GameMaker.

The first thing we’re gonna do is make yet another new sprite for our game. This time a sprite for a smaller, 32×32 asteroid. I called mine spr_asteroid_s. Make the sprite in the same way you made spr_asteroid in part 3. The only difference this time is the sprite should be 32×32 instead of 64×64. (or around half the size in general if you used a different size for your first asteroid.)

Make sure it has exactly 3 frames with varied artwork like the example below, because we’re going to be using the same code that picked a frame for obj_asteroid with our smaller asteroids.

sprite2

Next up, go ahead and create a new object called obj_asteroid_s. Assign it the sprite that we just created and then where you see the word ‘Parent‘ click the drop down list icon and select obj_asteroid to make this object a child of obj_asteroid.

parent

Once this is done, this object will inherit any code run by obj_asteroid and will be affected by any code that would affect obj_asteroid. To see how this works, go ahead and place a few of obj_asteroid_s around your game room and run the game. You’ll see that they behave exactly as your larger asteroids do, just with a different (smaller) sprite. They fly around, react to being shot and can also kill the player.

Now remove any test instances you just placed of obj_asteroid_s and open up obj_asteroid, it’s time to make the bigger rocks split into smaller ones!

Pages: 1 2