So, I wanted to make it so that you could add more than one sprite, and I’ve encountered many issues while doing that! Using blocks once you’ve used javaScript is frustrating sometimes, because sometimes it does things behind the scenes that I would do very, very differently.
This is more of a giant explanation for how I did this, so it’s a very, very long post. I like doing this more than just sending a solution because I think it’s more interesting, and more fun! For me at least. I don’t know if anyone else is gonna have fun reading all this!
Spoilers!
My first “solution” used On Game Update but otherwise works about as well as it could possibly work (I’ll get to that later)
This was a bit more complicated before I cheated and looked at the actual source code for the follow block. I was doing the button checks and then moving the sprites inside the button IF blocks, instead of holding the button state inside variables and then using it later, which is much cleaner.
Then only after I had looked at the actual implementation did I realize that technically I shouldn’t be using the On Game Update block (even though that’s basically how the actual code works) so I just changed it to make it look better.
Then I tried to turn it into a single function like this, to stop using the On Game Update block:
(The “Run In Parallel” block is from my JavaScript Extras extension but it’s the exact same code as the “Separately Do” block from the timers extension)
But it turns out that since I’m not using them anywhere else, those arrays have become local variables instead of global variables, which means every time you run the function they are created new, so I changed the code to this:
Which still doesn’t work because the “isMoving” variable is still global.
My main struggles here were due to the way Makecode does global and local variables. Most variables are global but for some reason arrays are not global, which is truly annoying to me. I mean, I know that it wouldn’t be an issue if I could just add this code:
But I cannot, so I made the isMoving state variable into an array in the hopes that it would become a local variable like the other ones, but it… didn’t? Yeah. It became a global variable instead. Looking at the javaScript, I could see that for some reason this one is defined outside of the function, making it global, instead of inside the function, like the sprite arrays. This led me to realize that, for some reason, if I define a sprite array normally inside the function it becomes global!
But for whatever reason simply adding to an array inside the function without ever defining it, like I was doing before, leads to it being a local variable. So now, finally, I can actually do this whole thing inside a single function by tricking Makecode into making my sprite arrays global! I also got tired of the Run In Parallel + While loop thing, so I added an inline On Game Update block to my JavaScript extras extension just because I can. This turned out to be a really cool small adventure! Here’s my final solution:
https://arcade.makecode.com/S72282-30700-01822-40073
The smiley faces are controlled by the original block, and the boxes around them are controlled by my function!
The reason none of our functions could ever be perfect is because Makecode is a big meanie and the move sprite code is handled right before the sprite physics is done, then all our code is run after that. This means that the move sprite block will always literally be one step ahead of our code:
How unfair!




