Moving one tile at a time (redux)

97 views
Skip to first unread message

ab5tract

unread,
Jun 19, 2012, 5:20:32 AM6/19/12
to mel...@googlegroups.com
Since my concerns seem to be somewhat different than the previous "Moving one tile at a time" thread (I'm not worried about traveling N pixels in N seconds, I am concerned with "only ever moving N pixels"), and because I've been erroneously posting new problems on the "Spritesheets not animating" thread, I wanted to raise an issue I am having in a fresh topic.

The game I am working on is a very simple Pacman-like. Because there are a lot of twists to the mazes, I want to be able to move the player 32px at a time. It seemed to me that using vel.x for this purpose is not appropriate, because I want to move a specific number of pixels without interacting with framerates and other variables that may influence vel.x.

To that end, I developed the following logic that, unfortunately, doesn't work. Am I barking up the wrong tree? Is it actually better to be using vel.x somehow here? IMO, I think it would be good to develop a canonical approach to this that we can then include in melonJS (I will be contributing code to this great project once I am finished with freelancing).

This is inside the player update function (I have added this.tile in the init function, setting it to a string value in order to determine that when it returns as null (which is what happens*) that this.tile has in fact been changed).

    else if (me.input.isKeyPressed('right')) 
    {
        this.nextX = this.pos.x + 32;
        console.log("we want to go here: " + this.nextX);
        console.log("what collision layer: " + this.collision);
        this.tile = me.game.collisionMap.getTile(this.nextX, this.pos.y);
        console.log("what tile: " + this.tile);
        if( ! me.game.collisionMap.tileset.isTileCollidable(this.tile.tileId) ) {
          this.pos.x = this.nextX;
          console.log("we should be here: " + this.pos.x);
        }
        
        if(this.isRight == false) {
          this.flipX(false); 
          this.isRight = true;
        }
    } 


* the console.log that outputs this.tile always prints "what tile: null"

Orthonormal

unread,
Jun 20, 2012, 7:29:11 AM6/20/12
to mel...@googlegroups.com
1. If you just want to move N pixel instantly in 1 frame, just "this.vel.x = 32" would do it.
2. If you want to move N pixel over X milliseconds, then you have the same issues as I do.

Actually, I don't understand why the basic MelonJS movement doesn't suit your project. Perhaps you can post an example of what you're trying to do? You can link us to your game and tell us why the current movement doesn't suit the game and let us try it out to see for ourselves.

Since I have some experience with this topic, I might be able to help if I can get a clearer picture.

melonJS

unread,
Jun 22, 2012, 9:30:30 AM6/22/12
to mel...@googlegroups.com
Hi guys,

Quickly a few hints.

When doing vel.x+=value, it's actually adding the given value to the current velocity, so if you want a fixed step it's better to rather to vel = value

Note as well that the update function is triggered every frame (60 times per second by default or less if you changed the fps settings) which means that you could also count the passed frame and only set vel once you reach 60 ( for example), else do vel = 0

Also you can disable key repetition by using true for the last parameter of the bind key function, see here :
http://www.melonjs.org/docs/symbols/me.input.html#bindKey

In that case, the user has to release the key and press it again before being able to move again

I hope this will help you,
Cheers,
Oliver

Reply all
Reply to author
Forward
0 new messages