Jump to content

Adding group as a subgroup causes crash


Piggy
 Share

Recommended Posts

Hi,

I'm using a group for all bullets in the game, and would like to keep the bullets of enemies in subgroups of the main bulletgroup. For some reason, this code crashes, and I can't figure out why. 

 

The error I get: Uncaught TypeError: Cannot read property 'x' of undefined        phaser.js:34380
 

    this.bullets = this.game.add.group();    var enemyBullets = this.game.add.group();    enemyBullets.createMultiple(30, 'arrow');    enemyBullets.setAll('anchor.x', 0.5);    enemyBullets.setAll('anchor.y', 0.5);    enemyBullets.setAll('outOfBoundsKill', true);    this.bullets.add(enemyBullets);    this.game.physics.enable(enemyBullets, Phaser.Physics.ARCADE);
Link to comment
Share on other sites

Thanks for answering! That's odd... It works as long as I avoid using sub groups, and keep every bullet in the same group. Does a group with supgroups in it behave differently from one without?

 

EDIT
From what I can tell the crash happens in my collision code. Even adding an empty group to my bulletgroup is enough to make the game crash in the following row:

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

 

Do I have to check for collisions in every individual supgroup, or should this row be enough?

Link to comment
Share on other sites

Here's the code I just tried. If I collide against the parent group then obviously nothing happens, because it doesn't iterate through sub-groups. If I collide against the child group it works fine:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });function preload() {    game.load.image('atari', 'assets/sprites/atari130xe.png');    game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);}var bob;var atari;var balls;var cursors;function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    game.stage.backgroundColor = '#2d2d2d';    bob = game.add.group();    balls = game.add.group();    balls.createMultiple(50, 'bullets');    balls.setAll('anchor.x', 0.5);    balls.setAll('anchor.y', 0.5);    bob.add(balls);    game.physics.arcade.enable(balls, true);    atari = game.add.sprite(300, 450, 'atari');    game.physics.arcade.enable(atari, true);    game.physics.arcade.gravity.y = 400;    atari.body.allowGravity = 0;    atari.body.immovable = true;    cursors = game.input.keyboard.createCursorKeys();    game.time.events.loop(150, fire, this);    game.add.text(16, 16, 'Left / Right to move', { font: '18px Arial', fill: '#ffffff' });}function fire() {    var ball = balls.getFirstExists(false);    if (ball)    {        ball.frame = game.rnd.integerInRange(0,6);        ball.exists = true;        ball.reset(game.world.randomX, 0);        ball.body.bounce.y = 0.8;    }}function reflect(a, ball) {    if (ball.y > (atari.y + 5))    {        return true;    }    else    {        ball.body.velocity.x = atari.body.velocity.x;        ball.body.velocity.y *= -(ball.body.bounce.y);        return false;    }}function update() {    game.physics.arcade.collide(atari, balls, null, reflect, this);    // game.physics.arcade.collide(atari, bob, null, reflect, this);    atari.body.velocity.x = 0;    if (cursors.left.isDown)    {        atari.body.velocity.x = -200;    }    else if (cursors.right.isDown)    {        atari.body.velocity.x = 200;    }    balls.forEachAlive(checkBounds, this);}function checkBounds(ball) {    if (ball.y > 600)    {        ball.kill();    }}function render() {}

The above is tested against the 2.0.1 release in the dev branch.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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