Discuss Scratch

Jenbobby
Scratcher
2 posts

How do I make multiple clones within different positions?

I'm making a space invaders style game. I've made it so that each time it clones, the sprites move 10, however when I make a clone x10, only 1 clone appears next to the original. How can I fix this?
CreeperKing777
Scratcher
15 posts

How do I make multiple clones within different positions?

Could you give a link your project so I can look at it? It's hard to tell what's wrong just from your description.
AJK_21
Scratcher
49 posts

How do I make multiple clones within different positions?

CreeperKing777 wrote:

Could you give a link your project so I can look at it? It's hard to tell what's wrong just from your description.
Yes, and you probably need this block:
whenIstartasacloneCodehere...
StampDanFan
Scratcher
100+ posts

How do I make multiple clones within different positions?

You would make a loop that increments a local (i.e. “for this sprite only”) variable.
This also includes the built-in x and y position variables.
Ex:

setxto0repeat10changexby60 Tweak the numbers to fit your gamecreatecloneofenemy

I also created a short tutorial explaining clones in-depth, if you would like to check that out:
https://scratch.mit.edu/projects/433864976/
Jenbobby
Scratcher
2 posts

How do I make multiple clones within different positions?

Thank you! My clones are still pretty broken so if you have any more advice on how to fix them then please lmk
https://scratch.mit.edu/projects/715122152
StampDanFan
Scratcher
100+ posts

How do I make multiple clones within different positions?

Btw the art is really cool! Did you make it yourself?

A few things:
1. The difference between “for this sprite only” variables and “for all sprites” variables
When you create a variable, you can choose it to be for all sprites, or for this sprite only.

For “all sprites” variables (AKA global variables), there is only one instance of them.
Changing them anywhere will also change them everywhere, even in clones.
setmyvarto0 Global variablerepeat5createcloneofthis spritechangemyvarby1
In this example, myvar is being incremented while creating new clones.
Each clone will reference the same value of myvar (meaning myvar is not copied over clones, there is only one variable).
At the end, myvar will equal to 5.
This means that you can't use global variables to store clone-specific information. Each clone will have the same value.

In your project, you a variable called “myID”, which is global.
I'm not sure for how they are meant to be used, but if each clone needs to have a different ID, you shouldn't use global variables.

The solution? Local variables!

Using “For this sprite only” variables (AKA local variables), there is one copy for each clone of a sprite, as well as the main sprite itself.
Changing them will only affect the specific instance.
Important: Note that when a clone is created, it makes a copy of the variables from the instance that created the clone.
setmyvarto0 Local variablecreatecloneofthis spritesetmyvarto1whenIstartasaclonesetmyvarto2
In this example, the main sprite sets myvar to 0, then creates a clone, and finally sets myvar to 1.
Because myvar is a local variable, the main sprite and the clone each has their own version.
When the clone is created, it copies the value of myvar=0, and then sets myvar to 2.
At the end, the main sprite has myvar=1, and the clone has myvar=2.

When making clones with IDs, you can do something like this:
setcloneidto1repeat10createcloneofthis spritechangecloneidby1setcloneidto0
It will create 10 clones, with IDs from 1-10, and the main sprite has an ID of 0.
Replace 10 with whatever number you want.

Note: Certain built-in variables including position, direction, and costume # are local variables.

2. Events
In general, event “hat” blocks trigger ALL sprites and ALL clones of them, with each one running the blocks underneath. *see below for exceptions*
This is why you usually want to put a control flow statement (if/else) using local variables to avoid unexpected behavior.
For example, say you created 10 clones with the example above. (each has a cloneid from 1-10). Then send a broadcast with this code:
WhenIreceiveBroadcastcreatecloneofthis sprite
This will create 11 more clones, with clone ids from 0 to 10. You might want this, you might not depending on the purpose.

*For the “when I start as a clone” and “when this sprite clicked” hat blocks, it is only triggered for the relevant clone/sprite.

3. Redundant variables
*Note: I don't fully understand the purpose of all your variables, don't delete them if I misunderstood*
In your code, I see a xvar/yvar position variable, and two id variables?

You dont need the xvar/yvarposition variables, as they are already variables! just use the built-in:
xpositionypositionchangexby...setxto...changeyby...setyto...gotox:...y:...
And you only need one id variable, if any.
I don't see any use for them, as collision detection and deletion don't need them.

Additionally, there are unused xpos and ypos lists, and an unused “createcloneid” variable.
Unless other clones/sprite need to know every clones' position, you probably don't need the lists.

4. Enemy movement
Right now, it looks like your enemy movement is finite.
If the player does nothing for long enough, the enemies will stop moving.
To remedy this, you might want to create a playing? variable that will be either 0/1 (or true/false) depending on the state of the game.
Then you can use a repeat until block:
repeatuntilplaying?=0move1move2. . .

Sorry if this got really long lol. Hope it helped!

Powered by DjangoBB