Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using a symbol buttons as a keyboard buttons in a mobile AIR application

Guest
Oct 28, 2016 Oct 28, 2016

I have four buttons in my stage and I would like to set every button with its equivalent in keyboard buttons, you can say like a shortcut buttons. this is the instance name of my buttons with keyboard buttons.

  • "btn1" for Left arrow
  • "btn2" for Right arrow
  • "btn3" for Up arrow
  • "btn4" for Down arrow

I added an eventListener to every button, but I don't know what can I put inside the function.

btn1.addEventListener(MouseEvent.CLICK, Left);
function Left(event:MouseEvent😞void
{
//Left arrow was pressed
}

is there any way to do this?

Thanks.

TOPICS
ActionScript
582
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 29, 2016 Oct 29, 2016

You cannot control the keyboard with actionscript, but you can mimic what the keyboard keys do by programming the buttons to take the same actions as what you code for the keys.   If you code the right arrow keyboard key to make something move right, then you use that same moving right code for the button that you want to make things move right.

Example...

var goLeft:Boolean = false;  // create goRight, goUp, goDown as well for the other controls

function hearKeyDown(yourEvent:KeyboardEvent):void{ 

...
Translate
LEGEND ,
Oct 29, 2016 Oct 29, 2016

You cannot control the keyboard with actionscript, but you can mimic what the keyboard keys do by programming the buttons to take the same actions as what you code for the keys.   If you code the right arrow keyboard key to make something move right, then you use that same moving right code for the button that you want to make things move right.

Example...

var goLeft:Boolean = false;  // create goRight, goUp, goDown as well for the other controls

function hearKeyDown(yourEvent:KeyboardEvent):void{    // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{        // you can include all the keys in here

    if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseDown(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = true; }   

    stage.addEventListener(Event.ENTER_FRAME, moveObject);

}

function hearMouseUp(yourEvent:MouseEvent):void{    // you can include all the buttons in here

    if (yourEvent.currentTarget==btn1){ goLeft = false; }   

    stage.removeEventListener(Event.ENTER_FRAME, moveObject);

}

function moveObject(evt:Event):void {                                // share this for all the keys and buttons

    if (goLeft){ object_mc.x-=5 };   

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

btn1.addEventListener(MouseEvent.MOUSE_DOWN, hearMouseDown);

btn1.addEventListener(MouseEvent.MOUSE_UP, hearMouseUp);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 29, 2016 Oct 29, 2016

Thank you sir for your reply, but I don't want to control the keyboard, I just want to make these buttons equal to keyboard arrow keys and doing the same things if they pressed.

I am using these buttons for mobile to control an external flash game.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2016 Oct 30, 2016

Please reread my response - What you just asked for is what my posting explained.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 12, 2018 Dec 12, 2018
LATEST

Hello Master Ned, I'm new to As3, my question is also this ... please try to implement this technique, but give me errors, and I do not know how to solve them ... I know it will sound a little rough, but you could Please pass this code to me in a more basic way with examples please

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines