I am trying to make a little blackjack game. I ran in to a problem, ant I cant find my mistake. When you want an extra card the first round, you get one card. (You have to press a button to get it), but in the second round, when you click the button the function triggers twice, in the third round 3 times and so on. Between the rounds I remove everything from screen.
Here is the function that trigger to often:
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;
}
The drawcard function:
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);
}
And the remove everything function
function realreset() {
canBet = true;
while (numChildren != 0) {
removeChild(getChildAt(0));
}
startsinglegame();
}
Does anyone know a solution?
Thanks!
You didn't include the necessary code, but what's most likely happening is that you are registering your click handler (that eventually calls your extracard
function) an additional time each round (which is why for the n
'th round you are getting n
extra cards).
The code is something like:
button.addEventListener(MouseEvent.CLICK, extracard);
Now if you click your button 3x, it will call extracard
3x.
To ensure this doesn't happen, try something like
if (!button.hasEventListener(MouseEvent.CLICK)) {
button.addEventListener(MouseEvent.CLICK, extracard);
}