// Inside onResetEvent() after adding player for (let i = 0; i < 10; i++) { const coin = new Collectible( Math.random() * (me.game.viewport.width - 32), Math.random() * 200 // Only top part of screen ); me.game.world.addChild(coin); } melonJS uses bounding boxes automatically. To detect when the player touches a collectible, add this to your PlayerEntity.update() method (before return true ):
Now run the game. Move your orange square into the gold squares – they disappear and you get a console log. Create src/js/ui/ScoreLabel.js : melonjs tutorial
this.body.update(dt); this.pos.x += this.body.vel.x; this.pos.y += this.body.vel.y; // Inside onResetEvent() after adding player for (let
update(dt) { if (me.input.isKeyPressed("left")) { this.body.force.x = -4; } else if (me.input.isKeyPressed("right")) { this.body.force.x = 4; } Create src/js/ui/ScoreLabel
Don’t forget to preload the image in your src/js/resources.js :
draw(renderer) { this.font.draw(renderer, `Score: ${this.score}`, this.pos.x, this.pos.y); } }