Why "is Hitting Tile" not work?

I set the projectile to hit the left wall, then set life +1. But it doesn’t work, why?

thanks!!

2 Likes

I added 2 blocks, the “Set tilemap” and the “Stay in screen.” Good luck on your project!

2 Likes

Oh, the background and the tilemap are different…
Thank you very much! :handshake:

1 Like

And you used block “if” in block “forever”, but mine was block “while”. “while” is not work…

1 Like

oh… it dosent really matter. i just prefer the “If” block.

this seems like a very interesting game… I wonder what it will become :thinking:

1 Like

“while” won’t work in this case because it will lock up the game! There isn’t a great way to explain this without getting into the technical details, but basically you should never use a while loop unless:

  1. There is a block that pauses inside it (e.g. “pause”, “play sound until done”, etc.)
  2. The expression in the condition is set to false somewhere inside the loop
  3. You use the “break” or “return” blocks in the loop to break out of it

Otherwise you will end up freezing your game. I think an “if” block will have the behavior you want anyways!

Technical explanation (feel free to ignore):

MakeCode has basic support for multi-threading (we call our threads “fibers”). Our multithreading uses a strategy call non-preemptive scheduling, which means each fiber will run until it gives up control back to the scheduler. That means if a fiber never gives up control, it will prevent all of the other fibers from running. The forever loop runs in one fiber and the game engine runs in another, so if the forever loop never gives up control the game engine will stop running and the screen will never be updated.

The only way to give up control is to either pause or finish running. The forever loop never actually stops running, but it has a hidden pause at the end of each iteration that gives up control. If you were to change your program to have a pause block somewhere inside the while loop, this problem would go away!

1 Like

Thank you! i had no idea what the “While” block was! (Ive never used it in any of my games, but i use the “If” block in 99% of my games)

2 Likes

According to the author’s sharing, we can see that he should write the program in Chinese. So maybe the translation of “while do” is not appropriate enough. Now the translation is a bit like the block translation of ‘if do’, it might be better to translate ‘while do’ block into “while repeate”. Translated into Chinese is “当条件为 重复执行”

1 Like