Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Convert Object to String

Community Beginner ,
Apr 04, 2013 Apr 04, 2013

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);

}

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 04, 2013 Apr 04, 2013

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

...
Translate
Community Expert ,
Apr 04, 2013 Apr 04, 2013

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 04, 2013 Apr 04, 2013

Awesome.Thank you so much.  One more question can I pass the event.currentTarget. to another function to stopdrag();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2013 Apr 04, 2013

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 05, 2013 Apr 05, 2013

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);

                    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2013 Apr 05, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 05, 2013 Apr 05, 2013

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 05, 2013 Apr 05, 2013
LATEST

ok I got it.  e.target.stopDrag();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines