Introduction to Families for Construct Classic

1

Stats

3,683 visits, 5,845 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 24 Jul, 2011. Last updated 19 Feb, 2019

What are Families?

Families are a way of grouping together objects that have similar attributes or purpose. To illustrate, let's think of the classic game BreakOut. This game involves destroying different types of coloured bricks with a ball. The bricks could have many similar attributes, but with maybe 1 or 2 differences. So for instance, one brick type would have different colours, but would all be destroyed after 1 hit, and give a score of 10. Another type of brick may need to be hit twice to be destroyed, and would give a score of 20. Some brick types would drop a bonus when hit, and so on.

Without Families, we would have to check every different type and colour of brick when the ball hits it, and act accordingly. Even if you have only 20 different bricks, that's still a lot of very similar code to write for each brick - very boring!

The answer is to put the bricks into Families. Then we need only write code once that checks the Family for collision, rather than all the individual bricks.

To show this in action, let's create a (very) simple BreakOut game.

I'll (very) quickly go through the parts needed to create our sprites for the game, but don't worry - the .cap is available at the bottom of this article for you to download, and is fully commented.

Break Me Up - b4 u go go !

Start a new DirectX Project.

Step 1 - Making Bricks

Insert a sprite to represent our brick. Make it any colour you choose.

Ok the Picture Editor and click anywhere on the Layout Editor to place the brick.

Set it's size to 32x16 and make it Solid - this is under Groups/Attributes.

Make it's name a bit more easier to remember. I called mine 'brick_white', 'brick_blue' etc.

Now we're going to add it to a Family!

Click on the Add option under Groups/Families.

Select the 'Blue' Family and click OK.

That's it for brick 1.

Now repeat the Making Bricks section for several more bricks.

When you've finished, you can create a pattern on the Layout Editor to use as our 'test level'.

Create sprites for both the bat and the ball. I made the ball 16x16 and the bat 64x16. The bat should have the 'Solid' attribute ticked. Rename the sprites as 'bat' and 'ball'.

Add the 'Ball' Behaviour to our ball sprite. Click on the ball sprite, and click on the Add Behaviour option in Properties.

We also need walls on the top and sides of our game area, so create a 'Tiled Backgound', make it 'solid', and stretch it so it fits along the top of our game area. Do the same for both sides. Rename as 'wall'.

Insert the 'Mouse & Keyboard' object as we are going to use the mouse to control our game.

Checking for Collisions

We are now in a position to add our code.

I won't go through all of the code (unless asked) as it's available in the .cap and commented.

What is more important is how we use the Family to check for collisions.

If we didn't use the Family option, then the pseudo code for each brick could be something like the following:

+ On collision between Ball and brick_white

-> Destroy brick_white

-> Add brick_value to Score

-> subtract 1 from number_of_bricks (used as a counter)

It doesn't seem a lot yet, but having to do this for lots of different bricks gets boring quickly.

Instead, we can just say:

+ Blue: On collision between Blue and ball

-> Blue: Destroy

It really is that easy. The system keeps track of which member of the Family we collided with and destroys it for us.

Family Variables

If we want our game to be able to allow different bricks to need different number of hits before they are destroyed, then we are going to need to use variables in order to keep track.

Construct allows us to create global and private variables directly for objects. But as mentioned earlier, if we had to create a variable for all of our different bricks individually, it would be a nightmare.

Luckily for us, we can create variables for every member of a Family automatically.

In Layout view, click on the Project tab at the top and notice the option 'Manage Families'. Click on this. Click on the 'Blue family and notice that it shows us all the current members of the family on the top-right pane.

Below that, are 3 tabs, the currently active one being 'Private Variables'. Lets add a private variable for our family call 'NumOfHits and set it's default value to 1.

Now we just need to add a new brick (make it stand out a bit) and set its properties as before, and add it to our Family. While we're changing it's properties, set it's 'Private Variable - NumOfHits' = 2.

Now we just need to amend the collision code.

Add the code below, either by removing the old collision code, or just amending the current code:

What this says is:

If any member of our 'Blue' family collides with the ball, subtract 1 from that member's value for 'numofhits', and if 'numofhits' is zero, destroy.

And that's it. We can now give our blocks different settings, if required.

This was a VERY simple example, but can easily be expanded to make use of Scores, different types of bricks, Lives etc, and of course, MUCH better graphics.

This was a very quick introduction to Familes. Any questions, just ask.

Cap is HERE.

zen

  • 0 Comments

Want to leave a comment? Login or Register an account!