Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » how to make clones go to clones
- EmprorQ
-
10 posts
how to make clones go to clones
How do I make a clone go to another when it starts clone?

- drmcw
-
1000+ posts
how to make clones go to clones
@scubajerry has some tower defence projects which point to the nearest clone using lists and @TheLogFather has a project which does that but without lists.Once you get a clone to point to another then it's just a case of using the move block!
- DadOfMrLog
-
1000+ posts
how to make clones go to clones
I think the essential question is how to find the appropriate co-ordinates to start the new clone - since there are lots of clones that the new clone may be starting from, right, each in different positions…?
There are three ways I'd suggest…
1) Have the first clone (the shooter, say) set a couple of global variables containing its co-ordinates before it starts up the second clone (the bullet, say). The second clone would then start from the position given by those variables.
Downside: you need to make sure two new clones (bullets) are not created at the same time by different clones (shooters), otherwise the global values could get mixed up.
2) The second way is the same in essence, except that you keep a list of co-ordinates for the first set of clones, and then set a global variable which says which of them is shooting. Then the new clone (bullet) would see which clone is shooting when it starts up, and so can read the co-ords from the list. This has the extra potential advantage that you can make some other use of the list of co-ords at some point (though if you don't need them, then the first method may be better).
Downside: also may need some kind of locking mechanism to prevent two shooting clones from shooting at the same time…
3) This would be the method I would tend to use, since it does not require any global variables or lists…
Have the shooters and the bullets all in the same sprite. Then, just before it creates a clone of itself, you just have the shooter set a local variable (i.e. set for this sprite only) which says it's shooting (and then set it back after clone created). When the clone starts, you check that local variable, and when you see it's being shot, then you can switch to a bullet costume and do everything you need.
The advantage of this is that the new clone (bullet) will start with the same position as the creator (shooter). You also have a local variable which says whether the clone is a shooter or a bullet that you can use to check when receiving broadcasts (and so act accordingly).
Downside: you end up with the scripts for controlling both types of clone all in the same sprite, so it may feel somewhat less tidy in terms of finding/editing scripts…
Hope that helps!
There are three ways I'd suggest…
1) Have the first clone (the shooter, say) set a couple of global variables containing its co-ordinates before it starts up the second clone (the bullet, say). The second clone would then start from the position given by those variables.
Downside: you need to make sure two new clones (bullets) are not created at the same time by different clones (shooters), otherwise the global values could get mixed up.
2) The second way is the same in essence, except that you keep a list of co-ordinates for the first set of clones, and then set a global variable which says which of them is shooting. Then the new clone (bullet) would see which clone is shooting when it starts up, and so can read the co-ords from the list. This has the extra potential advantage that you can make some other use of the list of co-ords at some point (though if you don't need them, then the first method may be better).
Downside: also may need some kind of locking mechanism to prevent two shooting clones from shooting at the same time…
3) This would be the method I would tend to use, since it does not require any global variables or lists…
Have the shooters and the bullets all in the same sprite. Then, just before it creates a clone of itself, you just have the shooter set a local variable (i.e. set for this sprite only) which says it's shooting (and then set it back after clone created). When the clone starts, you check that local variable, and when you see it's being shot, then you can switch to a bullet costume and do everything you need.
The advantage of this is that the new clone (bullet) will start with the same position as the creator (shooter). You also have a local variable which says whether the clone is a shooter or a bullet that you can use to check when receiving broadcasts (and so act accordingly).
Downside: you end up with the scripts for controlling both types of clone all in the same sprite, so it may feel somewhat less tidy in terms of finding/editing scripts…
Hope that helps!
Last edited by DadOfMrLog (May 15, 2014 15:50:18)
- EmprorQ
-
10 posts
how to make clones go to clones
I just remembered something it's a lot more complex then what I said at first. It's a clone that's firing a clone at a clone.
Last edited by EmprorQ (May 15, 2014 17:33:12)
- DadOfMrLog
-
1000+ posts
how to make clones go to clones
Doesn't make much difference in terms of the bullet clone creation - the principles I outlined above still apply when creating a bullet. The only extra bit is that you need to make the clones doing the shooting also point to another specific clone. I just remembered something it's a lot more complex then what I said at first. It's a clone that's firing a clone at a clone.
Look up tower defence projects, and you'll find plenty of examples of how to do all this kind of thing using lists. (And there's also the list-less method in my sig that drmcw mentioned, although I think that can appear more complicated to follow than a list-based method, despite it being basically the same in its methodology - just using carefully synchronised broadcasting to do the calculations over multiple scripts in multiple clones, rather than in a single script with loops over list items.)
Last edited by DadOfMrLog (May 15, 2014 18:11:33)
- EmprorQ
-
10 posts
how to make clones go to clones
I'v just decided to make the mouse control the weapons it adds a bit of challenge to the game and it's easy to script.
- DadOfMrLog
-
1000+ posts
how to make clones go to clones
Decided I'd throw together a little game that demonstrates that third clone-firing method I mentioned above:

