Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Custom Blocks
- CoolGuyBug
-
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:
Before:
After:
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
-
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
-
100+ posts
Custom Blocks
Here. It isn't the exact same project, but it's a simple recreation of the glitch.
- -CodePro-
-
100+ posts
Custom Blocks
At the start the sprite is not touching black, so
will always be false until the custom block ends, which it never does.
- Scratch-Minion
-
1000+ posts
Custom Blocks
At the start the sprite is not touching black, sowill 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
-
100+ posts
Custom Blocks
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.I will expand -CodePro- s answer a little:At the start the sprite is not touching black, sowill always be false until the custom block ends, which it never does.
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.
- deck26
-
1000+ posts
Custom Blocks
You don't need the boolean in the custom block call surely? Just do
Last edited by deck26 (Aug. 20, 2019 08:15:33)
- Tutor-42
-
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).
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
-
1000+ posts
Custom Blocks
no, do this:
you might also need to make the script longer like this:
Last edited by miniepicness (Aug. 20, 2019 12:59:46)
- CoolGuyBug
-
100+ posts
Custom Blocks
You don't need the boolean in the custom block call surely?
…
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. no, do this:…
It works! Thank you! 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.
…
- Discussion Forums
- » Help with Scripts
-
» Custom Blocks