Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Sprite cloning is duping/lagging and I dont know why
- Roblockman
-
2 posts
Sprite cloning is duping/lagging and I dont know why
please check out my project at https://scratch.mit.edu/projects/714704608/ sorry it is quite a mess
When the Pea is traveling towards the zombie, it seems to be either duping itself or lagging, creating multiple projectiles. I dont know why or how to fix it. please help.
When the Pea is traveling towards the zombie, it seems to be either duping itself or lagging, creating multiple projectiles. I dont know why or how to fix it. please help.
- RT_Borg
-
1000+ posts
Sprite cloning is duping/lagging and I dont know why
Hi Roblockman,
I think maybe the receive block works a little differently than you expect. In any sprite file with a receive block, the original (non-clone) sprite and all clones receive the message and run the script.
Initially, there's one copy of the assets sprite–the non-clone.
Then peashooter broadcasts “spawn aiming cursor and brain”.
This is received by the single non-clone assets and creates two clones (one for the cursor costume and one for the brain).
Now there are 3 copies of assets, the non-clone, and two clones.
Then peashooter broadcasts “pea hit”.
This is received by all 3 copies of assets (the non-clone, and the two clones).
All three copies set their clone number to 3 and create a clone.
Now there are 6 copies of assets, the non-clone, the cursor, the brain, and 3 new peas.
When you have a sprite that needs to receive messages and also has clones, you typically only want the non-clone to do something when the message is received. So you have to do something to make the non-clone distinct from the clones.
A simple way to do this is:
And then
Now, you can tell them apart and have code like
(You can do other things to make the non-clone distinct from the clones, like assigning the clone-id “0” to the non-clone, and positive numbers to the clones, but you would have to assign your id values differently than you are now. This technique with another variable is very clear and always works.)
I hope this helps.
– RT_Borg
I think maybe the receive block works a little differently than you expect. In any sprite file with a receive block, the original (non-clone) sprite and all clones receive the message and run the script.
Initially, there's one copy of the assets sprite–the non-clone.
Then peashooter broadcasts “spawn aiming cursor and brain”.
This is received by the single non-clone assets and creates two clones (one for the cursor costume and one for the brain).
Now there are 3 copies of assets, the non-clone, and two clones.
Then peashooter broadcasts “pea hit”.
This is received by all 3 copies of assets (the non-clone, and the two clones).
All three copies set their clone number to 3 and create a clone.
Now there are 6 copies of assets, the non-clone, the cursor, the brain, and 3 new peas.
When you have a sprite that needs to receive messages and also has clones, you typically only want the non-clone to do something when the message is received. So you have to do something to make the non-clone distinct from the clones.
A simple way to do this is:
(is-clone) // make a new "For this sprite only" variable
when green flag clicked
set [is-clone v] to [false]
And then
when I start as a clone
set [is-clone v] to [true]
Now, you can tell them apart and have code like
when I receive [spawn aiming cursor and brain v]
if <(is-clone) = [true]> then
stop [this script v] // clones stop the receive script early
end
... // only the non-clone is still running and doing whatever you want now
(You can do other things to make the non-clone distinct from the clones, like assigning the clone-id “0” to the non-clone, and positive numbers to the clones, but you would have to assign your id values differently than you are now. This technique with another variable is very clear and always works.)
I hope this helps.
– RT_Borg
- Roblockman
-
2 posts
Sprite cloning is duping/lagging and I dont know why
Hi Roblockman,
I think maybe the receive block works a little differently than you expect. In any sprite file with a receive block, the original (non-clone) sprite and all clones receive the message and run the script.
Initially, there's one copy of the assets sprite–the non-clone.
Then peashooter broadcasts “spawn aiming cursor and brain”.
This is received by the single non-clone assets and creates two clones (one for the cursor costume and one for the brain).
Now there are 3 copies of assets, the non-clone, and two clones.
Then peashooter broadcasts “pea hit”.
This is received by all 3 copies of assets (the non-clone, and the two clones).
All three copies set their clone number to 3 and create a clone.
Now there are 6 copies of assets, the non-clone, the cursor, the brain, and 3 new peas.
When you have a sprite that needs to receive messages and also has clones, you typically only want the non-clone to do something when the message is received. So you have to do something to make the non-clone distinct from the clones.
A simple way to do this is:(is-clone) // make a new "For this sprite only" variable
when green flag clicked
set [is-clone v] to [false]
And thenwhen I start as a clone
set [is-clone v] to [true]
Now, you can tell them apart and have code likewhen I receive [spawn aiming cursor and brain v]
if <(is-clone) = [true]> then
stop [this script v] // clones stop the receive script early
end
... // only the non-clone is still running and doing whatever you want now
(You can do other things to make the non-clone distinct from the clones, like assigning the clone-id “0” to the non-clone, and positive numbers to the clones, but you would have to assign your id values differently than you are now. This technique with another variable is very clear and always works.)
I hope this helps.
– RT_Borg
Thank you for both explaining the issue in detail, and giving an easy fix to it. I have implemented your fix and it works very well. I very much appreciate you!
Last edited by Roblockman (July 18, 2022 20:21:38)
- Discussion Forums
- » Help with Scripts
-
» Sprite cloning is duping/lagging and I dont know why