[mx04]Why won't a varaible assigned through a "for in" loop show up in next function?
This is really driving me nuts!
I want to change direction of a ball based on which clip it is over – I’m using “hitTest()” to check for the change in clips.
To flag up a change I’m using a homemade property of the clip “num”. It is just a simple number reference so each clip has a number property starting at 1 & increasing with each clip.
_root.hold_mc.base1_mc.num =12; is a sample of the assignment for the first clip.
I store a reference to the current clip that the ball is on’s reference number in a variable called “oldHit”
So to test for a collision I use:
if (Number(_root.hold_mc[i].num) != oldHit){
//A change has happened do something else;
}
It all seems to work except when I want to assign the new clips variables to the onEnterFrame event.
if (Number(_root.hold_mc[i].num) != oldHit && asign == 0) {
newHit = _root.hold_mc[i];
trace(newHit);
asign = 1;
}
This will give me a reference to the correct clip, but when I try to use “newHit” in the “If” statement that follows a “trace” command tells me that it is “undefined”?
The full code is set out below together with an attached fla which is commented in the function in question.
Thank you very much for any help
code:
_root.hold_mc.base_mc.angle = -Math.PI/2;
_root.hold_mc.base_mc.sign = 1;
_root.hold_mc.base_mc.num = 1;
_root.hold_mc.base2_mc.angle = -Math.PI;
_root.hold_mc.base2_mc.sign = 0;
_root.hold_mc.base2_mc.num = 2;
done = 0;
ardone = 0;
var radius = 100;
var speed = .5;
asign = 0;
_root.onEnterFrame = function() {
for (i in _root.hold_mc) {
if (_root.circ_mc.hitTest(_root.hold_mc[i])) {
if (ardone == 0) {
oldHit = _root.hold_mc[i].num;
ardone = 1;
}
if (Number(_root.hold_mc[i].num) != oldHit && asign == 0) {
newHit = _root.hold_mc[i];
trace(newHit);
asign = 1;
}
if (done == 0) {
angle = _root.hold_mc[i].angle;
trace(_root.hold_mc[i]);
//comes up as "undefined"
//I would like to use the new clips reference for the following variables
// but Flash is having none of it!
//So I would have liked to have used :sign = newHit.sign;
sign = _root.hold_mc[i].sign;
centerX = _root.hold_mc[i]._x;
centerY = _root.hold_mc[i]._y;
radianSpeed = speed*Math.PI/180;
done = 1;
}
}
sign == 1 ? angle += radianSpeed : angle -= radianSpeed;
ax = Math.cos(angle)*radius;
ay = Math.sin(angle)*radius;
_root.circ_mc._x = centerX+ax;
_root.circ_mc._y = centerY+ay;
//trace(_root.circ_mc._y);
}
};