Help with code: Clones + Randomizing movement

Hey guys, I’m currently working on coding a version of the game Space Race (I explained more about my plan for the game here) and I need to find a way to have a randomized amount of clones of one object move left and right across the screen at random speeds and at different y positions.

Incase that doesn’t make sense, here’s a quick sketch:

This stars will all be at random positions on both sides of the screen, shooting back and forth.

I’m having trouble coding this because I can’t find a way to use clones to have the stars be in random position, and then all move back and forth with random speeds. If anyone has any ideas how to do this, please let me know!

9 Likes

What you are going to want to do is use self variables. Clones can access self variables and set them to their own things. So you could use
(Self)move = rand(1,5)
change x by (self)move

3 Likes

Here you go!

Just click on the link and look through the code. I’ve added a couple comments explaining what to change, so be sure to look in the set text objects once.

The code is ftu so feel free to copy/paste it into your project, no credit required.

Hopefully this helped, lmk if you need anything else or have any questions and I’d be happy to help

3 Likes

thank you, I will try that

3 Likes

Thank you so much!!

2 Likes

No problem!

2 Likes

You could just set position to either edge of the screen for x (multiply a random number between 1 and 0 by width, random rounds the number) with a random y position from 0 to height. Then use the set speed block to a random number.

Then just move by -1 * width points if you are at the right edge or +1 * width points if you are at the left edge. You can compare your x position to width (or 0, or the middle of the screen) or you can use math to give a result that is -1 if you are on the right edge or +1 if you are at the left edge.

5 Likes

I agree wholeheartedly

7 Likes

Thank you! I’m going to try out all the ways that people suggested here and see which way works the best for what I’m envisioning.

2 Likes

I used your code, and with a few minor adjustments it worked exactly how I wanted it to! Thank you so much!

2 Likes

No problem! I’m glad it helped :D

1 Like