Jump to content

Asking about collision


Recommended Posts

Hi Guys,

I'm working on my first game with Phaser.io and i have a problems with my collisions.

Quickly, i have enemies that spawning around me and shot on me, and with my player i need to return their bullet on their face.

But the collision between the bullet and the enemies don't work, (actually, my bullet and my enemy overlaped when the game start so i activate my collide function between them when the bullet hit my player, each time the bullet is returned but she don't destroy the enemy)

that part is in my update and it's my collide checking between shield and bullet.

if(this.game.physics.arcade.collide(this.shield, this.bulletPool, this.Reditum))
    		console.log("collide shield/bullet");
    		this.game.physics.arcade.collide(this.enemy, this.bullet, this.backSuccess);
    		//this.game.physics.arcade.collide(this.RedPool, this.bullet, this.backSuccess);

And here my function backSuccess, but my code never go inside this function.


backSuccess: function(enemy, bullet){
  		console.log('renvoi fctionnel');
  		this.enemy = enemy;
  		this.bullet = bullet;

    	this.score += 10;

Any one have an idea please ?


Thank you very much

Link to comment
Share on other sites

Hi, you are checking collision shield vs. bullet every frame. If this collision happen, you are, immediately in the same frame, testing collision between this bullet and enemy. In oher words, you are checking if in particular frame both collisions: shield vs. bullet and bullet vs. enemy happen.

 If I were in your place, I would create 2 Phaser.Groups - one for enemy bullets and second for repeled bullets. I would put bullets shoot by enemies into first group and in update I would check collision shield vs. whole enemy bullets group. If there is collision (bullet is repeled), then I would move it into group of repeled bullets. My second (independent, not based on result of first check) check in update would be for collisions between enemies and repeled bullets group.

Link to comment
Share on other sites

Ok because i created my group in create and i make in my update. this.oldgroup.remove(this.bullet) / this.newgroup.add(this.bullet) but that say 'a is undifined' in my phaser file .... (and in the lane this.newgroup.add(this.bullet) ) ... But i don't understand why

Link to comment
Share on other sites

You should probably copy us part of code regarding those groups and give exact error message with specific line. You wrote that error is 'a is undefined' but there is no 'a' in two commands you wrote. Or this 'a' error comes from phaser.js?

Link to comment
Share on other sites

Yes sorry 3ddy, here it's my code and my error is in phaser.min.js

setupBullets: function () {
		this.nbrbullet = 100;

		// Add an empty sprite group into our game
		this.bulletPool = this.add.group();
		// Enable physics to the whole sprite group
		this.bulletPool.enableBody = true;
		this.bulletPool.physicsBodyType = Phaser.Physics.ARCADE;
		this.bulletPool.createMultiple(this.nbrbullet, 'bullet');
		// Sets anchors of all sprites
		this.bulletPool.setAll('anchor.x', 0.5);
		this.bulletPool.setAll('anchor.y', 1);
		// Automatically kill the bullet sprites when they go out of bounds
		//this.bulletPool.setAll('outOfBoundsKill', true);
		//this.bulletPool.setAll('checkWorldBounds', true);
		this.nextShotAt = 500;


	setupBulletreturn: function () {
		this.nbrreturnbullet = 1;
		this.bulletreturnPool = this.add.group();
		this.bulletreturnPool.enableBody = true;
		this.bulletreturnPool.physicsBodyType = Phaser.Physics.ARCADE;
		//this.bulletPool.createMultiple(this.nbrreturnbullet, 'bullet');
		this.bulletreturnPool.setAll('anchor.x', 0.5);
		this.bulletreturnPool.setAll('anchor.y', 1);


My two groups called in 'Create', and a part of my update:

if(this.game.physics.arcade.collide(this.shield, this.bulletPool))
    		console.log("collide shield/bullet");
    		var lastbullet = this.bullet;

And my error is :


TypeError: a is undefined          phaser.min.js:6:31418

Game2.js: 64 is my lane "this.bulletreturnPool.addChild(lastbullet);"


I hope someone will find a solution. Or if someone know an other way to ensure that my bullet could destroy his own canon after impact on my shield, I'm interested !

Thank you for your help.



Link to comment
Share on other sites


 first, you can set your return group simply like this:

this.bulletreturnPool = this.add.group();

all physics properties are already set on level of every single bullet. You just need container (group) to store them. Think of groups in your case just like scene graph/tree nodes and what you need is just move bullets from group to group - organize them (but not setup anymore as you did all setup in create method).

Secondly, your code in update does not make sense. Calling collide retruns treu / false and just says: "yes, there is collision" or: "no, there is not collision" - good if you want to know if your player touched ANY from (for example) deadly lava sprites and you are not interested in details. Your call does not set tihs.bullet and it probably remains in its initial state - undefined - causing that error.  In your case you need to know which bullet collided. So, you have to add callbacks to method call - look at this example: http://phaser.io/examples/v2/arcade-physics/sprite-vs-group (line 43). There is added callback for collision which will be called when collision happens and it will be called with parameters, that say which two bodies collided. Inside callback you will know which bullet collided and you can move it into another group.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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