我试图通过flash + actionscript制作一个简单的卡片匹配游戏,并且在分配事件监听器和名称方面遇到了很大的麻烦。我已经获得了所有的卡片生成语句,它们都绘制到我的舞台上,但即使我使用newCard.name分配它们的实例名称,我在跟踪点击时获得的名称在每个按钮上总是“root1”,我不明白为什么。
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public dynamic class cardGameMain extends MovieClip {
public function cardGameMain() {
addCards();
}
public function addCards() {
var lastCard:int;
for (var i = 1; i < 17; i++) {
var newCard:MovieClip;
newCard = new cardBackSymbol();
newCard.name = "card" + i;
addChild(newCard);
newCard.addEventListener(MouseEvent.MOUSE_UP, decideCard);
if (i == 1 || i == 5 || i == 9 || i == 13) {
newCard.x = 20;
if (i == 1) {
newCard.y = 20;
}
else if (i == 5) {
newCard.y = 240;
}
else if (i == 9) {
newCard.y = 460;
}
else if (i == 13) {
newCard.y = 680;
}
lastCard = 20;
} else if (i > 1 && i < 5) {
newCard.x = lastCard + 145;
newCard.y = 20;
lastCard = lastCard + 145;
} else if (i > 5 && i < 9) {
newCard.x = lastCard + 145;
newCard.y = 240;
lastCard = lastCard + 145;
} else if (i > 9 && i < 13) {
newCard.x = lastCard + 145;
newCard.y = 460;
lastCard = lastCard + 145;
} else {
newCard.x = lastCard + 145;
newCard.y = 680;
lastCard = lastCard + 145;
}
trace(newCard.name + " position is " + newCard.x + ", " + newCard.y);
}
}
public function decideCard(e:MouseEvent):void {
trace(this.name)
}
}
}
对此事的任何帮助都很有意义!
答案 0 :(得分:1)
您正在使用引用包含类的this
关键字,而不是单击的对象。
请改为尝试:
public function decideCard(e:MouseEvent):void {
trace(DisplayObject(e.currentTarget).name)
}