Jump to content

[P2] Collision between player, bounds and layer


leonardovilarinho
 Share

Recommended Posts

I'm trying to make a prototype for a RPG game, I managed to make the drive, but I can't do the collision, can help?

I have the ' background ' layer, that would be my floor, the 'colisao', where I keep all objects that the player will collide, as walls, and finally the 'coletaveis', where the items that the player can interact.

Follow my code level 1:

 

Quote

 

Player = function(game, x, y) {
    this.controls = {
        right: game.input.keyboard.addKey(Phaser.Keyboard.RIGHT),
        left : game.input.keyboard.addKey(Phaser.Keyboard.LEFT),
        up   : game.input.keyboard.addKey(Phaser.Keyboard.UP),
        down : game.input.keyboard.addKey(Phaser.Keyboard.DOWN),
        run  : game.input.keyboard.addKey(Phaser.Keyboard.SPACE),
    };

    this.speed = 150;
    this.level = 0;
    this.xp = 0;

    this.object = game.add.sprite(x, y, 'player');
    this.object.smoothed = false;
    this.object.anchor.setTo(0.5,0.5);
    this.object.animations.add('idle', [3], 1, true);
    this.object.animations.add('up', [0, 1, 2], 3, true);
    this.object.animations.add('down', [3, 4, 5], 3, true);
    this.object.animations.add('right', [6, 7, 8], 3, true);
    this.object.animations.add('left', [9, 10, 11], 3, true);   

    game.physics.p2.enable(this.object); 
    this.object.body.collideWorldBounds = true;
    game.camera.follow(this.object); 
}

Game.Level1 = function(game){

};

var map;
var tileset;
var player;
var gameXPsteps = 15;

Game.Level1.prototype = {
    create : function(game) {
        game.physics.startSystem(Phaser.Physics.P2JS);
        game.physics.p2.restitution = 0.8;

        map = this.add.tilemap('map', 32, 32);
        map.addTilesetImage('cenario01');

        layer1 = map.createLayer('background');
        layer1.resizeWorld();
        
        layer2 = map.createLayer('colisao');
        layer2.resizeWorld();

        layer3 = map.createLayer('coletaveis');
        layer3.resizeWorld();

        map.setCollisionByExclusion([], false, layer1);
        map.setCollisionByExclusion([], true, layer2);
        map.setCollisionByExclusion([], true, layer3);

        map.setTileIndexCallback(4, this.powerUp, this);
        // map.setTileIndexCallback(6, this.killed, this);


        player = new Player(game, 128,64);

        

    }
};

 

 

Not even the collision with the edge of the screen is working, can anyone help me?

 

game.zip

Link to comment
Share on other sites

  • 2 weeks later...

You specified the tiles for collision with "setCollisionByExclusion". After that, you need to convert these tiles into P2 bodies like that:

game.physics.p2.convertTilemap(map, layer2);
game.physics.p2.convertTilemap(map, layer3);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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