Sign in to follow this  
Ravetracer

Creating On-Screen controls for mobile devices

Recommended Posts

Hi there,

currently I'm trying to figure out, how to make on screen controls for mobile devices work.

In the HUD I added some GUI objects and tried to trigger key events, when the objects are "hold".
Also the buttons are appearing on the screen, but unfortunately they don't work.
I'd also tried to use "onClick" event, but it didn't work either.

Any ideas?
 

game.HUD.buttonLeft = me.GUI_Object.extend({
    init : function (x, y) {

        var settings = {};
        settings.image = 'button_left';
        settings.framewidth = 40;
        settings.frameheight = 40;

        this._super(me.GUI_Object, "init", [x, y, settings]);
        this.pos.z = 4;
    },

    onHold : function () {
        me.input.triggerKeyEvent(me.input.KEY.LEFT, true);
        return false;
    }
});

game.HUD.buttonRight = me.GUI_Object.extend({
    init : function (x, y) {

        var settings = {};
        settings.image = 'button_right';
        settings.framewidth = 40;
        settings.frameheight = 40;

        this._super(me.GUI_Object, "init", [x, y, settings]);
        this.pos.z = 4;
    },

    onHold : function () {
        me.input.triggerKeyEvent(me.input.KEY.RIGHT, true);
        return false;
    }
});

game.HUD.buttonJump = me.GUI_Object.extend({
    init : function (x, y) {
        var settings = {};
        settings.image = 'button_jump';
        settings.framewidth = 40;
        settings.frameheight = 40;

        this._super(me.GUI_Object, "init", [x, y, settings]);
        this.pos.z = 4;
    },

    onHold : function () {
        me.input.triggerKeyEvent(me.input.KEY.UP, true);
        return false;
    }
});

 

Bildschirmfoto 2016-12-29 um 12.35.41.png

Share this post


Link to post
Share on other sites

Hi,

To use this callback, you have to set the isHoldable property to true. That should fix the issue with the callback not firing. But I've never used it, so I don't really know what to expect. From the source code, it looks like the callback fires once and only once. And the timer value controls how long the button must be held before the callback fires.

If you want a callback to trigger every frame, you need to override the `update` method. You can use the various properties that keep track of click state: https://github.com/melonjs/melonJS/blob/4.0.0/src/renderable/GUI.js#L96-L97

That said however, it's probably better if you use the `onClick` and `onRelease` callbacks with `me.input.triggerKeyEvent`; the second param indicates whether the key event is a click (true) or a release (false), and that maps nicely to the GUI_Object click and release callbacks.

Share this post


Link to post
Share on other sites

else there are a few "virtual joystick" out there, that probably worth checking :

https://github.com/jeromeetienne/virtualjoystick.js

https://github.com/austinhallock/html5-virtual-game-controller

.. and other (google returns a long list)

I always wanted to look at how to turn one of those as a plugin for melonJS, but it's behind a long list of other things to do :)

Share this post


Link to post
Share on other sites
On 11.1.2017 at 11:49 AM, obiot said:

@Ravetracer just curious, did you make any progress with this ?

Unfortunately I hadn't the time to try it. You know, XMas, New Years Eve and then work again ;-).
But I'll try next days or so when everything calmed down a bit.

On 11.1.2017 at 11:53 AM, obiot said:

else there are a few "virtual joystick" out there, that probably worth checking :

https://github.com/jeromeetienne/virtualjoystick.js

https://github.com/austinhallock/html5-virtual-game-controller

.. and other (google returns a long list)

I always wanted to look at how to turn one of those as a plugin for melonJS, but it's behind a long list of other things to do :)

Yes, looks interesting. Maybe I should try one of these "virtual devices" so prevent to inventing the wheel again ;).

Thank you, guys!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.