http://scratch.mit.edu/projects/22222215/
Enjoy!

http://scratch.mit.edu/projects/22222215/
Enjoy!

- lmsp7cm
-
19 posts
how to make clones go to clones
Okay, this is cool, but my only problem is that these clones are not moving at all. I am trying to make a game where the clones are moving, but then they stop to shoot until the enemy is no longer there, then they continue to move and the cycle repeats. @DadOfMrLog, how would you make a clone go to a moving clone?
- DadOfMrLog
-
1000+ posts
how to make clones go to clones
OK, the fact that they don't move makes no difference to the firing part of this - it's one of the features of creating clones using that third method which means where the clone is makes no difference - because the firing clone is creating its own bullets, they will automatically start from exactly the right place.
Making a clone go to another, though, does require some way to have some way to choose a particular clone, and then retrieve its information (i.e. co-ordinates, or distance+direction) so that the ‘following’ clone can go to it, or move towards it, or at least point towards it in some way.
As mentioned before, there are essentially two methods…
First, and I think somewhat simpler to understand the basic concept and script, is to use lists to store the clones' co-ordinates. Then, as long as you know how to identify a particular clone, you can look up where it is from the lists. There are loads of examples of tower-defense type projects that you should be able to look up and learn from for that.
Secondly, and more difficult to follow the flow of the scripts I think, is to use something like the list-less method I mentioned above, and linked by drmcw.
Does that help…? If you have more specific questions about certain aspects of above, then fire away…
Making a clone go to another, though, does require some way to have some way to choose a particular clone, and then retrieve its information (i.e. co-ordinates, or distance+direction) so that the ‘following’ clone can go to it, or move towards it, or at least point towards it in some way.
As mentioned before, there are essentially two methods…
First, and I think somewhat simpler to understand the basic concept and script, is to use lists to store the clones' co-ordinates. Then, as long as you know how to identify a particular clone, you can look up where it is from the lists. There are loads of examples of tower-defense type projects that you should be able to look up and learn from for that.
Secondly, and more difficult to follow the flow of the scripts I think, is to use something like the list-less method I mentioned above, and linked by drmcw.
Does that help…? If you have more specific questions about certain aspects of above, then fire away…
- lmsp7cm
-
19 posts
how to make clones go to clones
Thank you! It took me a while, but I did figure out how to do this myself. It uses a constantly changing x coordinate list that stops changing any list that is a certain distance to an enemy clone. Thank you for explaining it. My next question is about how to make a clone go only to one part of a list, because the way I have it is it goes to a random code instantly, and that causes multiple clones to sometimes disappear and reappear. Help would be great, and an example would be better! Thank you!
- EmprorQ
-
10 posts
how to make clones go to clones
What about making an AI clone fire at a AI clone?
- lmsp7cm
-
19 posts
how to make clones go to clones
What kind of game are you trying to make? I can try to help, but all an AI does is follow a bunch of “if” “ands”. I do not know how to set a number, but I am working on a game that changes coordinates on a list and the unit goes to a random item on the list forever. I hope this helps!
- EmprorQ
-
10 posts
how to make clones go to clones
It's hard to explain but this is the game #http://scratch.mit.edu/projects/21466339/ I'm trying to make an AI that defends the base by attacking the AI Enemy's that attack back .
- lmsp7cm
-
19 posts
how to make clones go to clones
Okay, I did a little research. First off, clones make a copy of private variables for its normal sprite. This can make it so that you can set one of these new cloned variables to any number or name you like, and the clone will remember it and it will only be for that clone. Do this by setting one of these cloned variables to a non private variable that all sprites share. The catch, the cloned variables not affect the original variable whatsoever. This is means you can have a clone with its own health, name, and x and y value. Hope this helps!
- azbl
-
1 post
how to make clones go to clones
you can make the clones constantly updating a list so all sprites can know where it is.
- lmsp7cm
-
19 posts
how to make clones go to clones
Yep! You most certainly can. This allows for things like targeting systems to work on clones if that is what you are wondering.
- Discussion Forums
- » Help with Scripts
-
» how to make clones go to clones