Skip to main content
Known Participant
June 13, 2011
Answered

Can not get the index number from a StartDrag Function

  • June 13, 2011
  • 4 replies
  • 978 views

//*//*//*//*//**//**/**//**//**//**//**//**//**//**//**//**//**//

var t:Array=new Array();
t=[d1,d2,d3,d4,d5];

var d:Array=new Array();
d=[t1,t2,t3,t4,t5];

// ** // ** //
var ust:int;

for(var i:int=0;i<t.length;i++){
t.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
}


for(var j:int=0;j<t.length;j++){
t.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
}

function mouseDowner(event:MouseEvent):void {
event.target.startDrag();
trace(t.indexOf(MovieClip(event.target)));
var ast:int;
ast=t.indexOf(MovieClip(event.target));
trace(ast);
ust=ast.valueOf();
}

function mouseReleased(event:MouseEvent):void {
event.target.stopDrag();
if(d.indexOf(MovieClip(event.target).dropTarget.parent)!=-1){
trace(d.indexOf(MovieClip(event.target).dropTarget.parent));}


else{trace("not appropriate");}
//trace(MovieClip(event.target).dropTarget.parent.name);
}


var tf1:TextField = new TextField();
tf1.text="LIGHT";
tf1.x=-30;
tf1.y=-30;
t1.addChild(tf1);
tf1.mouseEnabled=false;

var tf2:TextField = new TextField();
tf2.text="EASY";
tf2.x=-30;
tf2.y=-30;
t2.addChild(tf2);
tf2.mouseEnabled=false;

var tf3:TextField = new TextField();
tf3.text="IMPOSSIBLE";
tf3.x=-30;
tf3.y=-30;
t3.addChild(tf3);
tf3.mouseEnabled=false;

var tf4:TextField = new TextField();
tf4.text="FAST";
tf4.x=-30;
tf4.y=-30;
t4.addChild(tf4);
tf4.mouseEnabled=false;

var tf5:TextField = new TextField();
tf5.text="UGLY";
tf5.x=-30;
tf5.y=-30;
t5.addChild(tf5);
tf5.mouseEnabled=false;

//**//**//**

var tf6:TextField = new TextField();
tf6.text="DARK";
tf6.x=-30;
tf6.y=-30;
d1.addChild(tf6);
tf6.mouseEnabled=false;


var tf7:TextField = new TextField();
tf7.text="DIFFICULT";
tf7.x=-30;
tf7.y=-30;
d2.addChild(tf7);
tf7.mouseEnabled=false;


var tf8:TextField = new TextField();
tf8.text="POSSIBLE";
tf8.x=-30;
tf8.y=-30;
d3.addChild(tf8);
tf8.mouseEnabled=false;


var tf9:TextField = new TextField();
tf9.text="SLOW";
tf9.x=-30;
tf9.y=-30;
d4.addChild(tf9);
tf9.mouseEnabled=false;


setChildIndex(d5, 1);
var tfh:TextField = new TextField();
tfh.text="BEAUTIFULL";
tfh.x=-30;
tfh.y=-30;
d5.addChild(tfh);
tfh.mouseEnabled=false;

setChildIndex(d[0], 0);
setChildIndex(d[1], 0);
setChildIndex(d[2], 0);
setChildIndex(d[3], 0);
setChildIndex(d[4], 0);

trace(ust);

This topic has been closed for replies.
Correct answer Ned Murphy

Do all of the traces in the function show what you expect?  If so, what makes you conclude that the ust variable is not assigned the ast value?

One thing I would recomend is that you use "event.currentTarget" instead of "event.target", just to be sure that the object to be dragged is the object with the listener assigned to it.  Otherwise, the target could be another object inside the object with the listener assigned.

4 replies

ohamsiciAuthor
Known Participant
June 13, 2011

Ned,thnx, what you say again worked. As shown above, I returned eventTargets to

event current Targets and I caught what I want. But this is a  very specific nuissance that I relaize I couldnt do it myself.

What is the clear difference between curretTarget and my mistake event.target ???

Ned Murphy
Legend
June 13, 2011

The simplest way of describing the difference is that currentTarget will always point to the object that has the event listener assigned to it, while target can point to a child object within the object that has the event listener assigned to it.

Here's a link to a brief article that might be helpful for you...

