Jump to content

Tilemap collisions problem.


AramCP
 Share

Recommended Posts

EDIT: I solved it, thanks anyway

 

Hi, im here again posting another noobie problem that i have.

Well, as i said in my other post, im making (well... im trying to make) a pokemon game using phaser.

I have to add that i've never programmed before in any lenguage so this is a bit difficult for me, but i like it and its so fun so i wil continue learning and getting better.

Okay, my problem is that the collisions in my game dont work, ofc i know im doing something wrong but i dont know what, so i really need your help because i spend a lot of time trying to solve this.

This is my map, i exported it in json and implemented it into my code.

http://prntscr.com/d9by0m

And this is my code: 

var juego = new Phaser.Game(400,400,Phaser.AUTO, 'ejemplo', { preload: preload, create: create, update: update });

var map;
var layer;
var jugador;
var cursors;


function preload() { 
    juego.load.tilemap('pokemon', 'maperino.csv', null, Phaser.Tilemap.TILED_JSON);
    juego.load.image('tiles', 'tileset.png');
    juego.load.spritesheet('red', 'spritesh.png', 32, 64);

}

function create() {

    map = juego.add.tilemap('pokemon'); 
    map.addTilesetImage('tileset1', 'tiles'); 

    map.setCollisionBetween(23, 24);
    map.setCollisionBetween(33, 34);

    layer = map.createLayer('Capa1');
    layer = map.createLayer('Capa2');
    layer = map.createLayer('Capa3');
    layer.resizeWorld(); 

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

	jugador = juego.add.sprite(128, 148, 'red');
	juego.physics.arcade.enable(jugador);
    juego.camera.follow(jugador);
    jugador.frame = 1;

	jugador.animations.add('arriba', [3, 4, 5,], 8, true);
    jugador.animations.add('abajo', [0, 1, 2,], 8, true);
    jugador.animations.add('izquierda', [9, 10, 11,], 8, true);
    jugador.animations.add('derecha', [6, 7, 8,], 8, true);

	cursors = juego.input.keyboard.createCursorKeys();
}

function update() {

    if (juego.physics.arcade.collide(jugador, layer)){
        jugador.body.velocity.x = 0;
        jugador.body.velocity.y = 0;
    }

	jugador.body.velocity.x = 0;
    jugador.body.velocity.y = 0;

    if (cursors.left.isDown)
    {
        jugador.body.velocity.x = -80;
        jugador.animations.play('izquierda');
		return;
    }


    if (cursors.right.isDown)
    {
        jugador.body.velocity.x = 80;
        jugador.animations.play('derecha');
		return;
    }

    if (cursors.up.isDown)
    {
        jugador.body.velocity.y = -80;
        jugador.animations.play('arriba');
		return;
    }


    if (cursors.down.isDown)
    {
        jugador.body.velocity.y = 80;
        jugador.animations.play('abajo');
		return;
    }
    else
    {
        jugador.animations.stop();

        jugador.frame = 1;
    }

}

As you can see, i created the collisions, the numbers 23, 24 and 33, 34, are the ID's from the tree tile. And then i created a conditional in the update function, but when i run the game, nothing happens, the player just passes over the trees like a ghost. 

Guys if you help me in that you will make me so happy, because collisions are so important in a game, and this is the first time i do somthing like that. So.. yeah, thank you!

Link to comment
Share on other sites

Try doing this:
First remove the if statement on your update function, This:

if (juego.physics.arcade.collide(jugador, layer)){
        jugador.body.velocity.x = 0;
        jugador.body.velocity.y = 0;
    }

And change it to this:

juego.physics.arcade.collide(jugador, layer, function(){jugador.body.velocity.x = 0; jugador.body.velocity.y = 0;});

Don't put it in If statement

juego.physics.arcade.collide(obj1, obj2, function);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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