Objects




Amazon.com Widgets

Let’s create four simple objects that will be our “game”. They can be anything you want actually, though two of them will be combining to make the fourth. I’m going to make three shapes and a key. Clear and consistent naming is helpful here as you will be referring to these objects frequently in your code. I made four movie clips named square_mc, circle_mc, triangle_mc and key_mc. I created a new layer inside my rooms_mc movie clip and added an instance of each of the shapes (not the key) – one to each room. I named each instance by its object name plus a “1” – in other words, square1_mc, circle1_mc, triangle1_mc.

inventory2.png

Now let’s go back to our inventory.

When playing one of these games it usually appears that you once you have clicked on an object it “moves” to your inventory. In our game, that’s an illusion. We’re going to put an entirely different instance of our object in our inventory. Clicking on an object in our game will simply make that instance of the object disappear and the object in our inventory appear. Easy, right?

Let’s open our inventory and create instances of our objects there for our collection. The objects in your inventory can be smaller than they are in the game and their relative size need not be consistent – for instance, a table could be the same size as the key. Therefore you can size them down as necessary to fit them in and make them look neat. Drag an instance of each of our four objects to a box in the inventory and give it an instance name that is its name with a “2 “ – ie. square2_mc, circle2_mc. Etc. Again, this is important because when you're writing your code you want to know easily what the instance name of the object you need is. If you keep it consistent, you won't have to go back to the object and look.

Once you have everything looking the way you want it take a moment to jot down the x and y values of each of these objects – you’ll need it for coding later.

inventory3.png

Now let’s start adding the code that will allow us to collect our objects from the room and "put" them into our inventory. First let’s go to the first frame of the actions layer of our main timeline. This is where we’ll initiate the variables that give us the state of each object – is it found or not? Is it visible or not? These are the states we’ll be changing throughout the game and what will affect our players ability to move around the room without having objects appear and reappear in their inventory. We don't want these states to change every time the player moves around the room -- we only want them to reset each time the game is loaded. The code for the first object is as follows:

var squarelocated:String;
squarelocated="Not located";
_root.inv_mc.square2_mc._visible=false;

First, we’re declaring a variable called “Square located” and typing it as a string (it could also be Boolean, but as the game gets more complicated I’ve found it useful to keep these variable as strings). We define this string as “Not located”. As you can guess, later on when the player finds the object, we’ll change that variable's value to “Located”. We also set the visible property of the square instance in our inventory to false. Cut and paste this code for each other the three remaining objects – circle, triangle and key and change the instance names accordingly.

The resulting code should look like this:

var squarelocated:String;
squarelocated="Not located";
_root.inv_mc.square2_mc._visible=false;
var circlelocated:String;
circlelocated="Not located";
_root.inv_mc.circle2_mc._visible=false;
var trianglelocated:String;
trianglelocated="Not located";
_root.inv_mc.triangle2_mc._visible=false;
var keylocated:String;
keylocated="Not located";
_root.inv_mc.key2_mc._visible=false;

If you save and test your movie now, not much should happen except that all the objects in your inventory will be invisible. Now let’s go into our rooms_mc and play with our objects there.