Piggy Posted March 21, 2014 Share Posted March 21, 2014 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 More sharing options...
rich Posted March 21, 2014 Share Posted March 21, 2014 It must be something else triggering the error I'm afraid, because I pasted your code above into a clean test and it worked fine. Link to comment Share on other sites More sharing options...
Piggy Posted March 21, 2014 Author Share Posted March 21, 2014 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? EDITFrom 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 More sharing options...
rich Posted March 21, 2014 Share Posted March 21, 2014 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. Piggy 1 Link to comment Share on other sites More sharing options...
Piggy Posted March 21, 2014 Author Share Posted March 21, 2014 Woah, thanks for the example, really appreciated! Then my problem is simply that I tried to collide with the parent group only. I'll simply loop through the children and collide them like that instead. Thanks again for the help! Link to comment Share on other sites More sharing options...
Recommended Posts