Search code examples
mit-scratch

Is this a valid method for setting a value to "true" in Scratch?


So I have started to create a Plants v.s Zombies mod in Scratch. My main concern right now is attempting to figure out how to code a drag and drop system (i.e. clicking the seed packet and then drag and dropping it), but when I attempt to make it work when I start the project, it doesn't seem to work, since I can only seem to drag the seed packet for some reason, even with turning the seed packet dragging off.

What I have tried (scratchblocks format):


When I click the seed packet

[scratchblocks]
when this sprite clicked
Greater than or equal to (var) (number)
[/scratchblocks]

enter image description here

A possible way I could simplify the "if var is greater than or equal to number" code to use less blocks

[scratchblocks]
define Greater than or equal to(var)(number)
if <(var) > (number)> then 
broadcast [message 1 v]
else
end
[/scratchblocks]

enter image description here

For when the plant that I am making a clone of receives the specific message.

[scratchblocks]
when I receive [message 1 v]
create clone of [myself v]
[/scratchblocks]

enter image description here

The code for the clone (dragging and dropping it [test])

[scratchblocks]
when I start as a clone
if <<mouse down?> = [true]> then 
  show
set [brightness v] effect to (40)
go to x: (mouse x) y: (mouse y)
else
delete this clone
end
[/scratchblocks]

enter image description here


My question


What am I doing wrong that would cause the code to be like this?


Solution

  • You have a few issues in your code.

    1. You never initialize your peashooter cost variable, so it's NOT greater or equal to 100.

    2. The mouse down is a boolean variable, and it is never equal to an empty string. You should compare it to false.

    3. In your when I start as a clone block - why do you have a forever block? As soon as you release the mouse button, the clone will be shown, the code will proceed to the next iteration of that forever, and since the mouse is no longer down - you would delete that clone!