Search code examples
actionscript-3flashdynamicactionscriptmovieclip

my dynamically added movieclips have a name of "instance XX"


There are a couple things going on here that I dont fully understand. I have created a custom class that extends MovieClip to give some custom properties and create a geometric shape inside of the created MovieClip

package com.hyatt
{
import flash.display.*;
import flash.geom.*;
public class mapPin extends MovieClip
{

    public var spirit:String;
    public var callName:String;
    public var hotelName:String;
    public var city:String;
    public var s:String;
    public var zip:String;
    public var country:String;
    public var brand:String;
    public var featured:Boolean;
    public var horizon:Boolean;
    private var _mc1:MovieClip = new MovieClip();

    public function mapPin(_brand:String)
    {
        brand = _brand;
        switch (_brand)
        {
            case "Andaz":
                    pinCircle(0xff0000);
                    break;
            case "Grand Hyatt":
                    pinCircle(0x0000ff);
                    break;
            case "Hyatt":
                    pinCircle(0x4600f0);
                    break;
        }
    }

    private function pinCircle(color:uint):void
    {
        _mc1.graphics.beginFill(color);
        _mc1.graphics.drawCircle(0,0,20);
        this.addChild(_mc1);
        _mc1.graphics.endFill();
    }
}

}

Then I'm adding an couple instances of the mapPin class to a container movieclip on my stage and adding an event listener to that container clip.

var myTest1:mapPin = new mapPin("Andaz");
myTest1.brand = "Andaz";
container_mc.addChild(myTest1);
myTest1.name = "myTest1" //this is added purely for testing the "instance xx", same result
myTest.x = 100;
myTest.y = 100;

var myTest2:mapPin = new mapPin("Hyatt");
container_mc.addChild(myTest2);
myTest2.brand = "Hyatt";
myTest2.x = 400;
myTest2.y = 400;

container_mc.addEventListener(MouseEvent.CLICK, pinClicked);

finally I'm trying to be able to access the properties (the only one set thusfar is "brand") of the mapPin that is clicked.

function pinClicked(e:MouseEvent):void
{
    trace(e.target.name); // traces "instance xx" instead of "myTest1"
trace(e.target.brand); // traces "undefined"
}

I can add the mapPin instances, and adjust their x and y though i cannot reference the custom class properties like "brand" and their name becomes a generic instance name. What am I missing? There are going to be upwards of 500 of these items added and I want to be able to pull information from them based upon a users click.


Solution

  • I'd have to see your mapPin class to be sure, but I think that the DisplayObject that is dispatching the event, is a child of mapPin.

    To fix this, inside your mapPin class constructor add this line :

    mouseChildren = false;
    

    That will specify that children shouldn't receive clicks/dispatch mouse events.