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"