logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
wini3d  
#1 Posted : Thursday, September 26, 2019 9:33:53 PM(UTC)
wini3d

Rank: Newbie

Groups: Registered
Joined: 9/26/2019(UTC)
Posts: 0

Hi, new to melonjs and I was wondering if it supports tile based movement.
astupidnerd  
#2 Posted : Monday, October 14, 2019 6:41:14 AM(UTC)
astupidnerd

Rank: Newbie

Groups: Registered
Joined: 10/14/2019(UTC)
Posts: 0
United States

Hello.

It doesn't look like it has it built in, but you can add this functionality yourself pretty easily.

What I did was to add a moving flag to the entity that checked whether or not it was moving to the next tile.

Inside the update method, I checked for up/down/left/right keys being pressed if it was not moving, then I would set the moving flag to true, and set the body's x/y velocity accordingly. Once the entity reached (or surpassed) the point I wanted it to move to, I would set the velocity to 0, adjust its position so it was centered on the tile, and then set the moving flag to false.

Here is a snippet of the relevant code.

Note: I just wrote this this morning, so it's not going to be perfect. Use it as a starting point.

Code:

    if (this.moving) {
      if (this.move_direction == 'up' && this.pos.y <= this.to_y) {
        this.pos.y = this.to_y;
        this.stopMoving();
      } else if (this.move_direction == 'down' && this.pos.y >= this.to_y) {
        this.pos.y = this.to_y;
        this.stopMoving();
      } else if (this.move_direction == 'left' && this.pos.x <= this.to_x) {
        this.pos.x = this.to_x;
        this.stopMoving();
      } else if (this.move_direction == 'right' && this.pos.x >= this.to_x) {
        this.pos.x = this.to_x;
        this.stopMoving();
      }
    }

    // This is not done inside an else to avoid a 1 frame hangup when constantly moving
    if (!this.moving) {
      if (me.input.isKeyPressed('left')) {
        this.moveLeft();
      } else if (me.input.isKeyPressed('right')) {
        this.moveRight();
      } else if (me.input.isKeyPressed('up')) {
        this.moveUp();
      } else if (me.input.isKeyPressed('down')) {
        this.moveDown();
      }
    }

    // Also not checking inside the above if so that the idle animation only plays when they
    // are really not moving
    if (!this.moving && !this.renderable.isCurrentAnimation(`stand-${this.move_direction}`)) {
      this.renderable.setCurrentAnimation(`stand-${this.move_direction}`);
    }
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.