Discuss Scratch

CraZGuy
Scratcher
77 posts

How to make Clones with different scripts

I know it has lists, blocks, variables and what not, but I still can't grasp how to do it. Need help.
coxy5
Scratcher
100+ posts

How to make Clones with different scripts

I would guess you have to give each clone a unique number as it is created and then control them via this number. I haven't tried to do this before so can't help you any more than that i'm afraid. I am sure someone will though.

It might be easier just to make more sprites?
TheLogFather
Scratcher
1000+ posts

How to make Clones with different scripts

You don't necessarily need to use clone IDs nor lists, nor even any sprite-local variables at all. If your needs are not too complex, you can control your new clone simply through the costume number it has when it starts up - and have the original sprite set the costume appropriately just before it creates a clone.

As an example, something like this:
when GF clicked
hide // so you don't see the original sprite switching costume
switch to costume [base v]
repeat (5) // want to make five bases, for example
create clone of [myself v]
end
switch to costume [enemy v]
repeat (10) // want to make ten enemies, for example
create clone of [myself v]
end
switch to costume [player v] // the main sprite could be the player, for example
go to starting position for player...
show
do stuff for player...
when I start as a clone
if < (costume #) = [2] > then // this is the costume # of the base
do stuff for base...
else
if < (costume #) = [3] > then // this is the costume # of an enemy
do stuff for enemy...
end
end
Repeat and extend as required…

One thing to note when using clones is that each clone runs broadcast receive scripts. You may want a clone to behave differently when it receives a broadcast, in a way that depends upon what that clone is (e.g. a ‘base’ vs ‘enemy’ vs ‘player’ in the example above).

Again, you can potentially use just the costume # to do that test, and so respond differently for each type of object, without needing any local variables at all.


Of course, as you start to do more complex things, you may well find you do need local variables (i.e. you set it as “For this sprite only” when you make it), but hopefully the above will be enough to get you starting to experiment with clones!

Last edited by TheLogFather (July 6, 2014 22:31:58)

vidarfw02
Scratcher
100+ posts

How to make Clones with different scripts

Hi, I made an extremely simple project as an example. Very few lines of code(blocks).

http://scratch.mit.edu/projects/24422627/
RedFactoryEdit
Scratcher
6 posts

How to make Clones with different scripts

I tried this, and this is what I came up with.

This should work. The only problem is, you have to make sure no variables are in the do stuff block, it has to be self contained.

Last edited by RedFactoryEdit (Jan. 26, 2016 00:30:08)

BKFighter
Scratcher
1000+ posts

How to make Clones with different scripts

RedFactoryEdit wrote:

I tried this, and this is what I came up with.

This should work. The only problem is, you have to make sure no variables are in the do stuff block, it has to be self contained.
Please don't necropost.
funnygags
Scratcher
1 post

How to make Clones with different scripts

Actually I'm not sure…

Last edited by funnygags (Feb. 12, 2023 10:14:43)

Powered by DjangoBB