Jump to content

how to detect if a player is inside an tilemap area


marvster
 Share

Recommended Posts

I'm working on a jrpg/pokemon-like game mechanics and wonder how to check if the player is in a certain area (battlezone).

 

My basic Idea was to create a map of certain layers for certain actions:

 

1. background: full filled with ground textures, no physics

2. battlezone: if a tile is exists and player overlaps with one or many of it or is totally inside the tile, I like to count tics for random encounter of an enemy, if there is no more overlapping or inbounds of the area, tic will be reseted. later this tiles will ahve a property to have a zone-identifier to use for choosing enemies. At the moment I use high grass to visualize (as I don't created any sprites of my self atm), in future it may be invisible and sounds will be used for conveyance.

3. obstacles: all tiles a body enabled and player cant pass

 

(later/ nice to have:)

4. overtop: tiles over player and background, like large trees, laterns etc to simulate depth. if player overlaps (or is inside) the opacity should decrease to have the player in focus

5. sky: no collisions here, just clouds, fog, birds and random stuff for additional depth and atmosphere

 

At the moment I figured out how to manage background an obstacles and I'm now stuck in the battlezone.

Here is my demo: http://tannhauser-gate.net/projects/games/tilemap-test/ All high grass marks battle zone.

 

Thats my layer:

this.layer2 = this.map.createLayer('battlezone');this.layer2.resizeWorld();game.physics.arcade.enable(this.layer2);this.map.setCollisionByExclusion([],true,this.layer2);

and in update I use: checkBattlezone()

which is:

checkBattlezone: function() {        (game.physics.arcade.overlap(this.player, this.layer2)) ? console.log('battlezone')  : '';    }

I figured out that I'm supposed to define collision on the map.

As a result my player can't enter the zone and if forced (it may happen from time to time) and the player is inside to collision area overlap no longer counts.

 

So finally, here's my question:

How can I manage to detect if the player overlaps or is inside a tilearea of the given layer, without baring him out?

Did I missed something, or is the solution I prefere not avaiable in phaser?

Link to comment
Share on other sites

You need to look at tile functions. You can use an overlap over a certain tile. You'll need to declare it as its own variable as I ran into a few issues. Just do:

var battleTile = { // tile function here }

Look at the examples, I'm sure there's some code about it but that's what you need.

Link to comment
Share on other sites

Thanks Heppell08, I checked the Tile object in the documentation but I just don't get your drift, my layer is yet a physical body, so I don't need to iterate over Tiles, or?

 

How can I prevent that overlaps or collide just works on the bounding of a body (if the player is inside the body both functions will not fired) and how can I manage that bouncing is not triggered, so both bodies are able to phase in each other?

Link to comment
Share on other sites

my example of tile function from game is like this. 

var tileFunc4 = {    playerReset: function()    {        player.kill();    }}
map.setTileIndexCallback([168,2], tileFunc4.playerReset, this);

so above i have my tile function then below i decalre the function. So i set the tile index callback and the tile ID's (168, 2) So if the player interacts with them 2 tiles the playerReset function will fire. So if you know the ID's of the tiles you can declare the functions.

It will always fire on contact, so you dont need extra code to tell the game the player has touched tile 10 because it knows already. just set the index and create the function needed.

Hopefully that makes more sense to you :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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