Changing costumes based on direction

id: 628398

category: Help with Scripts

posts: 10

icon96 icon96 loading
I'm trying to have the Tasmanian devil chase the bunny and change costumes depending on where the bunny is located, but it keeps switching to the wrong costume, anyone know what I'm doing wrong?

The link to my project:
Your text to link here…
povthebox povthebox loading
your code is really unorganized right now, organize it and i may help you
deck26 deck26 loading

icon96 wrote:

fixed it
The problem or the organisation of the project?
icon96 icon96 loading
The organization
RT_Borg RT_Borg loading
Hi icon96, Welcome to Scratch!

It looks like you've mostly got your costumes switching when you want now. Congratulations!

If you want the most precise direction numbers for when you should switch, change +/- 55 to +/- 45, and change +/- 120 to +/- 135.

(In math, the angle between horizontal and vertical is 90 degrees. The 45 degrees I'm suggesting comes from 1/2 that 90 degrees. Then to rotate a 1/4 of a circle, it's 45 + 90 = 135. Similar angles for the - values going counter-clockwise.)

Also, this is a great place to learn an important lesson about animations in scratch. That is, it's always good to separate your movement code and your animation code into 2 different sprites.

What's happening every time through your loop is:
  1. follow (animation runs)
  2. point toward bunny
  3. move 1 step

But when the animation runs, it picks the right direction and does costume changes with pauses between each costume. These pauses are necessary to make the timing on the animation look right.

So that means in step 1, we pause for about 1/3 of a second to do a few costume changes.

Meanwhile, we can't move (for that 1/3) of a second.

Instead, we can have 2 scripts:

Script 1
  1. point toward bunny
  2. move 1 step
  3. maybe put a short wait if it's moving too fast

Script 2
  1. follow (animation runs)

Then Script 1 can move without being paused by whatever waits you need for the animation loop. It ends up looking like:

when @greenFlag clicked
forever
point towards [bunny v]
move (1) steps // If taz is moving too slow, move more than 1
... // if taz is moving too fast, put a short wait.
end

when @greenFlag clicked
forever
follow::custom
end

define follow
if <<(direction) \< [-45]> and <(direction) \> [-135]>> then
switch costume to [taz left walk v]
wait (.1) secs
switch costume to [taz left walk2 v]
wait (.1) secs
switch costume to [taz left walk3 v]
wait (.1) secs
switch costume to [taz left walk4 v]
wait (.1) secs // you're missing one wait on each animation loop
end
if <<(direction) \> [-45]> and <(direction) \< [45]>> then
switch costume to [taz up walk v]
wait (.1) secs
switch costume to [taz up walk2 v]
wait (.1) secs
switch costume to [taz up walk3 v]
wait (.1) secs
switch costume to [taz up walk4 v]
wait (.1) secs // you're missing one wait on each animation loop
end
if <<(direction) \< [135]> and <(direction) \> [45]>> then
switch costume to [taz right walk v]
wait (.1) secs
switch costume to [taz right walk2 v]
wait (.1) secs
switch costume to [taz right walk3 v]
wait (.1) secs
switch costume to [taz down walk4 v]
wait (.1) secs // you're missing one wait on each animation loop
end
if <<(direction) \> [135]> or <(direction) \< [-135]>> then
switch costume to [taz down walk v]
wait (.1) secs
switch costume to [taz down walk2 v]
wait (.1) secs
switch costume to [taz down walk3 v]
wait (.1) secs
switch costume to [taz down walk4 v]
wait (.1) secs // you're missing one wait on each animation loop
end

Happy to answer any questions,

– RT_Borg
icon96 icon96 loading
Thanks a lot for the thorough explanation RT_Borg.
ProtoBIT_09 ProtoBIT_09 loading
I'm trying to make a platformer and I don't know how to make the player switch to a different costume based on direction
SpyCoderX SpyCoderX loading

ProtoBIT_09 wrote:

I'm trying to make a platformer and I don't know how to make the player switch to a different costume based on direction
Please make a new topic. Thanks!
You should make a block of skip sounds to a different one if you're in Pennsylvania hope the solar eclipse goes well for you to