Friday, May 20, 2011

Forum rescue #003: Respawn an enemy

New scirra forum member darklad posted the following help request (original thread):

Let's open a new game project in Construct and add two sprites. One for the enemy and one for the nest, which is supposed to spawn the enemy. Additional to that we will make use of the Function object and also be needing the MouseKeyboard object for this tryout.

A simple way to add a little movement to the enemy would be to assign the Sine behavior to the sprite. Let's do that and also check the 'Destroy on startup' property.

> Download current state cap file <

An event that will reoccur in this little test is the nest spawning an enemy. This is why the Function object was added. When you define a function with the 'On function'-condition, you'll be able to reuse this piece of event-code whereever you want by simply calling that function with the action 'Call function'.

So add an event, give it an 'On function' condition and also a 'Spawn object'-action of the nest sprite. Maybe place already a few nest object in the layout.

> Download current state cap file <

Now if you run the game for a quick test, you'll notice that nothing happens. No enemies will be spawned, although we have an event with a spawning action? That's right, because the condition isn't met. Remember that functions are only executed if they are called.

We probably would want some enemies at the start of the layout. An event with a 'Start of layout'-condition followed by a 'For each object'-loop might do the trick. As action do simply call the function previously created.

Two things are really important about this. When you call a function, keep in that the name of the function is case sensitive. This means "create" wouldn't be the same function as "Create". It's a common mistake and sometimes causes some headscratching until you notice the typo. Also you need to select to remember picked objects when you create the action, otherwise there can't be proper picking for the different instances of the nest sprite.

> Download current state cap file <

If you run it now, enemies should have appeared at each nest. One step closer to our goal...

A little something is still missing to pair the enemies to their respective nests. Add a private variable 'Parent' to the enemy sprite. We will set this variable to the unique ID of the nest on creation, so it's easy to keep track of which enemy belongs to which nest. An unique ID is simply an ascending unique number that Construct automatically assigns to each object it creates.

Here's the improved 'Create'-function:

Since the objects are properly paired now, all that's left to do is a way of destroying enemies for testing. Thankfully we have the MouseKeyboard object already in there. Create an event with a 'On object clicked' trigger condition and select the enemy.

In the same event we would want to pick the respective nest as well, so we can make the correct instance to spawn another enemy.  Use the condition 'Compare unique ID' of the nest sprite and compare it to private variable of the already picked enemy.

The next action is pretty obvious: destroy the enemy. Followed by calling our function once again, this example would be complete.

Clicking on any enemy now will destory it, instantly spawning a new instance of the enemy sprite from the respective nest

> Download final cap file <

All this example does is storing the unique ID of one object in another's object private variable, therefore pairing those objects and making them easily pickable. Then we can call a function which creates a new object according to our needs.

I hope this helps some beginners to get along with Construct more smoothly. Any questions or suggestions feel free to comment.

No comments:

Post a Comment