Jump to content

Assign different properties to sprite in group


crouzilles
 Share

Recommended Posts

Hi,

 

I have a very basic breakout game with 20 bricks.

 

I add the bricks to the screen like this

bricks = game.add.group();bricks.enableBody = true;// Create the 20 bricksfor (var i = 0; i < 5; i++) {    for (var j = 0; j < 4; j++) {        var val = game.rnd.integerInRange(1, 4);        if (val % 3 == 0) {            game.add.sprite(40+i*160, 55+j*60, 'pink_brick', 0, bricks);        } else {            game.add.sprite(40+i*160, 55+j*60, 'blue_brick', 0, bricks);        }     }}// Make sure that the bricks won't movebricks.setAll('body.immovable', true);

I also have a function being triggered in the update when the ball hits a brick like this:

function update() {    .....    .....    // Make the bricks and the ball collide    game.physics.arcade.collide(ball, bricks, ball_hits_bricks);    .....    .....}function ball_hits_bricks(ball, brick) {    brick.kill();    update_score(10);    sound_brick_hit.play();}

Currently the player gets 10 points for hitting a brick, but I would like the player to get 20 points for hitting a pink brick and 10 for hitting a blue brick.

 

Am I able to assign different properties to sprites added to a group and then retrieve those properties when something happens (e.g: ball hits brick), what ever the properties maybe be or the number of properties for that matter? If so, how do I achieve this?

 

If not, what do you suggest?

 

Regards

Crouz

Link to comment
Share on other sites

Javascript is very flexible.

You should be able to do this when creating the bricks:

 

brick.scoreValue = 10;

 

And then in your function

function ball_hits_bricks(ball, brick) {brick.kill();update_score(brick.scoreValue);sound_brick_hit.play();}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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