Copy link to clipboard
Copied
I am trying to get the name of a MovieClip that the user is clicking on. I am getting an error at setting the startX value. Iss there a way to do this? Thanks.
Here is a sample code:
var MC;
function T01MouseDown(event:MouseEvent):void
{
trace("Tomato 01 Down");
trace(event.target.name);
MC = (event.target.name);
//trace(MC);
startX = MC.x;
startY = event.currentTarget.name.y;
event.currentTarget.parent.setChildIndex(event.currentTarget,event.currentTarget.parent.numChildren - 1);
event.currentTarget.name.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
currentClip = event.currentTarget.name;
trace(currentClip);
}
use getChildByName to use an instance name to return a display object:
...
var MC:DisplayObject;
function T01MouseDown(event:MouseEvent):void
{
trace("Tomato 01 Down");
trace(event.target.name);
MC = getChildByName(event.target.name);
//trace(MC);
startX = MC.x;
startY = MC.y;
event.currentTarget.parent.setChildIndex(event.currentTarget,eve nt.currentTarget.parent.numChildren - 1);
event.currentTarget.startDrag();
stag
Copy link to clipboard
Copied
use getChildByName to use an instance name to return a display object:
var MC:DisplayObject;
function T01MouseDown(event:MouseEvent):void
{
trace("Tomato 01 Down");
trace(event.target.name);
MC = getChildByName(event.target.name);
//trace(MC);
startX = MC.x;
startY = MC.y;
event.currentTarget.parent.setChildIndex(event.currentTarget,eve nt.currentTarget.parent.numChildren - 1);
event.currentTarget.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
currentClip = MC;
trace(currentClip.name);
}
Copy link to clipboard
Copied
Awesome.Thank you so much. One more question can I pass the event.currentTarget. to another function to stopdrag();
Copy link to clipboard
Copied
you're welcome.
and yes, you can pass event.currentTarget from within T01MouseDown to any other function: stopdrag(event.currentTarget)
btw, you don't need to access the name property of event.target/event.currentTarget and then use getChildByName. you can just reference event.target/event.currentTarget directly:
event.currentTarget.startDrag();
Copy link to clipboard
Copied
kglad
I am getting an erroron the stopDrag and I am not sure why. Do you see why?
function T01MouseDown(event:MouseEvent):void
{
trace("Tomato 01 Down");
//trace(event.target.name+'_name');
MC = getChildByName(event.target.name);
trace(MC);
startX = MC.x;
startY = MC.y;
MC.parent.setChildIndex(MC,MC.parent.numChildren - 1);
event.currentTarget.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stage2_onMouseUp);
//currentClip = MC;
//trace(currentClip.name);
}
function stage2_onMouseUp(event:MouseEvent):void
{
if (MC.hitTestObject(MovieClip(gray_mc)))
{
MC.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
gray_mc.gotoAndStop(gray_mc.currentFrame + 1);
removeChild(MC);
tomatoCount = tomatoCount + 1;//trace(tomatoCount);
tomatoGraybin = tomatoGraybin + 1;
grayCount.text = String(tomatoGraybin);
if (tomatoGraybin > 0)
{
tomatoGraypercentage = tomatoGraybin / tomatoSampleTotal;
tomatoGraypercentageformatted = Math.floor(tomatoGraypercentage*100)+"%";
grayPercentage.text = tomatoGraypercentageformatted;
}
if (tomatoCount == tomatoCountTotal )
{
whichBox = "question";
drawBox(whichBox);
}
stopDrag(event.currentTarget);
}
}
Copy link to clipboard
Copied
you're not using stopDrag() correctly. it accepts no parameters and can be used without even referencing the dragged object (because you can drag, at most, one object at any one time):
stopDrag(); // will always work.
you also may be able to use:
event.currentTarget.stopDrag();
if the dispatching objects for both listener functions are the same.
Copy link to clipboard
Copied
OK So my problem is that I am getting an error #1069 whith this code and it doesnt stopDrag.
function T01MouseDown(e:MouseEvent):void
{
trace("Tomato 01 Down");
//trace(event.target.name+'_name');
MC = getChildByName(e.target.name);
trace(e.currentTarget);
startX = e.currentTarget.x;
startY = e.currentTarget.y;
e.currentTarget.parent.setChildIndex(e.currentTarget,e.currentTarget.parent.numChildren - 1);
e.currentTarget.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stage2_onMouseUp);
}
function stage2_onMouseUp(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}
Copy link to clipboard
Copied
ok I got it. e.target.stopDrag();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now