Jump to content

Switch tilemap if X is 0


hondacalibra
 Share

Recommended Posts

Hi!

I am making an pokemon style mmo, and I've been struggling with this feature.

I have a tilemap which is called map1.json

When you move to a specific coordinate, I want my player to go over to another tilemap called map2.json

 

Is this possible?

 

My code (most is taken from a phaser example)

 

var game = new Phaser.Game(1200, 800, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.load.tilemap('desert', '/maps/map1.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', '/img/tmw_desert_spacing.png');
    game.load.image('car', '/img/car90.png');

}

var map;
var map2;

var layer;

var cursors;
var sprite;

function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);

    map = game.add.tilemap('desert');

    map.addTilesetImage('Desert', 'tiles');

    layer = map.createLayer('Ground');

    layer.resizeWorld();
	

    sprite = game.add.sprite(450, 80, 'car');
    sprite.anchor.setTo(0.5, 0.5);

    game.physics.enable(sprite);

    cursors = game.input.keyboard.createCursorKeys();

}

function update() {

	//movement

    sprite.body.velocity.x = 0;
    sprite.body.velocity.y = 0;
    sprite.body.angularVelocity = 0;

    if (cursors.left.isDown)
    {
        sprite.body.velocity.x = -200;
    }
    else if (cursors.right.isDown)
    {
        sprite.body.velocity.x = 200;
    }

    if (cursors.up.isDown)
    {
        sprite.body.velocity.y = -200;
    }
	
	if (cursors.down.isDown)
    {
        sprite.body.velocity.y = 200;
    }
	
	//collision detection
	
	if (sprite.body.x <= 0){
		sprite.body.x = 0;
	}
	
	if (sprite.body.y <= 0){
		sprite.body.y = 0;
	}
	
	if (sprite.body.x >= game.width - 30) {
		sprite.body.x = game.width - 30;
	}
	
	if (sprite.body.y >= game.height - 30) {
		sprite.body.y = game.height - 30;
	}

}

function render() {
    game.debug.text('Tile X: ' + layer.getTileX(sprite.x), 32, 48, 'rgb(0,0,0)');
    game.debug.text('Tile Y: ' + layer.getTileY(sprite.y), 32, 64, 'rgb(0,0,0)');
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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