这里发生了一些我不完全理解的事情。我创建了一个自定义类,它扩展了 MovieClip 以提供一些自定义属性并在创建的 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();
}
}
}
然后,我将 mapPin 类的几个实例添加到舞台上的容器影片剪辑中,并将事件侦听器添加到该容器剪辑中。
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);
最后,我试图能够访问被单击的 mapPin 的属性(迄今为止唯一的一组是“品牌”)。
function pinClicked(e:MouseEvent):void
{
trace(e.target.name); // traces "instance xx" instead of "myTest1"
trace(e.target.brand); // traces "undefined"
}
我可以添加 mapPin 实例,并调整它们的 x 和 y,尽管我无法引用自定义类属性(如“brand”)并且它们的名称变为通用实例名称。我错过了什么?将添加超过 500 个这样的项目,我希望能够根据用户的点击从它们中提取信息。