我正在尝试通过 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)
}
}
}
非常感谢您对此事的任何帮助!