views:

3085

answers:

4

Hi all,

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

+1  A: 

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.

Michael Prescott
He would still have the same problem unless he change "e.target" to "e.currentTarget" in the "callFullSub" function.
Lillemanden
Ah, yes that explains the instance names. The are not parent names, but child names. His Dot is a composition of movieClips or Sprites. The two applications I created while attempting to reproduce the problem assumed that Dot was a simple MovieClip containing only a graphic.
Michael Prescott
+2  A: 

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
}
Lillemanden
A: 

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.

Brian Hodge
blog.hodgedev.com

Brian Hodge
+1  A: 

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