shibbydoo
shibbydoo

Reputation: 569

ActionScript 3 name property is not returning the right name...?

I experienced a problem with the name property in as3, I created this "dot" movieclip and I exported to a class, then I anonymously created a bunch of dots using a loop. I assigned numbers as name to each dots

private function callDots(num:Number):void
    {                
        for (var i = 0; i < subImagesTotal[num]; i++)
        {
            var d:Dot = new Dot();
            d.x = i*23;
            d.y = 0;

            d.name = i;
            dotContainer.addChild(d]);
        }
    }  

so far so good, I checked that if I trace the name here, I will get the number I want. However, it's not giving me the numbers if I trace it in other functions. I added all of my dots to "dotContainer", and if I click on one of the dots, it will call this function

private function callFullSub(e:MouseEvent):void
   {
        var full_loader:Loader = new Loader();
        var temp:XMLList = subImages[sub];
        var full_url = temp[e.target.name].@IMG;

        full_loader.load(new URLRequest(full_url));
        full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
    }  

e.target.name is suppose to be numbers like 1 or 2, but it's giving me "instance66" "instance70" and I have no idea why. Because I did the same thing with loaders before and it totally worked. Any ideas? Thanks.

christine

Upvotes: 0

Views: 6616

Answers (5)

Brian Hodge
Brian Hodge

Reputation: 2135

You have not shown enough of your code for me to be able to give you a DEFINATE answer, I will however say this.

//After you create each loader you need to set its mouseEnabled
//property to false if you do not want it to be the target of
//Mouse Events, which may be superseding the actual intended target;

var full_loader:Loader = new Loader();
full_loader.mouseEnabled = false;
//Also you could name the loaders and see if what comes back when you click is the same.

ALSO! Add this to your Mouse Event handler for CLICK or MOUSE_DOWN:

trace(e.target is Loader);  //If traces true you have an answer

I believe that the mouse events are being dispatched by the Loaders.

please provide more of your code, the code where the Loader.contentLoaderInfo's COMPLETE handler fires. I assume this is where you adding the loaders to the display list as I cannot see that now.

Upvotes: 0

yutakaren
yutakaren

Reputation: 11

Try this might work,..

d.name = i.toString(); 

Upvotes: 1

Navee
Navee

Reputation:

The answer is [e.currentTarget.name] I perform this all the time!

Should return "Dot1" "Dot2", etc.

If the value you wish to return is a number or other data type other than a string (name of object) use [e.currentTarget.name.substr(3,1).toString()]

Should return 1, 2, etc.

Navee

Upvotes: 2

Jacob Poul Richardt
Jacob Poul Richardt

Reputation: 3143

The e.target returns the inner most object clicked on, this could be a TextField, another MovieClip or posibly a shape (I'm not 100% of the last one) inside the "Dot".

To prevent this you could try to set the mouseChildren property to false on the Dot's when you add them. This should insure that nothing inside the dots can dispatch the click event, and thus the Dot's should do it.

Perhaps you could also in the event handler verify the target type with code like this:

private function callFullSub(e:MouseEvent):void
{
    if(!e.target is Dot)
        throw new Error("target in callFullSub is not Dot but: " + e.target.toString());

    //The rest of you code here
}

Upvotes: 2

user56512
user56512

Reputation:

I tried to reproduce your problem first with Flex using runtime created movieClips and then with Flash using Dot movieClip symbols exported for ActionScript. Neither application exhibited the problem.

You may already know names like "instance66" "instance70" are default enumerated instance names. So, whatever is dispatching the MouseEvent is NOT the dot instance. Perhaps you are unintentionally assigning callFullSub to the wrong targets, maybe your containers? Try assigning it to dot instance right after you create them, like this:

private function callDots(num:Number):void
{                
    for (var i = 0; i < subImagesTotal[num]; i++)
    {
        var d:Dot = new Dot();
        d.x = i*23;
        d.y = 0;

        d.name = i;
        d.addEventListener(MouseEvent.CLICK, callFullSub);
        dotContainer.addChild(d]);
    }
}

Be sure to temporarily comment out your original assignment.

Upvotes: 1

Related Questions