crouzilles Posted April 29, 2015 Share Posted April 29, 2015 Hi, I have a very basic breakout game with 20 bricks. I add the bricks to the screen like thisbricks = 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? RegardsCrouz Link to comment Share on other sites More sharing options...
ZoomBox Posted April 29, 2015 Share Posted April 29, 2015 Javascript is very flexible.You should be able to do this when creating the bricks: brick.scoreValue = 10; And then in your functionfunction ball_hits_bricks(ball, brick) {brick.kill();update_score(brick.scoreValue);sound_brick_hit.play();} Link to comment Share on other sites More sharing options...
crouzilles Posted April 29, 2015 Author Share Posted April 29, 2015 Perfect solution, thank youCrouz Link to comment Share on other sites More sharing options...
drhayes Posted April 29, 2015 Share Posted April 29, 2015 You might want to consider creating a subclass of Sprite which means you can customize the constructor function and use "game.add.existing" instead of "game.add.sprite". Then you could say "game.add.existing(new Brick(game, x, y, brickColor));" and change the point value based on the brickColor. Link to comment Share on other sites More sharing options...
Recommended Posts