Lesson 21: Mini-Project - Side Scroller - score/health

How do you get the score/health to go up or down by only one?

Could you send the project in question?

hello @langilla1,

i assume your code is probably decreasing health too quickly due to it being in a collision loop an effective way of preventing this is giving the player iframes (invincibility frames) that prevent them from taking more damage than they should

var spr = createSprite(200, 200);
var object = createSprite(200, 200);
var health = 3;
var iframes = 0;
function draw() {
    if (spr.collides(object) && iframes < 1) {
        health -= 1;
        iframes = 2 * Game.pInst._targetFrameRate; // produces how long player is invincible for
    }
    // this will count down the amount of invincibility you have left
    iframes -= 1;
}

hopefully this demo will help you; like @pluto has said we don’t really have a project to debug but this probably would be the most likely cause of your distress feel free to update the thread if this does not solve your issue

Varrience

Hi @langilla1,

This link does not work.

I do think this forum post also does a good job of explaining the basics of creating a score in your game without being too specific about your particular game. Check out this link and @varrience’s link above or send us a working link (allow sharing) to the project you have a question about.

Good luck,
~Michelle

if your goal is whole numbers being displayed while the health is decreasing i’ve modified the code to use math ceil here i was able to get myself a copy of the code and debug it let us know if this solves your problem

Varrience