Copy link to clipboard
Copied
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.
it looks like you're only detecting an up key. if that's true you can use:
var jumpBool:Boolean;
stage.focus=stage;
stage.addEventListener(MouseEvent.MOUSE_DOWN,jumpF);
function jumpF(event:MouseEvent):void {
jumpBool=true;
yspeed=yaccelleration;
}
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_FR
Copy link to clipboard
Copied
it looks like you're only detecting an up key. if that's true you can use:
var jumpBool:Boolean;
stage.focus=stage;
stage.addEventListener(MouseEvent.MOUSE_DOWN,jumpF);
function jumpF(event:MouseEvent):void {
jumpBool=true;
yspeed=yaccelleration;
}
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 (jumpBool) {
jumpBool=false;
crunk.y-=yspeed;
if (crunk.y<=ground) {
jumping=false;
}
}
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)
}
Copy link to clipboard
Copied
Thanks, and that worked just fine But what would i change to incorporate both a click to jump, and a hold to jump that i could change based on a level or difficulty?
Copy link to clipboard
Copied
to change something based on how long the stage is pressed, use a mousedown listener function to trigger a loop that would repeatedly update a variable until a mouseup is detected.
that variable's updated value could be made a function of the level or difficulty.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now