actionscript 3 - Movement not working AS3 -
i'm working on game, , new as3.
i'm stuck on character movement: following guide , ended following code. when test game plays character animation , can't control it.
package { import flash.display.stage; import flash.events.keyboardevent; import flash.ui.keyboard; import flash.utils.proxy; import flash.utils.flash_proxy; dynamic public class keyobject extends proxy { private static var stage:stage; private static var keysdown:object; public function keyobject(stage:stage) { construct(stage); } public function construct(stage:stage):void { keyobject.stage = stage; keysdown = new object(); stage.addeventlistener(keyboardevent.key_down, keypressed); stage.addeventlistener(keyboardevent.key_up, keyreleased); } flash_proxy override function getproperty(name:*):* { return (name in keyboard) ? keyboard[name] : -1; } public function isdown(keycode:uint):boolean { return boolean(keycode in keysdown); } public function deconstruct():void { stage.removeeventlistener(keyboardevent.key_down,keypressed); stage.removeeventlistener(keyboardevent.key_up, keyreleased); keysdown = new object(); keyobject.stage = null; } private function keypressed(evt:keyboardevent):void { keysdown[evt.keycode] = true; } private function keyreleased(evt:keyboardevent):void { delete keysdown[evt.keycode]; } } } package { import flash.display.sprite import flash.events.event; import keyobject; public class main extends sprite{ private var key:keyobject; public function main() { addeventlistener(event.added_to_stage,setupkeyobject); } function setupkeyobject(e:event){ key = new keyobject(stage); stage.addeventlistener(event.enter_frame,moveplayer); } function moveplayer(e:event){ if(key.isdown(key.left)){ roy.x -= 5; } if(key.isdown(key.right)){ roy.x +=5; } if(roy.x<0){ roy.x = 0; } if(roy.x > (stage.stagewidth - player.width)){ roy.x = stage.stagewidth - player.width; } } } }
Comments
Post a Comment