Jump to content

Getting errors trying to do group vs group collisions


mikehamil10
 Share

Recommended Posts

Heyo,

 

Me again. I'm really trying to put Phaser through its paces. I'm using a state setup based on the State example in the wip folder (preload state, menu state, game state). My game state, among other things, contains 2 groups of Sprites: enemies and bullets. I'm also going by the "group vs group" collision example, so in my update() function I'm making the following call to check for collisions against enemies and bullets each frame:

 this.game.physics.collide(this.bullets, this.enemies, this.enemyCollisionHandler);

As soon as the game starts, I'm getting this error (this is in the un-minified 1.0.4 phaser.js):

 

Uncaught TypeError: Cannot read property 'exists' of undefined phaser.js:25523

 

Setting a breakpoint and stepping through the code, I see that in Group.collideGroupVsGroup() the offending line of code is checking an "exists" property of an object called "currenNode". In the debugger I can see that currentNode is set to my enemies group, but there's no property called "exists", and that check is whats causing the game to crash.

 

Any idea what I might be doing wrong? Is there something funky about using states AND group vs group collisions together that is broken perhaps?

Link to comment
Share on other sites

Hmm...I had tried that before posting, it yields the same result. My code now looks like this: 

this.game.physics.collide(this.bullets, this.enemies, this.enemyCollisionHandler, null, this);

and I still get the same "cannot read property 'exists' of undefined" error.

 

When inspecting the objects in collideGroupVsGroup(), I see that the "currentNode" object that's throwing the exception isn't a Phaser object at all, it's actually a PIXI.DisplayObjectContainer, which definitely doesn't have an exists property...

Link to comment
Share on other sites

Sure, here it is. This file is my 'game' state. I'm creating the groups with this.game.add.group(), passing a null parent and a name. Hopefully there's just something dumb I'm not doing correctly. Thanks for the help!

Shooter.Game = function (game) {	this.game = game;};Shooter.Game.prototype = {	//_______________________________________________________________________ Variable Declarations	shootTimer: 0,	enemyTimer: 0,	score: null,	scoreN: 0,		// Sounds	shot: null,	bossSound: null,	explo: null,	// Groups	lives: null,	bullets: null,	enemies: null,	// Display Objects	boss: null,	showBoss: false,	ship: null,	/*	var timerSource,	var bossHealth = 20,	*/		//_________________________________________________________________________ Lifecycle Functions	create: function () {		this.game.add.sprite(0, 0, 'bgGame');		// Player ship setup		var imageData = this.game.cache.getImage('playerShip');		this.ship = this.game.add.sprite(this.game.world.centerX, this.game.world.height - imageData.height, 'playerShip');		this.ship.anchor.setTo(0.5, 0.5);		this.ship.animations.add('idle');		this.ship.animations.play('idle', 30, true);		// Boss setup		this.boss = this.game.add.sprite(this.game.world.centerX, -72, 'boss');		this.boss.anchor.setTo(0.5, 0.5);		this.boss.body.immovable = true;		this.boss.animations.add('idle');		this.boss.animations.play('idle', 28, true);		// Score display setup	    var style = { font: "14px Arial", fill: "#ffffff", align: "center" };		this.score = this.game.add.text(1, this.game.world.height - 15, "Score:", style);		// Lives display setup		this.lives = this.game.add.group();		imageData = this.game.cache.getImage('icnLife');		for (var i = 1; i <= 3; i++) {	        var x = (this.game.world.width - imageData.width) - (5 * i + 1) - imageData.width * i + 20;	        var y = this.game.world.height - imageData.height;	        var life = this.lives.create(x, y, 'icnLife');    	}    	// Load audio    	this.shot = this.game.add.audio('sndShot', 1, true);    	this.bossSound = this.game.add.audio('sndBoss', 1, true);    	this.explo = this.game.add.audio('sndExplo', 1, true);    	// Group Setup    	this.bullets = this.game.add.group(null, "bullets");    	this.enemies = this.game.add.group(null, "enemies");	},	update: function() {		this.score.setText("Score: " + this.scoreN);		// handle input: left & right		this.ship.body.velocity.x = 0;		if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {			this.ship.body.velocity.x = -150;		} else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {            this.ship.body.velocity.x = 150;        }        // handle input: fire        if (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.game.time.now > this.shootTimer) {        	this.fireBullet();        }        // spawn enemies        if (this.game.time.now > this.enemyTimer) {        	this.addEnemy();		}		// update bullets		this.bullets.forEach(function(bullet) {           if (!bullet.inWorld) {                bullet.kill();            }        });        // update enemies		this.enemies.forEach(function(enemy) {           if (enemy.y > 480) {                enemy.kill();            }        });        // should we show the boss?        if(this.scoreN >= 50 && !this.showBoss) {	        this.bossSound.play('', 0, 1, false);	        this.game.add.tween(this.boss).to({ y: this.boss.height + (this.boss.height * 0.5) }, 1500, Phaser.Easing.Linear.Cubic, true);	        this.showBoss = true;	    }	    // handle collisions	    this.game.physics.collide(this.bullets, this.enemies, this.enemyCollisionHandler, null, this);	},	//__________________________________________________________________________ Collision Handlers	enemyCollisionHandler: function(bullet, enemy) {		this.scoreN += 50;		this.explo.play('', 0, 1, false);		bullet.kill();		enemy.kill();	},	//____________________________________________________________________________ Custom Functions	fireBullet: function() {		this.shot.play('', 0, 1, false);        var b = this.bullets.create(this.ship.x, this.ship.y - this.ship.height, 'bullet');        b.autoCull = true;        b.body.velocity.y = -100;        this.shootTimer = this.game.time.now + 800;	},	addEnemy: function() {		var e = this.enemies.create(this.game.world.randomX, -20, 'enemy');		e.animations.add('idle');		e.animations.play('idle', 24, true);	    e.body.immovable = true;	    e.body.velocity.y = 30;	    this.enemyTimer = this.game.time.now + 800;	},	alert: function(e) {	    var alertView = null;	    	    if (e == 'win') {	    	alertView = this.game.add.button(this.game.world.width * 0.5, this.game.world.height * 0.5, 'youWon', this.restart, this, 2, 1, 0).anchor.setTo(0.5, 0.5);	    } else {	        alertView = this.game.add.button(this.game.world.width * 0.5, this.game.world.height * 0.5, 'gameOver', this.restart, this, 2, 1, 0).anchor.setTo(0.5, 0.5);	    }	},	restart: function () {		console.log('restarting!');		this.game.state.start('game');	},}
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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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