Jump to content

How to do collision with p2?


leonardovilarinho
 Share

Recommended Posts

Could anyone help with some example of performing collision with Phaser's P2 physique?I have three layers on my tilemap, one showing the floor, one with the colliding items and another with collectible items. How do I put the player to crash or perform an action when crashing into any tile on a layer?
Already tried:
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);

        

    }
};

 

 

But it did not work, could you give me an example? Those that are present in the Phaser website are not served, because I want a move of RPG.
 
 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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