I am creating a simple game, where crunk, flies up and down with the help of gravity. At the moment, my code only works for the arrow keys on the keyboard, and I am looking to convert the code so that the user could press on the screen/click to make Crunk fly. In short, I am trying to convert a keydown event, to a touchevent or mouseclick event.I am fairly new to flash so go easy on me.
Current code:
var keysDown:Array=[];
stage.focus=stage;
stage.addEventListener(KeyboardEvent.KEY_DOWN,addKey);
stage.addEventListener(KeyboardEvent.KEY_UP,removeKey);
function addKey(event:KeyboardEvent):void {
keysDown[event.keyCode]=true;
}
function removeKey(event:KeyboardEvent):void {
keysDown[event.keyCode]=false;
}
var timer:Timer=new Timer(25);
timer.addEventListener(TimerEvent.TIMER, tick);
timer.start ()
function tick(event:Event):void {
if (keysDown[Keyboard.RIGHT]) { trace("right key is down!"); }
}
var accelleration:Number=2;
var friction:Number=0.5;
var maxspeed:Number=15;
var backmaxspeed:Number=-15;
var yspeed:Number=0;
var gravity:Number=2;
var yaccelleration:Number=25;
var ground:Number=crunk.y;
var jumping:Boolean=false;
addEventListener(Event.ENTER_FRAME, jump);
function jump(e:Event):void {
if (jumping) { crunk.y-=yspeed; if (crunk.y<=ground) { jumping=false; } } if ((keysDown[Keyboard.UP])) { jumping=true; yspeed=yaccelleration; } crunk.y-=yspeed; //Keeping the crunk within screen boundaries if ((crunk.y < ground)&&(jumping == false)) { yspeed-=gravity; } if (crunk.y>ground) { crunk.y=ground; } if (crunk.y< -121) { crunk.y=-119; } if (crunk.y< -115) { yaccelleration=0; } else (yaccelleration = 25)
}
Any help would be extremely helpful! If anyone could rewrite this code for mouseclick using the same variables that would be fine as well.