Skip to main content
Inspiring
February 8, 2014
Answered

Converting Keydown to mouseclick/touchevent.

  • February 8, 2014
  • 1 reply
  • 903 views

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.

This topic has been closed for replies.
Correct answer kglad

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)
}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 8, 2014

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)
}

Inspiring
February 8, 2014

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?

kglad
Community Expert
Community Expert
February 8, 2014

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.