Discuss Scratch

NoWayItsAj
Scratcher
1 post

Costume Rotation

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
mrtbts
Scratcher
100+ posts

Costume Rotation

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

Last edited by mrtbts (May 14, 2020 04:02:03)


when I receive [unansered post v]
answer [post v] :: looks
say [l'm glad to help]
Can someone join my intro contest please????
my profile I my school account I my test account
Scratch-Minion
Scratcher
1000+ posts

Costume Rotation

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).
mrtbts
Scratcher
100+ posts

Costume Rotation

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

when I receive [unansered post v]
answer [post v] :: looks
say [l'm glad to help]
Can someone join my intro contest please????
my profile I my school account I my test account
Oumuamua
Scratcher
1000+ posts

Costume Rotation

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

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


1234490873
Scratcher
55 posts

Costume Rotation

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


“Get busy living, or get busy dying.”
-Stephen King
“Get busy scratching, or get busy dying.”
-1234490873

While you are reading this, don't forget to check out my projects.
(Last released) Stickmen Platformer
Car Parking Game
DUDU's Adventure
Ghost Cat Platformer
PUBG Platformer
Coronavirus Information updated 8/5/2020
Dash In hell Platformer game
Stickmen Battle
Zombie shooting game
Football game
Shooting coronavirus game
go to [ 1234490873]
say [Check out my projects (This is a secret)] for (2) secs
if <You've checked out my projects> then
Why not leave a like, a fav, or a comment? I'll be really happy if you'd tell me how I can improve them!
end
if <You have> then
say [Thank you so much for visiting my project!]
end
if <More suggestions> then
I'll try my best to improve them
end
Thank you
See ya
deck26
Scratcher
1000+ posts

Costume Rotation

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
Oumuamua
Scratcher
1000+ posts

Costume Rotation

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.

Last edited by Oumuamua (May 14, 2020 08:01:58)



Scratch-Minion
Scratcher
1000+ posts

Costume Rotation

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.
Oumuamua
Scratcher
1000+ posts

Costume Rotation

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.


Powered by DjangoBB

Standard | Mobile