Jump to content

Breakout game, need help with groups and bricks!

Recommended Posts

Hello guys,

I just recently started to play with Phaser and so far I'm loving it.. However as I'm not used to it, I run into wall.. I'm creating a breakout game where I want different bricks to respond to ball hitting them differently.. Mostly I want to add bricks a health so to speak, so depending on the color of the brick, it will take 1,2,3.. etc ball hits to kill it..


I'm so far doing this in this fashion:

// Red bricks    bricks_1 = game.add.group();    bricks_1.enableBody = true;    bricks_1.physicsBodyType = Phaser.Physics.ARCADE;    // Yellow bricks    bricks_2 = game.add.group();    bricks_2.enableBody = true;    bricks_2.physicsBodyType = Phaser.Physics.ARCADE;    // Gray bricks    bricks_3 = game.add.group();    bricks_3.enableBody = true;    bricks_3.physicsBodyType = Phaser.Physics.ARCADE;    // Green bricks    bricks_4 = game.add.group();    bricks_4.enableBody = true;    bricks_4.physicsBodyType = Phaser.Physics.ARCADE;

So, I'm creating a group for each of the type of the possible bricks.. Then as I randomly create a level, I do this:

function randomLevel(){    var brick;    for (var i = 0; i < 8; i++)    {        for (var j = 0; j < 6; j++)        {            var rand = game.rnd.integerInRange(1, 4);            switch(rand)            {                case 1:                    brick = bricks_1.create(75 + (i*80), 55 + (j*40), 'brick_1');                    brick.body.bounce.set(1);                    brick.body.immovable = true;                    break;                case 2:                    brick = bricks_2.create(75 + (i*80), 55 + (j*40), 'brick_2');                    brick.body.bounce.set(1);                    brick.body.immovable = true;                    break;                case 3:                    brick = bricks_3.create(75 + (i*80), 55 + (j*40), 'brick_3');                    brick.body.bounce.set(1);                    brick.body.immovable = true;                    break;                case 4:                    brick = bricks_4.create(75 + (i*80), 55 + (j*40), 'brick_4');                    brick.body.bounce.set(1);                    brick.body.immovable = true;                    break;            }                    }    }}

And then I have 4 onHit functions (which I don't really like, would prefer a single one with some switch statement in there) where depending on what type of brick has been hit, it can play ie different sound for example..

        game.physics.arcade.collide(ball, bricks_1, ballHitBrick_1, null, this);        game.physics.arcade.collide(ball, bricks_2, ballHitBrick_2, null, this);        game.physics.arcade.collide(ball, bricks_3, ballHitBrick_3, null, this);        game.physics.arcade.collide(ball, bricks_4, ballHitBrick_4, null, this);

This is example of one of the onHit functions (I basically used example of breakout a lot):

function ballHitBrick_1(_ball, _brick) {    _brick.kill();    hit_fx.play();    score += 10;    scoreText.text = 'Score: ' + score;    //  Are they any bricks left?    if (bricks_1.countLiving() == 0       && bricks_2.countLiving() == 0       && bricks_4.countLiving() == 0)    {        //  New level starts        scoreText.text = 'Score: ' + score;        introText.text = '- Next Level -';        //  Let's move the ball back to the paddle        ballOnPaddle = true;        ball.body.velocity.set(0);        ball.x = paddle.x + 16;        ball.y = paddle.y - 16;        ball.animations.stop();        //  And bring the bricks back from the dead         randomLevel();    }}

And this all works well, ie I can play different sounds based on what brick has been hit.. However what do I need to do to add health to these bricks? So for example, bricks_1 bricks die only after 1 shot, while bricks_3 don't die ever (think of like concrete/metal/blocking bricks)..


Thanks a ton!

Link to post
Share on other sites

Alright, first, u need to create a brick object. 


Inside of it, u create its sprite and add its health.


Update Function:


I belive that you are every frame checking if the ball collide with the brick. If im right, you can pass to you function both objects (ball and brick). Inside of your function u can check its remmaning life like:

if(brick.health > 0){    brick.health--;}else{    brick.kill();}

Attention: one ball sprite never collide with an object, so u need to check if ball sprite's collides with brick's sprite

Link to post
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.

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.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...