Change Variable on Mouse Event
I'm trying to change the variable that's set to 0 into 1 on a mouse event. I have 3 movie clips 'maskedbgmc' and when clicked its supposed to change a variable. But it doesn't change the variable as far as i can see. What do i miss?
Thanks for your time :)
var checkCard1:Number = 0;
maskedbg_mc.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked1);
function cardChecked1 (event:MouseEvent):void {
checkCard1 = 1;
}
var checkCard2:Number = 0;
maskedbg_mc2.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked2);
function cardChecked2 (event:MouseEvent):void {
checkCard2 = 1;
}
var checkCard3:Number = 0;
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
functio开发者_高级运维n cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
}
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
Note that events happen asynchronously. This means that your if statements will execute the moment this code is interpreted, but the variables will only be modified when the mouse events occur. If you trace your variables value inside your event handler functions, you will likely see that it does indeed change when the mouse button is pressed.
Maybe what you want to do is add your if statements into a function, like so:
function checkCards() : void {
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}
You can then invoke this method inside your event listeners, and it will check the card variables using the above logic. An example of it used inside the cardChecked3() method:
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
checkCards();
}
Hope this helps.
Does the event have to be a MouseEvent.MOUSE_DOWN? If you are only interested in clicks I would try changing that to a MouseEvent.CLICK.
Just a thought tho.
Try changing your code to this for the last part
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}
精彩评论