Jump to content

Uncaught TypeError: Cannot read property 'sprite' of undefined (phaser.js:35543)


Martiny
 Share

Recommended Posts

Hi everyone,

 

I've been working on my game but I couldn't get past this error.

 

What I'm trying to do is that everytime I kill and enemy with the wrong bullet, it spawns more enemy in the screen as a penalty. The problem is that when I kill and enemy, there's a change that  "Uncaught TypeError: Cannot read property 'sprite' of undefined" would appear and the game stop.

 

Here's the code so far:

function killEnemy(bullet, enemy) {	if (bullet != player) {		bullet.kill();	}	enemy.kill();}function penaltyRed(bullet, enemy){	if (bullet != player) {		bullet.kill();	}	enemy.kill();	spawnEnemies("RED");	// console.log("penalty red");}function penaltyGreen(bullet, enemy){	if (bullet != player) {		bullet.kill();	}	enemy.kill();	spawnEnemies("GREEN");	// console.log("penalty green");}function penaltyBlue(bullet, enemy){	if (bullet != player) {''		bullet.kill();	}	enemy.kill();	spawnEnemies("BLUE");	// console.log("penalty blue");}function spawnEnemies (color) {	if (color == "BLUE") {		for (i = 0; i < 3; i++) {			do {				ghostEnemy = game.add.sprite(game.rnd.integerInRange(0, game.world.width-56), game.rnd.integerInRange(0, game.world.height-56), 'ghostEnemy');			}			while (game.physics.overlap(ghostEnemy, player) || game.physics.overlap(ghostEnemy, blueEnemy) || game.physics.overlap(ghostEnemy, redEnemy) || game.physics.overlap(ghostEnemy, greenEnemy));			blueEnemy.create(ghostEnemy.x, ghostEnemy.y, 'blueEnemy');			ghostEnemy.kill();		}	}	else if (color == "RED") {		for (i = 0; i < 3; i++) {			do {				ghostEnemy = game.add.sprite(game.rnd.integerInRange(0, game.world.width-56), game.rnd.integerInRange(0, game.world.height-56), 'ghostEnemy');			}			while (game.physics.overlap(ghostEnemy, player) || game.physics.overlap(ghostEnemy, blueEnemy) || game.physics.overlap(ghostEnemy, redEnemy) || game.physics.overlap(ghostEnemy, greenEnemy));			redEnemy.create(ghostEnemy.x, ghostEnemy.y, 'redEnemy');			ghostEnemy.kill();		}	}	else if (color == "GREEN") {		for (i = 0; i < 3; i++) {			do {				ghostEnemy = game.add.sprite(game.rnd.integerInRange(0, game.world.width-56), game.rnd.integerInRange(0, game.world.height-56), 'ghostEnemy');			}			while (game.physics.overlap(ghostEnemy, player) || game.physics.overlap(ghostEnemy, blueEnemy) || game.physics.overlap(ghostEnemy, redEnemy) || game.physics.overlap(ghostEnemy, greenEnemy));			greenEnemy.create(ghostEnemy.x, ghostEnemy.y, 'greenEnemy');			ghostEnemy.kill();		}	}		}

What happens in the spawnEnemies fuction is this: I create a ghostEnemy that has the same size of the normal enemies, but its sprite is an transparent image. Then I check if it collides with the player, the blueEnemy (group), the redEnemy (group) and the greenEnemy(group). If it does, it's not a suitable place and the while loop gets another position for me. If it doesn't collide, then it's an OK position and I create an actual enemy with ghostEnemy's X and Y position. And then I kill ghostEnemy.

 

I use this code so every enemy spawns in a random location, but not on top of any other enemy/player. And it works fine when I call it. The only problems is when I call this function when an enemy is killed (the penaltyBlue/red/green functions).

 

It's kind of a long problem so I understand if you don't have time to read all this. If anyone wants to download the game and see the problem in action, here it is: https://www.dropbox.com/sh/vkucvpouo1hqwn2/7TE4dd9tD7

Link to comment
Share on other sites

Havent seen where the error is yet, but this is not the best plan:

do {    ghostEnemy = game.add.sprite(game.rnd.integerInRange(0, game.world.width-56), game.rnd.integerInRange(0, game.world.height-56), 'ghostEnemy');}

Instead of creating a brand new sprite every time you try place an enemy, create one and move it around until you find where to put it.

Also, you would be better off creating an enemy of the colour you want, and positioning it, rather than making a ghost one, then making another one.

Looking at it more, there are a few more optimisations you could make to reduce the amount of code you have.

 

 

EDIT:

After downloading and running the code, the error is being thrown in the collisions between your enemies and bullets.

In your code, that comes from here, depending on which enemy you are shooting at the time:

    game.physics.collide(blueBullet, blueEnemy, killEnemy, null, this);    game.physics.collide(redBullet, blueEnemy, penaltyBlue, null, this);     game.physics.collide(greenBullet, blueEnemy, penaltyBlue, null, this);      game.physics.collide(blueBullet, redEnemy, penaltyRed, null, this);     game.physics.collide(redBullet, redEnemy, killEnemy, null, this);     game.physics.collide(greenBullet, redEnemy, penaltyRed, null, this);      game.physics.collide(blueBullet, greenEnemy, penaltyGreen, null, this);     game.physics.collide(redBullet, greenEnemy, penaltyGreen, null, this);     game.physics.collide(greenBullet, greenEnemy, killEnemy, null, this);      game.physics.collide(player, blueEnemy, penaltyBlue, null, this);     game.physics.collide(player, redEnemy, penaltyRed, null, this);     game.physics.collide(player, greenEnemy, penaltyGreen, null, this); 

When the error occurs, the collision function is trying to look at an index in the potential collisions array that doesn't exist.

In the Phaser code, this is the line throwing errors:

//phaser.js Line 35543if (this._potentials[i].sprite.group == group)

In the case of my test, the i variable is 5, while the length of the _potentials array is 4.

Logging the values of i and the length of the potentials array to the console showed me that something is changing the potentials array, because it started at 6, and lots 2 members when i was 3

 

So it looks like you are somehow removing elemets from the potentials array somewhere and should not be.

Somewhere in the spawnEnemies function, probably.

Link to comment
Share on other sites

Havent seen where the error is yet, but this is not the best plan:

do {    ghostEnemy = game.add.sprite(game.rnd.integerInRange(0, game.world.width-56), game.rnd.integerInRange(0, game.world.height-56), 'ghostEnemy');}

Instead of creating a brand new sprite every time you try place an enemy, create one and move it around until you find where to put it.

Also, you would be better off creating an enemy of the colour you want, and positioning it, rather than making a ghost one, then making another one.

Looking at it more, there are a few more optimisations you could make to reduce the amount of code you have.

 

Thanks very much for taking the time to look at my code.

 

You are right, that was not very clever. I restructured so I only create ghostEnemy once for now, but I'm aiming to do what you said about positioning without making a ghost.

 

I've been trying loads of stuff so far, but I'm not getting lucky. I'm afraid it's just a simple mistake, since I don't have that much experience with it. I guess I'm not killing the sprites correctly, since it appears that this error occurs when the new spawned enemy collides with the enemy I just killed.

 

I'll keep changing the code to make it more optimized. Maybe that would fix it, since it's likely to be a code smell of my part and not a Phaser thing.

 

EDIT:

 

Yeah, spawnEnemies seems the cause of all this. I decided to delete it and start again, one step at a time. Thanks again for your help.

 

EDIT:

 

yep, sounds like it was there after all. I made a new one that creates enemies randomly in the screen (doesn't check if is on top of anything), and it works fine. I can collide all day without problems. I just need to think about a better way to create enemies that don't spawn on top of each other.

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