PDA

View Full Version : Any other way of doing it? Movieclip's name as a variable?



kadaj
November 2nd, 2009, 08:45 AM
Hi,

I have one horse animation in the library.
I have made 6 instances of the Horse() that's in the library named:

horse0_mc
horse1_mc
.......
.......
horse5_mc

I have a function called checkBound() that checks whether the horse has crossed a particular line (x value)
The code is:


function checkBound()
{
if (horse0_mc.x <= -800) {
Hwin(0);
} else if (horse1_mc.x <= -800) {
Hwin(1);
} else if (horse2_mc.x <= -800) {
Hwin(2);
} else if (horse3_mc.x <= -800) {
Hwin(3);
} else if (horse4_mc.x <= -800) {
Hwin(4);
} else if (horse5_mc.x <= -800) {
Hwin(5);
}
}

Instead of typing the whole instances of the horse
can I do it with one if and else with the changing values as variables?
Problem: If so how is horseX_mc where X = 0, 1, ..5 be written ?
I suppose we could easily pass the corresponding variable X to the function Hwin(X).
Hope the question is clear.

Thank You.

[MuG]
November 2nd, 2009, 08:50 AM
var horse : MovieClip; // Create a holder var
for (var i : uint = 0 ; i < 6 ; i++) { // Loop 6 times, incrementing i by 1
horse = this["horse" + i + "_mc"]; // Set the holder var to horseX_mc
if (horse.x <= -800) { // Check the horse x position
Hwin(i); // Run Hwin with i
}
}

kadaj
November 2nd, 2009, 10:05 PM
;2513371']

var horse : MovieClip; // Create a holder var
for (var i : uint = 0 ; i < 6 ; i++) { // Loop 6 times, incrementing i by 1
horse = this["horse" + i + "_mc"]; // Set the holder var to horseX_mc
if (horse.x <= -800) { // Check the horse x position
Hwin(i); // Run Hwin with i
}
}


Thank You
Its works fine!
Could you please explain a bit more about: this[ ].
Is square brackets used to tell flash that the one inside it is an object?
Can you tell me the difference between this. and this[ ].

TheCanadian
November 3rd, 2009, 12:43 AM
Can you tell me the difference between this. and this[ ].

They're the same except object[var] (called associative array referencing) let's you use a string and therefore variables which is super handy. This explanation may help:
http://www.kirupa.com/forum/showpost.php?p=2059193&postcount=36

kadaj
November 3rd, 2009, 01:17 AM
They're the same except object[var] (called associative array referencing) let's you use a string and therefore variables which is super handy. This explanation may help:
http://www.kirupa.com/forum/showpost.php?p=2059193&postcount=36

Yup!
Thanks!