Key runs function multiple times
Hello!
I have a game that is pretty simple, you hit the right key or button and the right animation plays, if you hit the wrong key or button you try over and over.
Does anybody know why this code runs the correct funtion more than once when the keyboard is used but no when the mouse is used?
I know because it gets traced 2-3 times and doesn't run properly. Genius I know.
Thanks!
stop();
var answerObj2:Object={};
answerObj2["question 1"]=[mc_1,Keyboard.LEFT];
answerObj2["question 2"]=[mc_2,Keyboard.DOWN];
answerObj2["question 3"]=[mc_3,Keyboard.UP];
answerObj2["question 4"]=[mc_4,Keyboard.RIGHT];
for(var k:int=1;k<=4;k++){
this["mc_"+k].addEventListener(MouseEvent.CLICK,clickF2);
}
function clickF2(e:MouseEvent):void{
if(answerObj2[currentQuestion][0]==e.currentTarget){
correctAnswerF2(MovieClip(e.currentTarget));
} else {
incorrectAnswerF2(MovieClip(e.currentTarget));
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyBoardControl2);
function keyBoardControl2 (e:KeyboardEvent):void {
if(answerObj2[currentQuestion][1] == e.keyCode){
correctAnswerF2(MovieClip(answerObj2[currentQuestion][0]));
} else if(answerObj2["question 2"][1] == e.keyCode || answerObj2["question 3"][1] == e.keyCode || answerObj2["question 1"][1] == e.keyCode){
incorrectAnswerF2(MovieClip(answerObj2[currentQuestion][0]));
}
}
function correctAnswerF2(mc:MovieClip):void{
yesMC.continueMC.addEventListener(MouseEvent.MOUSE_DOWN, callbacksec);
TweenMax.to(yesMC, .3, {alpha:1});
yesMC.continueMC.buttonMode = true;
setChildIndex(yesMC,numChildren - 1);
var channel7:SoundChannel = sndright.play(0, 1);
ridAll();
function ridAll () {
haloMC.visible = false;
barMC.visible = false;
navMC.visible = false;
mc_4.visible = false;
mc_1.visible = false;
mc_2.visible = false;
mc_3.visible = false;
fadedMC.visible = false;
myText_txt.visible = false;
trace("interface gone.");
}
//enter key continues game
stage.addEventListener(KeyboardEvent.KEY_DOWN,handler);
function handler(e:KeyboardEvent):void{
if(e.charCode == 13){
callbacksec();
}
}
//correct sequence
function callbacksec():void {
yesMC.continueMC.buttonMode = false;
yesMC.continueMC.removeEventListener(MouseEvent.MOUSE_DOWN, callbacksec);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,handler);
TweenMax.to(yesMC, .3, {alpha:0});
boatMC.gotoAndPlay(1);
waterPan.play();
beaconLand.play();
TweenMax.to(boatMC, 1, {x:535});
//tween buoy out
channel02.stop();
//start rev
var snd2:Sound = new Rev();
var channel2:SoundChannel = snd2.play(0, 1);
var p = setTimeout(callbackp, 3000);
function callbackp():void {
var fade3 = setTimeout(fadetimer3, 2000);
function fadetimer3():void {
play();
};
var myTween19:Tween = new Tween(my_box, "alpha", Strong.easeOut, 0, 1, 1, true);
};
}
}
function incorrectAnswerF2(mc:MovieClip):void{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyBoardControl2);
var channelwrong:SoundChannel = sndwrong.play();
noMC.alpha = 1;
setChildIndex(noMC,numChildren - 1);
var call3 = setTimeout(callbacka, 1000);
function callbacka():void {
setChildIndex(noMC,0);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyBoardControl2);
//mc.currentTarget.gotoAndStop(1);
};
//mc.currentTarget.gotoAndStop(3);
}
currentQuestion = "question 4";
var myTween18:Tween = new Tween(my_box, "alpha", Strong.easeOut, 1, 0, 1, true);
this.focusRect = false;
//initiate question text
myText_txt.htmlText = "According to your direction of travel, which side of your boat should this Day Beacon (Daymark) be kept on? ";
//play motor idle
var channel02:SoundChannel = snd1.play(0, 9999);
mc_1.buttonMode = true;
mc_4.buttonMode = true;
mc_3.buttonMode = true;
mc_2.buttonMode = true;
