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

Instance name is null?

New Here ,
May 08, 2013 May 08, 2013

I generated a list of movieclip that contain different text.

for(var i:int=0;i<locationarray.length;i++) {

                                        locationmc=new locationname();

                                        addChild(locationmc)

                                        locationmc.name="loc1"

                                        locationmc.locationtxt.text=locationarray;

                                        locationmc.x = Math.floor(Math.random() * 500);

                                        locationmc.y = Math.floor(Math.random() * 300);

                                        locationmc.y=100;

                                        locationmc.x=200;

                                        trace(locationmc.name)

                                        locationobject.push(locationmc)

                                        locationmc.addEventListener(MouseEvent.MOUSE_DOWN,mousedown)

                                        locationmc.addEventListener(MouseEvent.MOUSE_UP,mouseup)

                                        stage.addEventListener(MouseEvent.CLICK,testobject);

                              }

But when I do a check on the instance name it would show up null, code for the checking is as follows

public function testobject(e:Event){

                              trace(e.target.name)

                              trace(e.currentTarget.name)

                    }

TOPICS
ActionScript
940
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

LEGEND , May 08, 2013 May 08, 2013

It is possible e.target is not what you think it is.  Try tracing e.target to see what you are trying to get a name from.  When you trace e.currentTarget you are going to get the object that has the listener assigned, which in this case is the stage, which does not implement a name property.

Translate
LEGEND ,
May 08, 2013 May 08, 2013

It is possible e.target is not what you think it is.  Try tracing e.target to see what you are trying to get a name from.  When you trace e.currentTarget you are going to get the object that has the listener assigned, which in this case is the stage, which does not implement a name property.

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
New Here ,
May 08, 2013 May 08, 2013

I'm so dumb, you right, I shouldn't have added to stage.

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
New Here ,
May 08, 2013 May 08, 2013

Another question, I have bunch of movieclip placed on stage already with different instance name, is there a way to select them in the as file? I tried getChildByName("loc1").addEventListener(MouseEvent.CLICK,test) but it seem to be null.

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
LEGEND ,
May 08, 2013 May 08, 2013

If you have an object you placed on the stage with an instance name of loc1 assigned to it, then all you should need to do is use...

loc1.addEventListener(MouseEvent.CLICK,test);

But that might not work if the as file is not the document class and the as file has no reference to objects on the stage..

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
New Here ,
May 08, 2013 May 08, 2013

It doesn't seem to work I tried to declear it on top public var loc1:MovieClip but it would still gave error 1009, and when I try to put a list of loc in array it appear to be null as well

var targetarray:Array=new Array(loc1,loc2,loc3,loc4,loc5,loc6,loc7,loc8,loc9,loc10,loc11,loc12)

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
New Here ,
May 08, 2013 May 08, 2013

but suprisingly, when I put the code on frame it would work, I don't understand why

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
New Here ,
May 08, 2013 May 08, 2013
LATEST

never mind, I fixed, thank you anyway

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