Discuss Scratch

TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

NOTE: This is not an easy script! It may seem like it, but it's not. (Unless I'm missing something obvious)

I hope the title wasn't confusing. Basically what I am asking is well…

I have a game in which there are six sprites that move randomly about the screen in any direction. These sprites cannot be controlled by whoever is playing the game and should not point towards the player either. I was wondering how I could make it that they face in the direction (Only the X direction) that they are going in. For example:

If they head positive 50 steps, they face to the right, if they move negative 50 steps they face to the left.

Any help would be greatly appreciated.

To see the game click the “Click” Button in my signature that is part of the “Click One Of These Words” Sentence.

Last edited by TheCentipede (Jan. 12, 2016 03:02:55)


You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

You do it by changing their direction first and then using the MOVE block.

The MOVE block moves them in whatever direction they are facing.


SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

@footsocktoe , but the direction is unforeseen so I can't tell which direction they'll need to face

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

Unforeseen but not unknown. There is nothing unknown in a computer program.


SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

Whatever code makes them change direction holds the info you need.


SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

The code looks like this:

glide (1) secs to x: (pick random (-190) to (190)) y: (pick random (-190) to (190))

No way I can tell which direction it might go

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

Sure there is. You know where they started and you know where they end up. (Position X and Position Y blocks)
Trigonometry gives you the direction. Y / X is the tangent of the angle.

But maybe there is another way. I'll have to think about this.

Last edited by footsocktoe (Jan. 12, 2016 03:14:53)



SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

By the way, do you want full rotation of the sprite, or just left-right rotation?

Because left-right would be easy. You just check whether the randomly picked x is going to be to the left or right of the current x position

Last edited by footsocktoe (Jan. 12, 2016 03:18:01)



SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

footsocktoe wrote:

By the way, do you want full rotation of the sprite, or just left-right rotation?

Because left-right would be easy. You just check whether the randomly picked x is going to be to the left or right of the current x position
Just left and right, and it's more difficult than you might think. I know how to do it, it's just that there is no scratch block allowing me to, and an extension couldn't fit where I needed it to. Thanks for helping, I hope we find an answer

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

Here is the way I would do it.

New X = pick random -190 to 190
If (New X) - (X position) > 0 then
Set direction 90
If (New X) - (X position) < 0 then
Set direction 270
Glide 1 second (New X) (Pick random for Y)


SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

footsocktoe wrote:

Here is the way I would do it.

New X = pick random -190 to 190
If (New X) - (X position) > 0 then
Set direction 90
If (New X) - (X position) < 0 then
Set direction 270
Glide 1 second (New X) (Pick random for Y)

Okay, thank you very much. I don't have time to test it tonight but I will add you to the credits of the game, thanks again!

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

I mean it looks like it works (It should).

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

Wait… is new x suppose to be a variable or an extension block?

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

It worked

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
footsocktoe
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

Great!


SHOOT THE SPACE MONSTERS! ….. A game everyone can play! Bright colors, bonky sounds!
THE 12 BALLS OF CRAZY AL ……. New scrolling adventure game!

zp100
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

Guys, guys! This is really easy! Instead of having it only move + or - steps, simply have it turn the direction you want it to point!

point in direction ( v)
move () steps

So it'll move in whatever direction it's pointing, and you can detect this with
(direction)

Logic will get you from A to B. Imagination will take you everywhere.

- Albert Einstein
deck26
Scratcher
1000+ posts

Make an unplayer controlled sprite switch directions

zp100 wrote:

Guys, guys! This is really easy! Instead of having it only move + or - steps, simply have it turn the direction you want it to point!

point in direction ( v)
move () steps

So it'll move in whatever direction it's pointing, and you can detect this with
(direction)
Not so simple if they want to glide to an x,y position.
zp100
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

Hmm, good point…
You could create a sprite. In the scripts I'll call it “sprite 2.” Have it hide when flag is clicked. When the first sprite needs to move somewhere, broadcast “move.” Have it go to wherever you want the original sprite to glide to.
when I receive [move v]
go to x: () y: ()

For the original sprite:
when I receive [move v]
wait (0.001) secs
point towards [sprite 2 v]
repeat ((distance to [sprite 2 v]) / [x])
move [x] steps
end
go to [sprite 2 v]

Make “x” whatever number you want. It represents the speed that your sprite moves.

Try this and tell me if it works.

Last edited by zp100 (Jan. 14, 2016 03:37:03)


Logic will get you from A to B. Imagination will take you everywhere.

- Albert Einstein
TheCentipede
Scratcher
100+ posts

Make an unplayer controlled sprite switch directions

zp100 wrote:

A script.
Wasn't exactly the type of script I needed

You should definitely feel mediocre at best compared to TheCentipede , DO NOT FEEL BAD
Phosphor
Scratcher
71 posts

Make an unplayer controlled sprite switch directions

Try organizing your scripts into a movement script, hit detection script, and game over script.

when I receive [game start v]
go to x: (pick random (-240) to (240)) y: (pick random (-180) to (180))
show
forever
wait (pick random (1) to (3)) secs
set [new x v] to (pick random (-240) to (240))
set [new y v] to (pick random (-180) to (180))
if <(new x) < (x position)> then //will move left
point in direction (-90 v)
end
if <(new x) > (x position)> then //will move right
point in direction (90 v)
end
glide (3) secs to x: (new x) y: (new y) //move to new location
end

when I receive [game start v]
wait until <touching [you v] ?>
broadcast [game over v]

when I receive [game over v]
stop [other scripts in sprite v]
hide

I can't actually test the code above right now, but let us know how it works!

Powered by DjangoBB