Topic Information

Costume Rotation by NoWayItsAj in Help With Scripts

Posts: 10

1: NoWayItsAj wrote at 5:49:39 AM 14/5/20204010793
Hello!

I have been experimenting with some ways to make a sprite follow my mouse cursor (Similar to the game diep.io), but instead of using a script where the sprite constantly faces the mouse pointer, I want to make the sprite change costumes based on where the mouse pointer is on the stage. I currently have costumes for every 45° increment. The sprite would always stay in the middle of the stage.

I understand if this is extremely difficult or impossible.

Thanks in advance,

NoWayItsAj
2: mrtbts wrote at 8:01:21 AM 14/5/20204010971
you can make 8 costume for 0,45,90.135,180,225,270,305 degree
then put this code
when green flag clicked
forever
set rotation style [don't rotate v]
point towards [mouse pointer v]
if <<(direction) > [327.5]> and < (direction) < [22.5]>>then
switch costume to [0 v]
else
if <<(direction) > [22.5]> and < (direction) < [67.5]>>then
switch costume to [45 v]
else
if <<(direction) > [67.5]> and < (direction) < [112.5]>>then
switch costume to [90 v]
else
if <<(direction) > [112.5]> and < (direction) < [157.5]>>then
switch costume to [135 v]
else
if <<(direction) > [157.5]> and < (direction) < [202.5]>>then
switch costume to [180 v]
else
if <<(direction) > [202.5]> and < (direction) < [247.5]>>then
switch costume to [225 v]
else
if <<(direction) > [247.5]> and < (direction) < [292.5]>>then
switch costume to [270 v]
else
if <<(direction) > [292.5]> and < (direction) < [327.5]>>then
switch costume to [305 v]
end
end
end
end
end
end
end
end
end
pew! after a very long time of writing i finally finish this post
3: Scratch-Minion wrote at 9:05:52 AM 14/5/20204011032
The direction reporter in Scratch varies from -180 to 180.
So the above solution would need to be modified to work as it uses angles from 0 to 360.


But here is a shorter solution assuming your costumes 1 to 8 are for angles 0,45,90,135,180,225,270,315.

switch costume to (((round ((direction) / (45))) mod (8)) + (1))

This will return the numbers 1 to 8 using Scratch directions from -180 to 180.
(Note that costume 1 corresponds to angle 0 not -180).
4: mrtbts wrote at 9:13:09 AM 14/5/20204011039

Scratch-Minion wrote:

The direction reporter in Scratch varies from -180 to 180.
So the above solution would need to be modified to work as it uses angles from 0 to 360.


But here is a shorter solution assuming your costumes 1 to 8 are for angles 0,45,90,135,180,225,270,315.

switch costume to (((round ((direction) / (45))) mod (8)) + (1))

This will return the numbers 1 to 8 using Scratch directions from -180 to 180.
(Note that costume 1 corresponds to angle 0 not -180).
wow your great
5: Oumuamua wrote at 9:27:42 AM 14/5/20204011049
when green flag clicked
forever
point towards [mouse-pointer v]
switch costume to (round ((direction) / (45)))
end

https://scratch.mit.edu/projects/395175849/
6: 1234490873 wrote at 9:56:18 AM 14/5/20204011075
This is a mobile-friendly platformer game of 20 challenging levels!
I'll adapt your suggestion if you think there is something I could improve.
Ghost Cat Platformer

7: deck26 wrote at 11:50:24 AM 14/5/20204011205

Oumuamua wrote:

when green flag clicked
forever
point towards [mouse-pointer v]
switch costume to (round ((direction) / (45)))
end

https://scratch.mit.edu/projects/395175849/
Ideally only switch costume when you know it needs to change or you're forcing a screen refresh every frame.

set initial costume
forever
work out costume number
if new number <> current costume then switch costume
end
8: Oumuamua wrote at 11:54:12 AM 14/5/20204011215

deck26 wrote:

Oumuamua wrote:

when green flag clicked
forever
point towards [mouse-pointer v]
switch costume to (round ((direction) / (45)))
end

https://scratch.mit.edu/projects/395175849/
Ideally only switch costume when you know it needs to change or you're forcing a screen refresh every frame.

set initial costume
forever
work out costume number
if new number <> current costume then switch costume
end

Yes, i knew that, it is just a proof(the project) of concept.
Greets.
9: Scratch-Minion wrote at 2:30:34 PM 14/5/20204011446

Oumuamua wrote:

when green flag clicked
forever
point towards [mouse-pointer v]
switch costume to (round ((direction) / (45)))
end

https://scratch.mit.edu/projects/395175849/

Note that if you use this formula as opposed to mine, the difference is that the last costume is for direction 0 (straight up) whereas with my formula the first costume is for direction 0. Both work.
10: Oumuamua wrote at 2:39:28 PM 14/5/20204011469

Scratch-Minion wrote:

Oumuamua wrote:

when green flag clicked
forever
point towards [mouse-pointer v]
switch costume to (round ((direction) / (45)))
end

https://scratch.mit.edu/projects/395175849/

Note that if you use this formula as opposed to mine, the difference is that the last costume is for direction 0 (straight up) whereas with my formula the first costume is for direction 0. Both work.
Yes, I did it on purpose, obviously the position on the costumes' pane is important for it to work well.
By the way, I am a big fan of you and of your excellent projects.