Jump to content

How i can do collide everything inside on a layer with my character


jopcode
 Share

Recommended Posts

Hi guys i have a tilemap with 4 layers(ground, water, roads, others) and a sprite with my character. how i do would for my character can collide with everything inside on others(layer),every layer use a different sprite, example ground use ground.png, water water.png etc, i have this code ( i use https://github.com/lean/phaser-es6-webpack):

/* globals __DEV__ */
import Phaser from 'phaser'
import Mushroom from '../sprites/Mushroom'
import Link from 'phaser-link'

export default class extends Phaser.State {
  super() {
  }
  init () {}
  preload () {
    this.load.image('terrain', '../../assets/sprites/tileset_basic_terrain.png', 16, 16)
    this.load.image('this.others', '../../assets/sprites/tileset_other.png', 16, 16)
    this.load.image('water', '../../assets/sprites/tileset_water.png', 16, 16)
    this.load.tilemap('tilemap', '../../assets/map/overworld_01.json',null,Phaser.Tilemap.TILED_JSON)
    this.load.spritesheet('character', '../../assets/sprites/character.png', 16, 32, 1)
  }

  create () {
    this.map = this.game.add.tilemap('tilemap')
    this.map.addTilesetImage('tileset_basic_terrain', 'terrain', 16, 16)
    this.map.addTilesetImage('tileset_other', 'this.others', 16, 16)
    this.map.addTilesetImage('tileset_water', 'water', 16, 16)

    this.ground = this.map.createLayer('Ground')
    this.ground.resizeWorld()

    this.water = this.map.createLayer('Water')
    this.water.resizeWorld()

    this.others = this.map.createLayer('Others')
    this.others.resizeWorld()
    this.game.physics.arcade.enable(this.others)
    console.log(this.map.layers[3])
    this.roads = this.map.createLayer('Roads')
    this.roads.resizeWorld()
    this.map.setCollisionBetween(0,1000, true, this.others)
    this.physics.startSystem( Phaser.Physics.ARCADE )
    this.link = this.add.existing(new Link({
        game: this.game,
        key: 'character',
        controls: true,
    }))
    this.game.camera.follow(this.link);
  }
  update() {
    this.game.physics.arcade.collide(this.link, this.others);
  }
  render () {
    if (__DEV__) {
      //this.debug.body(this.player,16, 32)
      
    }
  }
}

 

But sometime when i try test the collision between the player and layer others, this don't work correctly, collide in a blank block in somewhere place for example left and top place, but in bottom and right place the collide work correctly. for create my map i use tilemap editor 1.0.3

Images of example:

 

there i can't move more to right:

 cantmovetoright.PNG.fcef6f83ea6f62c7b739efa710e485f8.PNG

 

there i can't move more to bottom:

cantmovetobottom.PNG.5659d011c715f58490fed3dedfc63d24.PNG

 

there work correctly

workcorrectly.PNG.5d59a645e7766fb7df683a644eb16fa1.PNG

 

Sorry for my bad english. and thanks

overworld_01.json

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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