< All Topics
Print

Unit 7- Spawning & Destroying Objects


Items In This Unit

 

  • Spawning & Destroying ObjectsTutorial

The Spawning & Destroying Objects Tutorial

Creating or spawning sprites as well as destroying sprites are the foundations of video games. Think about platformer games, for example, you have enemies and obstacles you can destroy. Even the main player dies. This lesson will teach you how to create/spawn and destroy sprites. 

To demonstrate object spawning and destroying we will create a simple game we will call “bug hunt”. In the process, you will also extend your sprite drawing capabilities by drawing additional frames for the bug sprite to simulate animation. The objective of the game is to never allow more than 10 bugs on the screen at once.  Let’s start.

 

Step

Instructions

1

Create a new game (project).

2

Create a new scene. Call it “Level 1”.

3

Create a new sprite called “Bug” with 2 frames that looks like the one below.

4

Now go to your event sheet. Add a few events by clicking on “Add new event”

5

Next, let’s create new bug objects randomly on the viewable part screen every 2 to 8 seconds. The event should look like the following.

To create the above, first, click on “Add condition” on the top row. Now we will set up the timer.

Click on the “Other Conditions” tab

Select “Timers and time”

Select “Value of scene timer”

Enter the following for the Scene Timer:

   Time in seconds: RandomInRange(2,8)

   Timer’s name: “spawnrate”

Click on OK.
Now you should see the left side of the event populated as follows:

Click on “Add action” on the right side of this event. Next, we will create the Bug objects at a random location on the screen.

Click on the Bug object.

Select “Create an object” then fill out the dialogue box as follows:
   X position: Random(780)
   Y position: Random(580)

See the following screenshot which illustrates the above.  

Click on OK.

You may be asking, at this point, what is Random(780) or Random(580)?

Random() is a GD function which when called will return a random number between 0 and the number specified inside brackets. Using the random function helps in a way that our game is not predictable. Whatever number is returned by the Random() function is by chance.

Random(780) will return a number between 0 and 780.

Random(580) will return a number between 0 and 580.

In the nutshell, a new bug sprite object will be created every 2 to 8 seconds at a random location anywhere across the screen.

Next, on-click of a mouse (or a touch on touch displays) we want the bug to disappear.

6

The next event will look like this once created.

To create the next event, click on the highlighted “Add condition” as shown below.

Select the “Other conditions” tab

Select “Mouse and touch”

Select “Mouse button pressed or touch help”

For “Button to test” select “Left (primary)”

Click OK.

Next, click on the immediate “Add condition” just below within the same event (this event will have 2 conditions)

Next click on “Bug”

Click on “The cursor/touch is on an object”

Click on OK

7

Next click on the corresponding “Add action”

Select “Bug”

Select “Delete an object”

Click on “OK”

8

There you have it! Click on the Preview button and try the game we have thus far.

Game Files/Resources

You can download the files from the link below. 

Download link.

Previous Unit 6 – Movements Within Boundaries

Table of Contents