Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Walk cycle code
- ivy-glow
-
Scratcher
5 posts
Walk cycle code
Hello! I'm having trouble coding walk cycle's. I've made all the costumes. Walk1, Walk2, Walk3 and stand. How can I make the player walk when the right arrow key, or the left arrow key is pressed?
- zablonb2424
-
Scratcher
93 posts
Walk cycle code
when green flag clicked
set rotation style [side-to-side v]
forever
if <<key [right arrow v] pressed?> or <key [left arrow v] pressed?>> then
if <key [left arrow v] pressed?> then
point in direction (-90 v)
switch costume to [Walk v]
repeat (2)
wait (0.05) secs
next costume
end
end
if <key [right arrow v] pressed?> then
point in direction (90 v)
switch costume to [Walk v]
repeat (2)
wait (0.05) secs
next costume
end
end
else
switch costume to [Stand v]
end
end
Last edited by zablonb2424 (July 31, 2024 22:24:51)
- GoldenApple1000
-
Scratcher
94 posts
Walk cycle code
https://scratch.mit.edu/projects/1051507367/ check the code.
- invalidaccess
-
Scratcher
100+ posts
Walk cycle code
create a variable called “frame” and change frame by 1 everytime you move
then switch costume just like this:
then switch costume just like this:
when green flag clicked
forever
if <key [ move] pressed?> then
change [ frame] by (1)
end
switch costume to [ ((costume start) + ((frame) mod (costume count)))]
end
- invalidaccess
-
Scratcher
100+ posts
Walk cycle code
define switch costume to((costume start) + (([ floor] of (frame)) mod (costume count)))
- ivy-glow
-
Scratcher
5 posts
Walk cycle code
It seems you added a Costume start variable, and a Costume count variable. Do I need to set those to anything?
- SpyCoderX
-
Scratcher
1000+ posts
Walk cycle code
Actually, this would probably be better:switch costume to((costume start) + (([ floor] of (frame)) mod (costume count)))
when gf clicked
delete all of [walking v] ::list //[For this sprite only]
add [Walk1] to [walking v] //These must be EXACTLY like the names of the costumes you want to be part of the walking animation.
add [Walk2] to [walking v]
add [Walk3] to [walking v]
set [isWalking v] to (0) //[For this sprite only]
set [frame v] to (0) //[For this sprite only]
forever
set [isWalking v] to ((1)*<<key [a v] pressed?> or <key [d v] pressed?>>)
if <(isWalking) = (1)> then
switch costume to (item ((([floor v] of (frame)) mod (length of [walking v]))+(1)) of [walking v])
else
switch costume to [standing v]
end
change [frame v] by (0.1)
end
Hope this helps!
Last edited by SpyCoderX (Aug. 1, 2024 01:38:00)
- ivy-glow
-
Scratcher
5 posts
Walk cycle code
Thank you so much! Do you mind explaining how the code works?
Last edited by ivy-glow (Aug. 1, 2024 15:28:49)
- Koamodo975
-
Scratcher
1000+ posts
Walk cycle code
Thank you so much! Do you mind explaining how the code works?Somebody correct me if I'm wrong but I'm 99% sure this is how it goes:
If IsWalking = 1 (since if a/d is pressed it registers as one in math), it switches the costume to the corresponding item. The floor of a number is the greatest possible whole number less than or equal to the number you're calculating; hence the floor of 6.5 would be 6. Since frame goes up by 0.1, this adds a pause to the animation frames so you can see the anims clearly: the floor of frame would be zero until it reaches 1. () mod () gives you the remainder of the first number divided by the second; i.e., 13 mod 6 = 1. If the dividend is less than the divisor, the number you get is automatically the dividend. Looking at the code as is, frame would equal zero, so floor of frame(0) mod length of walking(3) would equal 0. 0 + 1 is 1, so it would set the costume to costume 1. When floor of frame goes up to 1, (1 mod 3) + 1 equals 2. (2 mod 3) + 1 is three, and (3 mod 3) + 1 is 1. The cycle repeats with 4,5,6 and so on. And lastly, if IsWalking doesn't equal 1, then the standing costume/animation plays.
Last edited by Koamodo975 (Aug. 1, 2024 16:38:34)
- SpyCoderX
-
Scratcher
1000+ posts
Walk cycle code
Yup. That’s basically it.Thank you so much! Do you mind explaining how the code works?Somebody correct me if I'm wrong but I'm 99% sure this is how it goes:
If IsWalking = 1 (since if a/d is pressed it registers as one in math), it switches the costume to the corresponding item. The floor of a number is the greatest possible whole number less than or equal to the number you're calculating; hence the floor of 6.5 would be 6. Since frame goes up by 0.1, this adds a pause to the animation frames so you can see the anims clearly: the floor of frame would be zero until it reaches 1. () mod () gives you the remainder of the first number divided by the second; i.e., 13 mod 6 = 1. If the dividend is less than the divisor, the number you get is automatically the dividend. Looking at the code as is, frame would equal zero, so floor of frame(0) mod length of walking(3) would equal 0. 0 + 1 is 1, so it would set the costume to costume 1. When floor of frame goes up to 1, (1 mod 3) + 1 equals 2. (2 mod 3) + 1 is three, and (3 mod 3) + 1 is 1. The cycle repeats with 4,5,6 and so on. And lastly, if IsWalking doesn't equal 1, then the standing costume/animation plays.
The one thing I would mention is this:
We aren’t using a costume number, rather, we are using a costume name. We use the item () of block to get the name of that frame of the walking animation.
This has the benefit of allowing us to specify the walking costumes by name rather than number. It also means we can have as many walking costumes as we want.

- Discussion Forums
- » Help with Scripts
-
» Walk cycle code