wip/subgroup

 
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() {

}