1
votes

Basically, I use Stencyl engine to make games. The engine uses Haxe and doesn't support everything that Haxe supports.

I want to manipulate he android 'back button' press. Stencyl supports code mode where I can write a haxe code. I wanted to overwrite android's default back button press. When player is playing the game, if he press back button, the game goes to background. Instead, I want the level to restart when backbutton is pressed. Can this be accomplished with any available source for Haxe?

I tried searching for any code help but can't find one. That's why messaging here. Appreciate any help.

P.S: Stencyl developer may not be a real developer for you. If that's the case, I'm not a developer. I'm just someone who uses all the resources to achieve what I want. So please avoid such topics which isn't going to help anybody.

1
Hi Andrew, thanks for the code. I'll try it. I have a small doubt. Where does it specify the back button? Is it the KEY_UP ? Edit: I think I got it. It's the .ESCAPE that mentions the key we choose isn't it? - Bhoopalan Thaati
Yep, KEY_UP is the event, when key up, and ESCAPE is code of the back button on Android - Andrew
Thanks soooo much for helping. I have some difficulty making it work but I will try to fix it myself for few more hours before bringing it here. - Bhoopalan Thaati
Also, there are two other buttons in android phone. I think they are named 'Home' and 'Menu'. Can these also be used in similar ways? That is, just replacing 'HOME' or 'MENU' at 'ESCAPE' in the above code will work? I could've tested it myself but since I can't test this code yet, I'm asking you the question. Once again thank you so much for your help. This brings lot more possibilities for me. - Bhoopalan Thaati

1 Answers

1
votes

You could try below code

Lib.current.stage.addEventListener(openfl.events.KeyboardEvent.KEY_UP, checkKeypress);

...

private function checkKeypress(e:openfl.events.KeyboardEvent):Void
{
    switch (e.keyCode)
    {
        case openfl.ui.Keyboard.ESCAPE:
            e.stopImmediatePropagation();
            restartLevel();
    }
}
private function restartLevel():Void
{
    //your code to restart level here
}