Jump to content

Overlap Group Vs Group Cannot read property 'exists' of undefined


Archer3CL
 Share

Recommended Posts

I'm spawning multiple enemies using the group class in random places but when i try to check for overlap with my bullets the console throws "Cannot read property 'exists' of undefined"

 

The code initializing enemies and bullets

function create(){    enemies = game.add.group();    enemies.createMultiple(20, 'star', 0, false);    enemies.enableBody = true;    enemies.physicsBodyType = Phaser.Physics.ARCADE;    bullets = game.add.group();    bullets.createMultiple(20, 'bullet', 0, false);    bullets.enableBody = true;    bullets.physicsBodyType = Phaser.Physics.ARCADE;    bullets.setAll('outOfBoundsKill', true);    bullets.setAll('checkWorldBounds', true);}

The update function to check overlap

function update(){     game.physics.arcade.overlap(enemies, bullets, destroy, null, this);}

The random spawn function

function randomSpawn() {       if (game.time.now > nextEnemy) {        nextEnemy = game.time.now + spawnRate;        var enemy = enemies.getRandom();        var random = getRandomInt(0, 2);        if (enemy) {            enemy.exists = true;            switch (random) {                case 0:                    enemy.reset(leftSpawnPoint.x, leftSpawnPoint.y);                    break;                case 1:                    enemy.reset(middleSpawnPoint.x, middleSpawnPoint.y);                    break;                case 2:                    enemy.reset(rightSpawnPoint.x, rightSpawnPoint.y);                    break;            }            enemy.body.acceleration.y += 25;        }    }}

Finally, the fire fuction

function fire() {    if ((game.time.now > nextFire) && selectedItem !== null) {        nextFire = game.time.now + fireRate;        var bullet = bullets.getFirstExists(false);        if (bullet) {            bullet.exists = true;            bullet.key = selectedItem.key;            bullet.tint = selectedItem.tint;            bullet.reset(selectedItem.x, selectedItem.y);            bullet.body.velocity.y -= 250;        }    }}

As far as i know, the problem lies on the random spawning of the enemies, because they don't exist at the same time, so only the last enemy spawn has this property and those born before don't.

 

Thanks  for reading

 

Link to comment
Share on other sites

The destroy function

function destroy(enemy, bullet) {        bullet.destroy();    enemy.destroy();}

If bullet is detroyed there is no problem, but if enemy.destroy is called then the console throws "Cannot read property 'exists' of undefined"

Link to comment
Share on other sites

Ok I think I know what the issue is (sorta anyway); you appear to be using object pools for your enemies and bullets, but then you're destroying the objects instead of killing them. This will deplete the pool and defeats the point of using the pool in the first place. Instead you should be killing enemies, and spawning them using a similar method as you use to spawn bullets, though using getFirstDead and revive as in the 'shootBullet' method of this example: http://gamemechanicexplorer.com/#bullets-2

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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