Stefan Đorđević
Stefan Đorđević

Reputation: 595

AS2 Add more movieclips to var

This question may be weird, but it's a big problem for me.

onClipEvent (load) {
    var ground:MovieClip = _root.bottomground1;
    var grav:Number = 0;
    var gravity:Number = 2;
    var speed:Number = 10;
    var maxJump:Number = -17;
    var touchingGround:Boolean = false;
}

So, the thing i want to do is to add more movieclips to var ground, such as

var ground:MovieClip = _root.bottomground1,_root.bottomground2...;

I tried this also:

var ground:MovieClip = _root.bottomground1, MovieClip = _root.bottomground2...;

None of it is working. Any reply would be very helpful.

Upvotes: 0

Views: 78

Answers (1)

Philarmon
Philarmon

Reputation: 1806

A variable can only have one value :) What you need is an array. Might not be a valid AS2, it's been a while:

Create an array:

var groundArray:Array = new Array(_root.bottomground1, _root.bottomground2);

OR something like

// add objects with instance names bottomground1 to bottomground9
var groundArray:Array = new Array();
for(var i:int = 1; i < 10; i++)
{
    groundArray.push(_root[(bottomground + i)]);
}

Read from your array:

var myGroundMC:MovieClip = groundArray[0] as MovieClip; // first object in array

OR

// Loop trough all objects and do something
for(var i:int = 0; i < groundArray.length; i++)
{
    var myGroundMC:MovieClip = groundArray[i] as MovieClip;

    // do something with it
}

Upvotes: 1

Related Questions