Jump to content

Questions about tilemap interactions


BdR
 Share

Recommended Posts

I'm working on a platform game using a tilemap and the player continously bounces around the level. The player moves around like a kangaroo and only has to press left or right while hopping around to navigate the level.

I've tried setting body.bounce(1.0), but then the maximum height is always the same, the player always bounces up to his starting y-position. Which is not what I'm trying to do.

So every time the player bounces on top of on a tile he hops up to a certain height.
In other words, every bounce has the same height, when measured to the surface the player bounced on. See screenshots in attachement to see what I mean.

My question is; how can I make the player always bounce to the same height? :wacko:

phaser_bounce1.png

phaser_bounce2.png

Link to comment
Share on other sites

I just figured out that I can use the collide callback to set the y-velocity so it's always the same. However, the problem is that the player will always bounce up, also when it collides with a tile from the left or bottom.

So my new question is; is it possible to detect if the player sprite collides with a tile in the tilemap from the top/bottom/left/right side?

Never mind, you can check the player.body.blocked.down property for that :)

	update: function() {
		this.game.physics.arcade.collide(this._player, this._levellayer, this.collidePlayerTilemap);
	//..
	collidePlayerTilemap: function(pl, tm) {
		// only bounce from top of a tile
		if (pl.body.blocked.down) {
			// always bounce with the same force
			pl.body.velocity.y = -250;
		}
	}

 

phaser_bounce3.png

Link to comment
Share on other sites

I'm making some progress, but I've got another question so I'll just ask it here instead of opening many threads

New question: How to make certain tiles interact differently?

Specifically, I want some blocks to be solid, that already works fine. But I also want certain tiles to be "pass through blocks". So the player sprite can pass through them and only collide from the top, so they kind of work like ladders. The Super Mario and Commander Keen games also use this type of blocks a lot. See screenshot below.
 

phaser_bounce4.png

Link to comment
Share on other sites

I found a hint in another thread (here and herethat you can set the collision directions for each tile using Tile.setCollision(). So using the code below gives the result I was looking for in the previous post. :)

// adjust ladder tiles
for(var y = 0; y < this._levelmap.height; ++y) {
    for(var x = 0; x < this._levelmap.width; ++x) {
        var tile = this._levelmap.getTile(x, y);
        console.log(tile);
        if (tile) {
            if (tile.index == 3) { // bridge/ladder tile
                tile.setCollision(false, false, true, false); // setcollide for left, right, up, down
            };
        };
    };
};

The strange thing is though, that it should also work using Tilemap.forEach. But for some reason when I try the code below it does not apply the setCollision to all tiles where index==3. In the screenshot from the previous post, it only works for the platform with the exit-door on it, but not for the columns of tiles below it. :wacko: very strange

this._levelmap.forEach(
    function(tile) {
        if (tile) {
            if (tile.index == 3) {
                //debugger;
                tile.setCollision(false, false, true, false); // setcollide for left, right, up, down
            }
        }
    },
    this,
    0,
    0,
    this._levelmap.width,
    this._levelmap.height
);

 

Link to comment
Share on other sites

I published a game just like the one your're making (https://play.google.com/store/apps/details?id=com.purpleghost.pichonfull) last year so I can tell you how I ended up solving the same problems. 
All I say implies I'm using arcade physics though in retrospect maybe I should've used P2. You should test both now to see which one you find more suitable before it's too late. 

Regarding bounce, I have a variable that holds the jump speed and apply that whenever body.onFloor() is true. Using onFloor you avoid having it bounce when it touches the sides or bottom of a tile. 
Regarding pass through blocks, I didn't use any of those but collide() allows you to define a callback so you can do additional checks before returning true or false. you can check that the player is falling and colliding with the top of the pass-through tile or return false otherwise.

When you collide a tilemap with a sprite you can also decide what to do besides separation in a collision callback. The callback will provide a reference to the sprite and the tile that are colliding, so you can check the tile index and decide what to to in each case. Is is a spring? Let's make the player jump higher and faster. Is it a spike? Let's kill the player. Is it the exit door? Let's exit the level. 

You can have collision callbacks assigned to a tileindex with setTileIndexCallback() but if every tile in the map is gonna have a callback then I think it's better to just add a callback to collide() and check the tile index yourself

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...