Jump to content

Phaser p2 collision issues


spitfire
 Share

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
	game.physics.startSystem(Phaser.Physics.P2JS);
	cursors = game.input.keyboard.createCursorKeys();
	game.physics.p2.setImpactEvents(true);
	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');
  	game.physics.p2.enable(player);
  	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
  	game.world.setBounds(0,0,640,640);

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

  	// Player Collisions
  	player.body.setCollisionGroup(playerCG);
  
  	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];
        layerBody.setCollisionGroup(collisionTable[layer]);
        layerBody.collides(playerCG);
    }
  player.body.collides(collisionTable[layer]);
};

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 

Thanks 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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