http://www.wastedpotential.com/?p=10

ohamsiciAuthor
Known Participant
June 13, 2011


//*//*//*//*//**//**/**//**//**//**//**//**//**//**//**//**//**//

var t:Array=new Array();
t=[d1,d2,d3,d4,d5];

var d:Array=new Array();
d=[t1,t2,t3,t4,t5];

// ** // ** //
var ust:int;
var alt:int;

for(var i:int=0;i<t.length;i++){
t.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
}


for(var j:int=0;j<t.length;j++){
t.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
}

function mouseDowner(event:MouseEvent):void {
event.target.startDrag();
trace(t.indexOf(MovieClip(event.currentTarget)));

ust=t.indexOf(MovieClip(event.currentTarget));
trace(ust);
trace(ust);
}

function mouseReleased(event:MouseEvent):void {
event.target.stopDrag();

if(d.indexOf(MovieClip(event.currentTarget).dropTarget.parent)!=-1){
trace(d.indexOf(MovieClip(event.currentTarget).dropTarget.parent));
alt=d.indexOf(MovieClip(event.currentTarget).dropTarget.parent);
trace(alt);}
trace(alt);
trace(ust);
if(alt==ust){trace("ok");}

else{trace("not appropriate");}
}


var tf1:TextField = new TextField();
tf1.text="LIGHT";
tf1.x=-30;
tf1.y=-30;
t1.addChild(tf1);
tf1.mouseEnabled=false;

var tf2:TextField = new TextField();
tf2.text="EASY";
tf2.x=-30;
tf2.y=-30;
t2.addChild(tf2);
tf2.mouseEnabled=false;

var tf3:TextField = new TextField();
tf3.text="IMPOSSIBLE";
tf3.x=-30;
tf3.y=-30;
t3.addChild(tf3);
tf3.mouseEnabled=false;

var tf4:TextField = new TextField();
tf4.text="FAST";
tf4.x=-30;
tf4.y=-30;
t4.addChild(tf4);
tf4.mouseEnabled=false;

var tf5:TextField = new TextField();
tf5.text="UGLY";
tf5.x=-30;
tf5.y=-30;
t5.addChild(tf5);
tf5.mouseEnabled=false;

//**//**//**

var tf6:TextField = new TextField();
tf6.text="DARK";
tf6.x=-30;
tf6.y=-30;
d1.addChild(tf6);
tf6.mouseEnabled=false;


var tf7:TextField = new TextField();
tf7.text="DIFFICULT";
tf7.x=-30;
tf7.y=-30;
d2.addChild(tf7);
tf7.mouseEnabled=false;


var tf8:TextField = new TextField();
tf8.text="POSSIBLE";
tf8.x=-30;
tf8.y=-30;
d3.addChild(tf8);
tf8.mouseEnabled=false;


var tf9:TextField = new TextField();
tf9.text="SLOW";
tf9.x=-30;
tf9.y=-30;
d4.addChild(tf9);
tf9.mouseEnabled=false;


setChildIndex(d5, 1);
var tfh:TextField = new TextField();
tfh.text="BEAUTIFULL";
tfh.x=-30;
tfh.y=-30;
d5.addChild(tfh);
tfh.mouseEnabled=false;

setChildIndex(d[0], 0);
setChildIndex(d[1], 0);
setChildIndex(d[2], 0);
setChildIndex(d[3], 0);
setChildIndex(d[4], 0);

ohamsiciAuthor
Known Participant
June 13, 2011

As you see, when the return and equality didnt work. I tried valuof. it didnt work al

so...

Ned Murphy
Ned MurphyCorrect answer
Legend
June 13, 2011

Do all of the traces in the function show what you expect?  If so, what makes you conclude that the ust variable is not assigned the ast value?

One thing I would recomend is that you use "event.currentTarget" instead of "event.target", just to be sure that the object to be dragged is the object with the listener assigned to it.  Otherwise, the target could be another object inside the object with the listener assigned.

ohamsiciAuthor
Known Participant
June 13, 2011

In the code above, there is an integer ust. this should be the index number of the event target of start drag function. But I coludnt return it in a way.

Itried function MouseDowner(event:MoUSEeVENT,num1:int):int

and

ust=....

but It didnt return. How can I return a number in a start drag function