Nested Movie Clip Timeline wont work (Depth/targets) in Flash AS2
My code works fine when all mc are placed on the main time-line(stage) However when I import them into a single Movie Clip called Container_Mc the code stops working. I'm sure it has to do with the target/depth
Here is the code that works on the time-line
stop ();
first = 1;
import mx.transitions.Tween;
import mx.transitions.easing.*;
function dragSetup(clip, targ) {
clip.onPress = function() {
startDrag(this);
};
clip.onRelease = clip.onReleaseOutside=function () {
stopDrag();
if (eval(this._droptarget) == targ) {
var t开发者_运维技巧x:Tween=new Tween(this,'_x',Elastic.easeOut,this._x,this.myFinalX,1,true);
var ty:Tween=new Tween(this,'_y',Elastic.easeOut,this._y,this.myFinalY,1,true);
targ.gotoAndStop(2);
} else {
var tx:Tween=new Tween(this,'_x',Elastic.easeOut,this._x,this.myHomeX,1,true);
var ty:Tween=new Tween(this,'_y',Elastic.easeOut,this._y,this.myHomeY,1,true);
targ.gotoAndStop(2);
}
};
//the variables below will store the clips starting position
clip.myHomeX = clip._x;
clip.myHomeY = clip._y;
//the variables below will store the clips end position
clip.myFinalX = targ._x;
clip.myFinalY = targ._y;
}
dragSetup(drag1,target1)
dragSetup(drag2,target2)
dragSetup(drag3,target3)
btn.onRelease = function() {
if ((_root.drag1._droptarget == "/target1") && (_root.drag2._droptarget == "/target2") && (_root.drag3._droptarget == "/target3")) {
if (first == 1) {
first = 0;
result +=
}
comment = "Great! correct answer";
_root.attachMovie("glamour", "glamour2", 202);
_root.glamour2._x = 226;
_root.glamour2._y = 153;
} else {
comment = "try again!!";
first = 0;
}
}
Im pretty sure the problem lies in the IF STATMENT in the BTN FUNCTION because the rest of the code above works fine when imported inside Container_Mc its just the if statement the else "Try Again" Answer is just returned even though the targets are right??
Anyone any Ideas?
In your button handler you have several references to _root
. This is probably the cause of the problem, as I imagine drag1, drag2 etc are all inside your container. The code in this function is executing in the scope of your button, so if the button is on the same timeline as the drop movieclips then you can safely replace all the occurrences of _root
with _parent
.
A better approach though would be to change the scope of the button handler to that of you container, meaning you need neither root nor parent. This is done using delegate:
btn.onRelease = Delegate.create(this,buttonHandler);
function buttonHandler() {
if ((eval(drag1._droptarget) == target1) && (eval(drag2._droptarget) == target2) && (eval(drag3._droptarget) == target3)) {
// only if all three dragable objects are dropped to their respective targets sentence returns true
if (first == 1) { first = 0; result += }
comment = "Great! correct answer";
attachMovie("glamour", "glamour2", 202);
glamour2._x = 226;
glamour2._y = 153;
} else {
comment = "try again!!";
first = 0;
}
Be sure to import the Delegate class at the top of your script for this to work:
import mx.utils.Delegate;
This second approach is better because you don't have to worry about different scopes, and also it is similar to the approach used in AS3.
精彩评论