Discuss Scratch

lucasbroo
Scratcher
14 posts

Help with tower defense plz

when I start the next round by pressing it, the right amount of sprites come out. but when auto start is pressed (space) then a extra sprite is sent at the start of each round. any suggestions?
https://scratch.mit.edu/projects/880759057/editor/
RokCoder
Scratcher
1000+ posts

Help with tower defense plz

Your “when space key pressed” script in Sprite1 is unreadable. I have no idea what you're trying to achieve with your mysteriously named “! or @” variable but that script looks to be the cause of the problem. Why not just -

when [space v] key pressed
if <(Round done?) = (true)> then
broadcast [Next round v]

Presumably the five “wait 0 seconds” blocks are to cause a five-frame pause but I don't know why you'd want to do that. You're then doing… well, I don't know what and there are no comments to show why you're doing whatever it is that you're doing there

I guess explaining why you need all the extra code rather than the short snippet above might help with understanding the issue.

As a side note, you also need a check to ensure you're in the game. Otherwise, you can start the next round by pressing the space bar before a game has started.


Scratch dabbling for fun…

lucasbroo
Scratcher
14 posts

Help with tower defense plz

The extra code allows you to press space again to disable auto play, and I named the variables random names sorry about that lol.
RokCoder
Scratcher
1000+ posts

Help with tower defense plz

lucasbroo wrote:

The extra code allows you to press space again to disable auto play, and I named the variables random names sorry about that lol.
It's fair to say that randomly named variables are a terrible idea. They'll mean nothing to you if/when you go back to look at the code weeks from now.

That aside, when you press space your code is broadcasting “Next round”. At this point, “Round done?” is false. Because of the way broadcasts work, “Round done?” isn't set to true until after the wait until block has executed which means code continues to the end of the “repeat until” loop and then back to the beginning. It broadcasts “Next round” again (which is why you're getting the extra sprite count) and then stops and waits at the “wait until”.

The code needs rewriting to work as you want it to anyway. You have code in there that checks for the space bar being pressed but, if the space bar is pressed it will just restart that entire “when space key pressed” block anyway.


Scratch dabbling for fun…

lucasbroo
Scratcher
14 posts

Help with tower defense plz

I renamed the variables as you recommended, can you help me fix the code? Because I don't know how broadcasting next round again is adding only one extra clone? thank you for helping!
lucasbroo
Scratcher
14 posts

Help with tower defense plz

I fixed the problem, it was exactly what you said! thank you so much!

Powered by DjangoBB