Instance Variables

3

Index

Attached Files

The following files have been attached to this tutorial:

.capx

instance-variables-loops.capx

Download now 2.26 MB

Stats

5,042 visits, 8,397 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 18 Jan, 2017. Last updated 19 Feb, 2019

Add text name it

txtAnimals

txtAnimals2

Now to add Instance Variable clicks on our animation sprite Animals and then click on LION. On right hand side Property click on Instance Variables box should come into view which looks like below.

In this click on plus sign and another Instance Variables box should come into view.

In this box enter Name as Animal_ID

We need Number so do not change it

In Initial Value enter nothing

Press OK and close this box

Press + plus sign again and add name_Animal and do same as above. Your Instance Variable box looks like below

Do not be concern if your above box not shows <varies> it will be shown once you enter the Instance Variable. Close the box we finished here. Adding code the long way will be like endless code e.g.

Once you start using this method then you will be copying above code 8 times and changing Lion to Elephant, Panda etc but if you use Instance Variable then see the result small code but do more intelligently and accurately as well.

Add this line of code in our:

The code above use sub event, if you don’t know how to create sub event then just do this Mouse Cursor over Animals, right click or press B on keyboard. This action should add empty SUB EVENT.

Running the project now and move mouse over any Animals sprite the text box will accurately display the Animals instance variable Animal_ID number. Cool.

Well that’s cool but what if you want player to distinguish which Animals he or she has mouse over?

Now what if we wants to hide the Animals sprite when certain button clicked then the sprite in question must be hidden.

Not bad Construct 2 is very versatile and flexible

Now we come to developing Fighter or design spaceship, also want player to fire missile as well. For this idea let’s create Sprite and name it Fighter. For Fighter animation import these 6 sprites into your default animation.

Suppose we want to let somebody know that clicking left mouse button on Fighter the fighter can fire missile as well. We can just add text and say press left mouse button to fire missile. To put this into preparation we can add this line of code.

This is all very well that person playing game can catch sight of Fighter he or she has clicked on but, nothing happens. We would like to give player choice to fire missile too. Code below will do just that and also display which Fighter has fired missile too.

As you can see what Instance variable can do. It is powerful if you know how to use it without adding hundreds of lines of code. Before we finish on this create sprite and name it Level_2, also add text and Level_2.

Level_1 screen

  • 0 Comments

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