CD U4L27 Code question Making their Own game

[Game Lab - Code.org

Student project linked above uses the mousepressedover tool in an if statement to score points for good or lose points for bad. The issue is the points are never correct.

How could this work?

This Fruit Ninja remake is pretty impressive so far! Since the student used mousePressedOver, the score goes up the entire time the mouse if pressed down over top of the given fruit. If you hang around over top of it, the score counts up very quickly. Same for down. That’s why the score is never correct. I’m not 100% sure how to keep it just to one point. I tried using mouseOver, but that had a similar result. Maybe others have some ideas?

1 Like

@blanden -

This is a tricky one because, as @edavis said, the points go up for as long as the mouse is pressed over the sprite even when animation changes. So, what you have to do is move the sprite off the screen (I tried to destroy the sprite but the points were still adding). Based on needing to move the sprite, here are the steps that I followed (and by the way - there are probably many other ways to solve this):

  • First, you need a sprite for the for the new fruit (meaning the fruit that appears when you click the original fruit). You can’t just change the fruit’s animation because if it is the same sprite, the points will continue to accumulate. So, you’ll need to make a sprite for all the new animations. In my example, I worked with just the cherry and the new fruit sprite is cherrySplat.
  • Then you’ll need to create two variables to capture the original fruits x and y position before it gets moved.
  • Inside the mousePressedOver(cherry) event, a series of things happen

score = score + 1; //score 1 point for touching the sprite
chx=cherry.x; //capture the current x postiion
chy=cherry.y; //capture the current y position
replaceFunc(chx, chy); //run a fucntion with these numbers to replace the fruit with other sprite in current x & y
cherry.x=1000; //move the old sprite so no longer scores points

Finally, you’ll need to create the function that replaces the original fruit and send the parameters that are the x and y position of the original fruit.
The cool part is that everything resets because the loopingfruits function replaces the new fruit back to the original. Here is my remix with only the cherry fruit complete.

Again - probably other ways to solve but this remix was my attempt:)
~Michelle

1 Like