U3L16 - Collision score - limiting to +1

When sprites collide, sometimes the score increases by 1 sometimes by multiple as one slides past the other’s axis. Is there a way to prevent this from happening and limit the score to 1?

Nancy,

This is a question that my students have all the time - but the code is correct… so I have them talk through it to see if they can come up with a solution. The score increases as long as the two sprites are touching - so if they have to touch for long than the split second - the score is going to increase like programmed. There are a few solutions to having the score increase only but one - most of my students put a “wait _ seconds” after the point value is increased so the objects can move away from each other - but others have come up with equally cleaver ideas. Sometimes people say that programming is 25% coding and 75% debugging - and I feel that this is one of those cases!

Let me know if you have any other questions,
Brad

I am learning this as I teach it, and I’m not getting the score “wait_seconds” code. Could you please share the code and the other’s cleaver ideas? Thanks!

A few ways that I’ve seen students deal with this issue:

  • Move the sprite to a new location as soon as they touch
  • Use a boolean variable to track if a sprite has been touched
  • Use a coutdown/countup variable that gets reset when sprites touch eg:
var countdown = 0;
var score = 0;
draw() {
    // If the countdown hasn't finished, keep counting down
    if (countdown > 0) {
        countdown = countdown - 1;
    }

    if (sprite1.isTouching(sprite2)) {
        // Check if the countdown timer is active before scoring
        if (countdown == 0) {
            score = score + 1;

            // Start the countdown timer to prevent another score
            countdown = 10;
        }
    }
}

Thank you! This helps me!

Thank you! Huge help.