Skip to main content
Known Participant
January 9, 2013
Question

if(Key.isDown) to be used in a button

  • January 9, 2013
  • 2 replies
  • 651 views

Hi guys! Please help me T_T

I am making a android game that has a left and right movement.. I made two buttons for the left and right movements.

What i want is to make my character move when I press the left/right button, i don't need to use keyboard movements..

So how can i make my character move using the left/right button.

These are my codes actionscript 3.0:

import flash.events.Event;

hero.gotoAndStop('still');

stage.addEventListener(Event.ENTER_FRAME,onenter)

function onenter(e:Event):void{

          if(Key.isDown(Key.RIGHT)){

                    hero.x+=5;

                    hero.scaleX=-1;

                    hero.gotoAndStop('walking');

          }

          else if(Key.isDown(Key.LEFT)){

                    hero.x-=5;

                    hero.scaleX=1;

                    hero.gotoAndStop('walking');

          }

          else hero.gotoAndStop('still');

          }

This topic has been closed for replies.

2 replies

natural_criticB837
Legend
January 9, 2013

Hey,

instead of trying to check the status of the button in the onenter handler, just add MouseEvent.MOUSE_DOWN and MouseEvent.MOUSE_UP listeners, write the status to a boolean variable and use that for evaluation in your onenter handler.

Known Participant
January 10, 2013

I did it! Thanks! .. I can now move my character with the MouseEvent.MOUSE_DOWN for moving my character on the 'moving' animation and the MouseEvent.MOUSE_UP for moving my character on the 'still' animation..

But i still have one problem .. I cannot make my character jump, my codes on the KeyboardEvent works but if i tried to make it work with the MouseEvent it does not move.. Please Help! I don't know what to do with the   if (Key.isDown(Key.UP) && canjump)  codes

import flash.events.Event;

hero.gotoAndStop('still');

var Key:KeyObject = new KeyObject(stage);

var dy:Number=0;

var gravity:Number=1;

var canjump:Boolean = false;

jump_btn.addEventListener(MouseEvent.CLICK, onenter);

function onenter(Event:MouseEvent):void{

          dy+=gravity;

          if (hero.y>300){

                    dy=0;

                    canjump=true;

          }

          if (Key.isDown(Key.UP) && canjump){

                    dy=-20;

                    canjump = false;

          }

          hero.y+=dy;

}

right_btn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);

function mouseDown(e:Event):void{

                    hero.x+=5;

                    hero.scaleX=-1;

                    hero.gotoAndStop('walking');

                    stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);

          }

function mouseUp(e:Event):void{

                    hero.gotoAndStop('still');

                    stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);

          }