Flash:功能被触发不止一次
Flash: Function is triggered more than once
我正在尝试制作一个小的 21 点游戏。
我 运行 遇到了一个问题,我找不到我的错误。
当你在第一轮想要一张额外的牌时,你会得到一张牌。 (你必须按一个按钮才能得到它),但在第二轮,当你点击按钮时,该功能触发两次,在第三轮中触发 3 次,依此类推。
在回合之间,我从屏幕上删除所有内容。
这里是经常触发的函数:
function extracard() {
var cextra: int = drawcard(cardpos, 400, true);
trace("drew extra card");
if (cextra == 1) {
var valchooser3: _1or11 = new _1or11();
addChild(valchooser3);
valchooser3.one.addEventListener(MouseEvent.CLICK, choose3_1);
valchooser3.eleven.addEventListener(MouseEvent.CLICK, choose3_11);
valchooser3.x = 280;
valchooser3.y = 380;
//chipsandcardonscreen.push(valchooser3);
function choose3_1() {
valchooser3.one.removeEventListener(MouseEvent.CLICK, choose3_1);
removeChild(valchooser3);
}
function choose3_11() {
valchooser3.eleven.removeEventListener(MouseEvent.CLICK, choose3_11);
playerpoints += 10;
trace(playerpoints);
removeChild(valchooser3);
}
}
cardpos += 150;
playerpoints += cextra;
}
抽奖功能:
function drawcard(xpos: int, ypos: int, faceup: Boolean): int {
var rand: int = Math.floor(Math.random() * shoe.length);
var newcard: Karte = shoe[rand];
addChild(newcard);
trace("aktuelle Karte:" + getChildAt(numChildren - 1).name);
trace("draw:" + numChildren);
newcard.x = xpos;
newcard.y = ypos;
if (!faceup) {
toturn.push(newcard);
newcard.turncard();
}
shoe.splice(rand, 1);
return newcard.getcardval();
if (shoe.length <= 0) {
trace("shoe empty");
realshoe.gotoAndStop(2);
}
以及删除所有功能
function realreset() {
canBet = true;
while (numChildren != 0) {
removeChild(getChildAt(0));
}
startsinglegame();
}
有人知道解决办法吗?
谢谢!
您没有包含必要的代码,但最有可能发生的情况是您在每一轮额外注册一次点击处理程序(最终调用您的 extracard
函数)(这就是为什么n
在第 n
轮你得到了额外的牌)。
代码是这样的:
button.addEventListener(MouseEvent.CLICK, extracard);
现在,如果您点击按钮 3 次,它将调用 extracard
3 次。
为确保不会发生这种情况,请尝试
if (!button.hasEventListener(MouseEvent.CLICK)) {
button.addEventListener(MouseEvent.CLICK, extracard);
}
我正在尝试制作一个小的 21 点游戏。 我 运行 遇到了一个问题,我找不到我的错误。 当你在第一轮想要一张额外的牌时,你会得到一张牌。 (你必须按一个按钮才能得到它),但在第二轮,当你点击按钮时,该功能触发两次,在第三轮中触发 3 次,依此类推。 在回合之间,我从屏幕上删除所有内容。
这里是经常触发的函数:
function extracard() {
var cextra: int = drawcard(cardpos, 400, true);
trace("drew extra card");
if (cextra == 1) {
var valchooser3: _1or11 = new _1or11();
addChild(valchooser3);
valchooser3.one.addEventListener(MouseEvent.CLICK, choose3_1);
valchooser3.eleven.addEventListener(MouseEvent.CLICK, choose3_11);
valchooser3.x = 280;
valchooser3.y = 380;
//chipsandcardonscreen.push(valchooser3);
function choose3_1() {
valchooser3.one.removeEventListener(MouseEvent.CLICK, choose3_1);
removeChild(valchooser3);
}
function choose3_11() {
valchooser3.eleven.removeEventListener(MouseEvent.CLICK, choose3_11);
playerpoints += 10;
trace(playerpoints);
removeChild(valchooser3);
}
}
cardpos += 150;
playerpoints += cextra;
}
抽奖功能:
function drawcard(xpos: int, ypos: int, faceup: Boolean): int {
var rand: int = Math.floor(Math.random() * shoe.length);
var newcard: Karte = shoe[rand];
addChild(newcard);
trace("aktuelle Karte:" + getChildAt(numChildren - 1).name);
trace("draw:" + numChildren);
newcard.x = xpos;
newcard.y = ypos;
if (!faceup) {
toturn.push(newcard);
newcard.turncard();
}
shoe.splice(rand, 1);
return newcard.getcardval();
if (shoe.length <= 0) {
trace("shoe empty");
realshoe.gotoAndStop(2);
}
以及删除所有功能
function realreset() {
canBet = true;
while (numChildren != 0) {
removeChild(getChildAt(0));
}
startsinglegame();
}
有人知道解决办法吗?
谢谢!
您没有包含必要的代码,但最有可能发生的情况是您在每一轮额外注册一次点击处理程序(最终调用您的 extracard
函数)(这就是为什么n
在第 n
轮你得到了额外的牌)。
代码是这样的:
button.addEventListener(MouseEvent.CLICK, extracard);
现在,如果您点击按钮 3 次,它将调用 extracard
3 次。
为确保不会发生这种情况,请尝试
if (!button.hasEventListener(MouseEvent.CLICK)) {
button.addEventListener(MouseEvent.CLICK, extracard);
}