Discuss Scratch

CoolGuyBug
Scratcher
100+ posts

Custom Blocks

I was always under the impression that custom blocks worked the same as regular blocks (unless “Run without screen refresh” is checked), so if you were to switch out a part of code for a custom block, it would run the same. This was always the case for me. So, while coding a platformer, I tried to put the gravity script as a custom block and make the <touching (ground)?> a boolean in the new block, like so:

Before:
when green flag clicked
forever
if <touching [ground v] ?> then
long gravity script :: grey
end
end
After:
when green flag clicked
forever
gravity <touching [ground v] ?> :: custom
end

define gravity <touching>
if <touching :: custom> then
long gravity script :: grey
end
But for some reason, this new script doesn't work. The most confusing thing is, the majority of the code does work, but there are a few bits at the end that need the original <touching (ground)?> instead of the new <touching> boolean, even though the two should be identical. For reference, I'm using a variant of @djpro's gravity script. Is this a glitch, or is there a reason behind it? Is there any way to fix it?
codeman1044
Scratcher
1000+ posts

Custom Blocks

I don't think there's a reason for this other than a scripting error. Could you share your project so we could see what you have?
CoolGuyBug
Scratcher
100+ posts

Custom Blocks

Here. It isn't the exact same project, but it's a simple recreation of the glitch.
-CodePro-
Scratcher
100+ posts

Custom Blocks

gravity <touching color [#000]> :: custom
At the start the sprite is not touching black, so
<boolean ::custom>
will always be false until the custom block ends, which it never does.
Scratch-Minion
Scratcher
1000+ posts

Custom Blocks

-CodePro- wrote:

gravity <touching color [#000]> :: custom
At the start the sprite is not touching black, so
<boolean ::custom>
will always be false until the custom block ends, which it never does.


I will expand -CodePro- s answer a little:

As -CodePro- says, you only call the custom block gravity once at the start of the project.
When you call gravity, the sprite is not touching black, so the input boolean is set to the value false and never changes.

Every part of your code inside an “if boolean then” block is never run.
The code for the left and right arrows is not inside a “if boolean then” block, so you can move left and right.

When the cat moves down with gravity, “if boolean then” is always false. It is the value of the input when the custom block was called.
But if you use “if touching color black” then the script tests again if the cat is touching black and when the cat moves down with gravity this becomes true.
CoolGuyBug
Scratcher
100+ posts

Custom Blocks

Scratch-Minion wrote:

-CodePro- wrote:

gravity <touching color [#000]> :: custom
At the start the sprite is not touching black, so
<boolean ::custom>
will always be false until the custom block ends, which it never does.
I will expand -CodePro- s answer a little:

As -CodePro- says, you only call the custom block gravity once at the start of the project.
When you call gravity, the sprite is not touching black, so the input boolean is set to the value false and never changes.

Every part of your code inside an “if boolean then” block is never run.
The code for the left and right arrows is not inside a “if boolean then” block, so you can move left and right.

When the cat moves down with gravity, “if boolean then” is always false. It is the value of the input when the custom block was called.
But if you use “if touching color black” then the script tests again if the cat is touching black and when the cat moves down with gravity this becomes true.
Okay, I understand. Thanks! Is there any way to fix it? I've tried putting the “forever” block under “when green flag clicked” (as I put in the example above, as well as the original project), but it still seems to get the same result.
deck26
Scratcher
1000+ posts

Custom Blocks

You don't need the boolean in the custom block call surely? Just do

if <touching [ground v]> then
call custom block
end

Last edited by deck26 (Aug. 20, 2019 08:15:33)

Tutor-42
Scratcher
100+ posts

Custom Blocks

If you want to be able to detect whether you are touching a particular RGB colour (eg. 0 for black), you can pass the RGB colour as an input to the custom block.
Then test inside the custom block if you are touching color RGB colour.

ie. the colour that you are testing for is an input, but you do the test if you are touching the colour inside the custom block.


(You could also test if you were touching a sprite by passing its name into the custom block as an input).
miniepicness
Scratcher
1000+ posts

Custom Blocks

CoolGuyBug wrote:

when green flag clicked
forever
gravity <touching [ground v] ?> :: custom
end

define gravity <touching>
if <touching :: custom> then
long gravity script :: grey
end
no, do this:
when green flag clicked
forever
gravity :: custom
end

define gravity
if <touching [ground v] ?> then
yes script thanks
end
you might also need to make the script longer like this:
define gravity
if <touching [ground v] ?> then
...
end
if <not <touching [ground v] ?>> then
...
end

Last edited by miniepicness (Aug. 20, 2019 12:59:46)

CoolGuyBug
Scratcher
100+ posts

Custom Blocks

deck26 wrote:

You don't need the boolean in the custom block call surely?

miniepicness wrote:

no, do this:
when green flag clicked
forever
gravity :: custom
end
For the purposes of the project I'm working on, it would be most convenient to have the boolean in the custom block. That's why I'm asking.

Tutor-42 wrote:

If you want to be able to detect whether you are touching a particular RGB colour (eg. 0 for black), you can pass the RGB colour as an input to the custom block.
Then test inside the custom block if you are touching color RGB colour.
It works! Thank you!

Powered by DjangoBB