The code below seems absolutely watertight to me, but for the life of me I can’t see why it doesn’t work as intended:
Basically I have a ball that travels across 2 movie clips & should start reversing once it senses it is over clip 2.
I’ll set out the code as it currently appears in the movie & then a commented version that shows my logic with it & possibly where I’m going wrong.
An fla is also attached.
Actual Code:
code:
_root.hold_mc.trackOne_mc.num = 1;
_root.hold_mc.trackTwo_mc.num = 2;
done = 0;
_root.ball_mc.onEnterFrame = function() {
this._x += 2;
for (i in _root.hold_mc) {
if (_root.ball_mc.hitTest(_root.hold_mc[i]) && done == 0) {
numHit = _root.hold_mc[i].num;
done = 1;
trace(_root.hold_mc[i].num);
if (numHit != _root.hold_mc[i].num) {
_root.ball_mc.onEnterFrame = null;
_root.ball_mc.onEnterFrame = function() {
_root.ball_mc._x -= 2;
};
}
}
}
};
Commented code:
code:
// I have assigned variables to each of the clips that the ball travels over so as to //differentiate them:
_root.hold_mc.trackOne_mc.num = 1
_root.hold_mc.trackTwo_mc.num = 2;
done = 0;// A flag variable to get the .num value of the first clip
// An onEnterFrame function to move the ball
_root.ball_mc.onEnterFrame = function() {
this._x += 2;
// A for(in) loop to capture the .num value of the first clip to then check against it.
for (i in _root.hold_mc) {
if (_root.ball_mc.hitTest(_root.hold_mc[i]) && done == 0) {
numHit = _root.hold_mc[i].num;
done = 1;
/*Ok this is where I think it is breaking down. To my mind this section will check the ball as it moves along – if whatever number “_root.hold_mc[i].num” throws up is different to the “numHit” variable designated above the ball goes into reverse*/
if (numHit != _root.hold_mc[i].num) {
_root.ball_mc.onEnterFrame = null;
_root.ball_mc.onEnterFrame = function() {
_root.ball_mc._x -= 2;
};
}
}
}
};




Reply With Quote