Jump to content

Phaser p2 collision issues


Recommended Posts

Hi ! 

the goal of this code is to collide the player with a certain layer of the map.

My map is in a JSON file.

Im having an issue with collisions in my code, here is the code :

this my function create :

// Physics and Keyboard Input
	cursors = game.input.keyboard.createCursorKeys();
	game.physics.p2.restitution = 0.8;

	// Create the map and layers
  	map = game.add.tilemap('map');
  	map.addTilesetImage('tile set bar 1');
  	map.addTilesetImage('tile set bar bordures');
  	map.addTilesetImage('tile set casino');
  	map.addTilesetImage('tile set casino bordures');
  	var Background = map.createLayer('Background');
  	var bord_tileset = map.createLayer('bord tileset');
  	var walls = map.createLayer('walls');
  	var tableau_mur = map.createLayer('tableau_mur');
  	var canape1 = map.createLayer('canape1');
  	var meubles = map.createLayer('meubles');
  	var canape2 = map.createLayer('canape2');
  	var objets_meubles = map.createLayer('objets_meubles');

  	// Player 
  	player = game.add.sprite(320, 288, 'nakedguy');
  	player.body.fixedRotation = true;
  	player.animations.add('walk_down', [0,1,2,3], 8, true);
 	player.animations.add('walk_left', [4,5,6,7], 8, true);
  	player.animations.add('walk_right', [8,9,10,11], 8, true);
  	player.animations.add('walk_up', [12,13,14,15], 8, true);
  	player.frame = 0;

  	// Set World Bounds

  	// Collision Groups
  	playerCG =   game.physics.p2.createCollisionGroup();

  	// Player Collisions
  	collisionBetween(player, 'walls');

 And here is the code for the collisionBetween function :

// This function causes collisions between the player sprite and a certain layer.
collisionBetween = function(player, layer) {
  collisionTable[layer] = game.physics.p2.createCollisionGroup();
  var layerObjects = game.physics.p2.convertTilemap(map, layer);
    for (i = 0; i < layerObjects.length; i++) {
        var layerBody = layerObjects[i];

As a result nothing collides execpt the player and the world bounds and i dont understand why....

because here the player should collide with every tile of the "walls" layer.

Any help would be appreciated 



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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