javascriptcraftyjs

About Crafty - Isometric and gravity; also fourway


I have two questions about Crafty (I've also asked in their google group community, but it seems very few people look at that).

I've followed this tutorial http://buildnewgames.com/introduction-to-crafty/ and also took a look at the "isometric" demo in crafty's website of a bunch of blocks (http://craftyjs.com/demos/isometric/). And I've been trying some stuff by combining what I've learned in both.

(Q1) When I use the fourway component (used in the tutorial a lot), if I hold the left arrow key for example and CTRL-TAB out the current tab while holding left, and then go back (not necessarily holding left anymore), then the my character seems to get stuck in moving to the "left" direction. It also happens for the other 3 directions. Is that a known issue? Is there anyway to fix it without changing crafty?

It happens here with firefox 29 and chrome 34. My code is pretty much the one in the final version presented at the tutorial's end (it's not the same, but even when it was the same I already had this issue).

By the way, when this happens, if I CTRL-TAB out and back again holding that left key, things go back to normal (the movement stops).

(Q2) The isometric-ish features interprets Z as being height, and the gravity component uses Y for height. Isn't this a problem? Can I, maybe, for example, tell gravity to use something else, other than y, for height?


Solution

  • Regarding (Q1), the movement is managed by keydown and keyup events. If you change the tab when the start of a movement was triggered, the fourway component never gets any keyup event to stop again. You could use a workaround like the following:

    Crafty.settings.modify("autoPause", true);
    

    Enabling autoPause (somewhere in your init function) will pause your game when the browser tab crafty is running in is inactive. You can then react to this event by triggering keyup events or preventing the player component to move like this:

    player.bind('Pause', function() {
        this.disableControl();
    });
    
    player.bind('Unpause', function() {
        this.enableControl();
    });
    

    You might want to stop animation there too if you handle that in your player component..