actionscript 3 - Adobe Flash CS6 AS3: KeyUp Handler triggers immediately after KeyDown Handler -
so in code attempting make had 2 players, each player had own key down , key handlers, labelled in attached code, , in handle movement, player 1 has wsad , player 2 uses left, right, , down keys, however, when ever press relative key down handler, fires key handler straight after, , not know why, here code, if great! :)
import flash.display.movieclip; import flash.events.event; import flash.events.mouseevent; import flash.events.keyboardevent; import flash.ui.keyboard; public class main extends movieclip{ var playeroneleft:boolean = new boolean(false); var playeroneright:boolean = new boolean(false); var playeroneup:boolean = new boolean(false); var playeronedown:boolean = new boolean(false); var playertwoleft:boolean = new boolean(false); var playertworight:boolean = new boolean(false); var playertwoup:boolean = new boolean(false); var playertwodown:boolean = new boolean(false); var playerspeed:int = 5; var gamepage:gamepage; public function main(){ gamepage = new gamepage(); addchild(gamepage); addallplayerhandlers(); } public function playeronekeydownhandler(event:keyboardevent){ if(event.keycode == keyboard.a){ playeroneleft = true; trace("a"); } else if(event.keycode == keyboard.d){ playeroneright = true; trace("d"); } else if(event.keycode == keyboard.w){ playeroneup = true; trace("w"); } else if(event.keycode == keyboard.s){ playeronedown = true; trace("s"); } } public function playeronekeyuphandler(event:keyboardevent){ if(event.keycode == keyboard.a){ playeroneleft = false; trace("a"); } else if(event.keycode == keyboard.d){ playeroneright = false; trace("d"); } else if(event.keycode == keyboard.w){ playeroneup = false; trace("w"); } else if(event.keycode == keyboard.s){ playeronedown = false; trace("s"); } } public function playertwokeydownhandler(event:keyboardevent){ if(event.keycode == keyboard.left){ playertwoleft = true; trace("left"); } else if(event.keycode == keyboard.right){ playertworight = true; trace("right"); } else if(event.keycode == keyboard.up){ playertwoup = true; trace("up"); } else if(event.keycode == keyboard.down){ playertwodown = true; trace("down"); } } public function playertwokeyuphandler(event:keyboardevent){ if(event.keycode == keyboard.left){ playertwoleft = false; trace("left"); } else if(event.keycode == keyboard.right){ playertworight = false; trace("right"); } else if(event.keycode == keyboard.up){ playertwoup = false; trace("up"); } else if(event.keycode == keyboard.down){ playertwodown = false; trace("down"); } } public function playermovementhandler(event:event):void{ if(playeroneleft){ gamepage.playerone.x -= playerspeed; } else if(playeroneright){ gamepage.playerone.x += playerspeed; } else if(playeroneup){ gamepage.playerone.y += playerspeed; } else if(playeronedown){ gamepage.playerone.y -= playerspeed; } if(playertwoleft){ gamepage.playertwo.x -= playerspeed; } else if(playertworight){ gamepage.playertwo.x += playerspeed; } else if(playertwoup){ gamepage.playertwo.y += playerspeed; } else if(playertwodown){ gamepage.playertwo.y -= playerspeed; } } public function playeronecollision(event:event):void{ if(gamepage.playerone.hittestobject(gamepage.leftwall)){ gamepage.playerone.x += playerspeed; } if(gamepage.playerone.hittestobject(gamepage.rightwall)){ gamepage.playerone.x -= playerspeed; } if(gamepage.playerone.hittestobject(gamepage.topwall)){ gamepage.playerone.y -= playerspeed; } if(gamepage.playerone.hittestobject(gamepage.bottomwall)){ gamepage.playerone.y += playerspeed; } if(gamepage.playerone.hittestobject(gamepage.middlewall)){ gamepage.playerone.x -= playerspeed; } } public function playertwocollision(event:event):void{ if(gamepage.playertwo.hittestobject(gamepage.leftwall)){ gamepage.playertwo.x += playerspeed; } if(gamepage.playertwo.hittestobject(gamepage.rightwall)){ gamepage.playertwo.x -= playerspeed; } if(gamepage.playertwo.hittestobject(gamepage.topwall)){ gamepage.playertwo.y -= playerspeed; } if(gamepage.playertwo.hittestobject(gamepage.bottomwall)){ gamepage.playertwo.y += playerspeed; } if(gamepage.playertwo.hittestobject(gamepage.middlewall)){ gamepage.playertwo.x += playerspeed; } } public function addplayermovementhandler(){ stage.addeventlistener(event.enter_frame,playermovementhandler); } public function addplayerkeyhandlers(){ stage.addeventlistener(keyboardevent.key_down,playeronekeydownhandler); stage.addeventlistener(keyboardevent.key_down,playeronekeyuphandler); stage.addeventlistener(keyboardevent.key_up,playertwokeydownhandler); stage.addeventlistener(keyboardevent.key_up,playertwokeyuphandler); } public function addplayercollisionhandlers(){ gamepage.playerone.addeventlistener(event.enter_frame,playeronecollision); gamepage.playertwo.addeventlistener(event.enter_frame,playertwocollision); } public function addallplayerhandlers(){ addplayerkeyhandlers(); addplayercollisionhandlers(); addplayermovementhandler(); } }
when ever press relative key down handler, fires key handler straight after
this because registered them as
stage.addeventlistener(keyboardevent.key_down,playeronekeydownhandler); stage.addeventlistener(keyboardevent.key_down,playeronekeyuphandler); // type key_down, handler "up" stage.addeventlistener(keyboardevent.key_up,playertwokeydownhandler); //that looks suspicious, stage.addeventlistener(keyboardevent.key_up,playertwokeyuphandler);
the lesson should learn copy & paste approach writing code bad practice. should not repeat yourself. duplicated code leads problems 1 @ hand.
Comments
Post a Comment