Search code examples
mit-scratch

"Switch costume" not triggering on the same conditions as other things successfully trigger


I'm taking a refresher course on C, SQL, etc and an introductory assignment has us using Scratch, which I'm unfamiliar with. In short, I'm making a "game" where a bat semi-randomly bounces around the screen, bouncing off balls which can be moved by clicking them, and trying to get to an apple. When the bat and the apple make contact (i.e. for my purposes, when bat's distance from apple < 50) the bat does its victory "animation". In addition, I have the apple coded to switch costumes to a half-eaten version when its distance from the bat < 50. However, the apple doesn't do this even though the bat's procedures run correctly. Even more strangely, if I re-run the program once it's finished, the apple may start out in its half-eaten costume.

I have tried:

  • Adding more for the bat to do after (hence the spinning) in case the program is "ending" too early for the apple to do anything. Same results.
  • Letting the apple's costume change trigger when its distance < a higher number, so it has a chance to switch before the bat meets its criteria. Same results.
  • Seeing if the apple can switch to the half-eaten costume under simpler conditions, like right when the program runs, in case there's some problem with the costume itself. It works fine.

Code for the bat

Code for the apple

If I'm not mistaken there's no way to switch to an "advanced" mode on Scratch, where I could see the code under the hood; otherwise I'd feel more comfortable sussing out the problem from there per my previous experience. Please let me know if there is such a function I'm missing though, because I may well be able to figure out the problem that way. Thanks!


Solution

  • Your apple code only runs once when you click the green flag. You have to have it running in a loop, continuously checking its distance, in order to have it be updated (preferably pausing in-between each loop iteration so it's not checking too frequently). The reason the apple sometimes starts as half-eaten is because it happened to be < 50 units away from the bat at the moment the flag was clicked.