Yash Kumar
Yash Kumar

Reputation: 11

What should I do to move a sprite randomly between two specific placements?

What should I do to move a sprite randomly between two specific placements.
For example, how can I implement this:
If direction = 90
Then, got to x:10 y:20 or x:30 y:50

(I have just started using scratch and have no coding experience so I won't be able to understand any code.)

Upvotes: 0

Views: 988

Answers (2)

User
User

Reputation: 376

Supplementary to Mithrandir's answer:

If you don't want to use a variable, you can do it like this:

This solution is basically the same but saves a little bit of time and space if you don't need to use the variable anymore.

Solution without using any variables

Or, in pseudocode:

if (direction) = 90 then
  if {pick random (1) to (2)} = 1 then
    go to x: (10) y: (20)
  else
    go to x: (30) y: (50)

Upvotes: 1

Mithical
Mithical

Reputation: 624

Here's one way you can do it:

transcript below

if (direction) = 90 then
  set (variable) to {pick random (1) to (2)}
  if (variable) = 1 then
    go to x: (10) y: (20)
  else
    go to x: (30) y: (50)

The way this works is, when the trigger ((direction) = (90)) is met, we choose where to go by picking randomly between 1 and 2. The project then sets a variable to the value of the number picked. We can then use that value to decide which option gets picked. If the number 1 was picked, then go to one option; if it was the other (since there are no other options there's no need to out in an explicit IF (variable) = 2), go to the other spot.

Remember that you will need a hat block (a starting block) to start the script, though, such as WHEN GREEN FLAG CLICKED. Also note that if you just use an IF block immediately after a hat block, the script will just check it once immediately after the project starts. If you want this to happen in the middle of the project, wrapping this whole thing inside a FOREVER block might be a good thing to do.

Upvotes: 0

Related Questions