1

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.

npretto
  • 1,098
  • 7
  • 18
  • 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 Dec 31 '15 at 17:42
  • Yep, KEY_UP is the event, when key up, and ESCAPE is code of the back button on Android – Andrew Dec 31 '15 at 21:19
  • 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 Jan 01 '16 at 22:08
  • 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 Jan 01 '16 at 22:13

1 Answers1

1

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
}
Andrew
  • 1,282
  • 6
  • 11
  • I believe this should work, provided Stencly is using a recent version of lime. This behavior is analagous to Alt+Enter causing a fullscreen action on Windows, and a while back I talked to Joshua about making sure that behavior was suppress-able, and this was exactly the prescribed method he suggested. – larsiusprime Dec 31 '15 at 16:24
  • 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. 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 Jan 02 '16 at 00:33
  • Hi again, I'm receiving 'characters 0-7 : Unexpected private' error. I added this code inside when updating event. Thus, whenever player press the back button it will be recognized. Am I missing something? – Bhoopalan Thaati Jan 02 '16 at 00:35
  • I was wondering whether the error is with the '...' as it serves no purpose I assume? I'll also post the whole code shortly – Bhoopalan Thaati Jan 02 '16 at 00:37
  • You don't need to add this in each update. Add Lib.current.stage.addEventListener(openfl.events.KeyboardEvent.KEY_UP, checkKeypress); somewhere in initialization, and functions near others. As i remember you can't redefine HOME button – Andrew Jan 02 '16 at 16:44
  • I search a little, if you use something like http://www.stencyl.com/help/view/code-mode/ for code writing, you should write Lib.current.stage.addEventListener(openfl.events.KeyboardEvent.KEY_UP, checkKeypress); inside init function. As I understand Stencyl use "public inline" functions for some reason, so you should try replace "private" with "public inline" in my code above – Andrew Jan 02 '16 at 16:56
  • You seem to be of great help. Since I'm not a coder but learning to be, this helps a lot. I'll test this and update if it works. Thank you so much. – Bhoopalan Thaati Jan 13 '16 at 23:37
  • It throws an error 'Unknown Identifier: Lib' I wrote like this in code mode: code override public function init() { Lib.current.stage.addEventListener(openfl.events.KeyboardEvent.KEY_UP, checkKeypress); } public inline function checkKeypress(e:openfl.events.KeyboardEvent):Void { switch (e.keyCode) { case openfl.ui.Keyboard.ESCAPE: e.stopImmediatePropagation(); restartLevel(); } } public inline function restartLevel():Void { //your code to restart level here reloadCurrentScene(null, createCrossfadeTransition(0)); } – Bhoopalan Thaati Jan 13 '16 at 23:53
  • Sorry. I can't enter the code properly. I had a discussion in Stencyl forum who says that I must import lib but they don't remember the package name. I don't know the package name. I tried to find in Google but can't get relevant results. Any help? – Bhoopalan Thaati Jan 14 '16 at 01:33