Jump to content

Arcade tilemap collision with offset world bounds


Thigydaze
 Share

Recommended Posts

Hi,

 

Trying to implement tilemap collision with the world bounds set to anything other than 0,0.

 

If I set the collision up using a simple tileMap, then everything is perfect. However, if I offset the world bounds (say to move it to the middle), then the tilemaps and sprite respect this new offset and are drawn ok. The layer.debug highlight also indicates all ok, but when moving around, the collisions happen as if the world bounds origin is 0,0. 

 

Here is a stripped down demo of the code in use...

 

var collisionTest = {};
 
collisionTest.maingame = function () {
 
    this.game; // a reference to the currently running game
    this.add; // used to add sprites, text, groups, etc
    this.camera; // a reference to the game camera
    this.cache; // the game cache
    this.input; // the global input manager (you can access this.input.keyboard, this.input.mouse, as well from it)
    this.load; // for preloading assets
    this.math; // lots of useful common math operations
    this.sound; // the sound manager - add a sound, play one, set-up markers, etc
    this.stage; // the game stage
    this.time; // the clock
    this.tweens;    //  the tween manager
    this.state;    // the state manager
    this.world; // the game world
    this.particles; // the particle manager
    this.physics; // the physics manager
    this.rnd; // the repeatable random number generator
 
};
 
collisionTest.maingame.prototype = {
 
    preload: function () {
        this.load.image("character", "images/character.png");
 
        this.load.tilemap("map", "levels/collisionTest.json", null, Phaser.Tilemap.TILED_JSON);
        this.load.image("tiles", "images/tiles.png");
    },
 
    create: function () {
        this.map = this.game.add.tilemap("map");
        this.map.addTilesetImage("tiles");
 
        this.layer = this.map.createLayer("TileLayer1");
        this.layer.debug = true;
 
        this.map.setCollision(2);//
       
        // Uncomment this to see the effect
        //this.world.setBounds(-300, -300, 600, 600);
 
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.game.physics.arcade.setBoundsToWorld();
 
        // background color light blue
        this.game.stage.setBackgroundColor(0xa0a0ff);
 
        // add character
        this.character = this.add.sprite(50, 50, "character");
        this.game.physics.enable(this.character);
        this.character.anchor.set(0.5);
        this.character.body.collideWorldBounds = true;
 
        this.cursors = this.game.input.keyboard.createCursorKeys();
        this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]);
    },
 
    update: function () {
 
        this.game.physics.arcade.collide(this.character, this.layer);
 
        if (this.cursors.left.isDown) {
            this.character.body.velocity.x = -100;          // move left
        } else if (this.cursors.right.isDown) {
            this.character.body.velocity.x = 100;          //  move right
        } else {
            this.character.body.velocity.x = 0;                                 //no speed left or right
        }
 
        if (this.cursors.up.isDown) {
            this.character.body.velocity.y = -100;          // move left
        } else if (this.cursors.down.isDown) {
            this.character.body.velocity.y = 100;          //  move right
        } else {
            this.character.body.velocity.y = 0;                                 //no speed left or right
        }
    },
 
    render: function () {
        this.game.debug.bodyInfo(this.character, 24, 24);
        this.game.debug.spriteInfo(this.character, 24, 200);
    }
};
 
I figure there is one thing I'm missing, but I can't figure it out. 
 
Any ideas?
 
Thanks
 
 
Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

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