So I have this code
function flip(e:MouseEvent)
{
//assign choice a random deck index.
choice=int(deck[Math.round((Math.random()*deck.length))]);
if(choice!=int(deck[9]))
{
//removeChild(MovieClip(e.target));
//position firecard.
addChild(fire);
fire.x=e.target.parent.x;
fire.y=e.target.parent.y;
//remove cardback
e.target.parent.removeChild(MovieClip(e.target));
fire.parent.setChildIndex(fire,numChildren-2);
trace(choice);
}
else if(choice==int(deck[9]))
{
trace(choice);
water.x=e.target.parent.x;
water.y=e.target.parent.y;
e.target.parent.removeChild(MovieClip(e.target));
water.parent.setChildIndex(water,numChildren-2);
}
}
Comments explain pretty much what everything is supposed to do. Flip is called via a listener from a movieclip. Choice is a number that is randomly picked every time flip is called, and gets its value from a random index from the deck Array. Then regardless of the choice, the card that was clicked is removed and in its position is placed a card according to the choice var. However, two errors happen (none of which do something in the Compiler or Output).
-
The new cards that get created (fire) are also removed when I click on another card. I want them to stay on place.
-
The trace always prints a 0. Shouldn’t it print like, anything from 0 to 9?
PS: Deck has 10 values. 9 of them are “fire”, the last is “water”. Choice starts with 0 initial value.
The problem is you are having string in the array(“fire” and “water”). But you are typecasting this string to an int, so turns to 0. Just try without typecasting the value of the array.
I didn’t change anything other than removing the typecasting.