# Animation not working in this code

**URL:** <https://forum.makecode.com/t/animation-not-working-in-this-code/15506>\
**Category:** Help\
**Tags:** extension\
**Created:** [September 22, 2022, 7:19pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506 "2022-09-22T19:19:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaturn68](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jaturn68/32/2180_2.png) [@jaturn68](https://forum.makecode.com/u/jaturn68)\
**Post date:** [September 22, 2022, 7:19pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506/1 "2022-09-22T19:19:25Z")

</div>

Hi,

This game is a work in progress; I am trying to figure out why the animation is not working… the players legs are supposed to move when walking. Any help would be greatly appreciated.

> **[student code animation not working](https://arcade.makecode.com/51228-06847-05959-09738)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [October 9, 2023, 9:36pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506/2 "2023-10-09T21:36:47Z")

</div>

Nice game idea and graphics, @jaturn68!

I am no expert in this field, but trying to get better coding in MakeCode Arcade, especially since pupils keep asking about similar challenges with their own creations, which I have a hard time figuring out within the average span of a modern kid’s attention… 😉

Handling movements in multiple directions, with jumps and weapons, and with animations, is a task that generally requires careful attention to details and keeping track of a quickly growing number of states, among other things and quirks of the MakeCode Arcade game engine, I am starting to learn.

In this particular game, I have found three common issues which also are recurring among pupils:

1. The [most common issue with animations not working, is typically not letting the animation play out uninterrupted](https://forum.makecode.com/t/sprite-not-animating-when-moving/22629), which has to do with handling states and keeping them going in parallel without being interrupted. @richard has made a nice [character animation extension](https://github.com/microsoft/arcade-character-animations), which has a number of common [predicates](https://berkeley-cs61as.github.io/textbook/conditional-expressions-and-predicates.html#:~:text=Predicate%20and%20Style) predefined, (is)_moving right_, (is)_facing left_ etc., to help one keep track of such states, which I can recommend using.  
Specifically, in the game link you presented here, the walking animation is interrupted on every game loop by setting a new sprite image and because the animation is started anew before it got the time to progress to the next image.  
 ![Skjermbilde](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/15b1b2571e7847b22b4e59af434849604a6c1974.jpeg)

2. The next challenge that I struggled a bit with here, after attempting to keep track of states manually and using the [character animation extension](https://github.com/microsoft/arcade-character-animations), was that the various sprite images used did not have the same dimensions, thus also triggering a movement(and predicate/state change) every time a sprite image was changed, since the sprite (x,y) position is determined by the sprite’s upper left corner pixel. This can be solved, for instance like I chose to do here, by making all _link_ character sprites the same dimension, 15x17, or - like I did in my example for the sprite image while lying down flat in the air- by not using any condition/predicate that has to do with horizontal left or right movement in the main game loop’s _if_-statements.

3. Another small challenge was that I did not get the _facing right_ or _facing left_ predicates to work correctly even with same-sized sprite images which did not trigger movement when changing images. I then had to make my own state variable, _faceLeft_, which changes with the _left_ and _right_ buttons, respectively.

4. The last challenge was that any sprite image that was not 16x16 dimension, exhibited uneven motion horizontally for every vx speed which were not a factor of 50, for some strange reason that I have addressed in its own topic(link will be provided later). That I solved by setting vx to 100, but could also be fixed by setting all sprites to 16x16.

Here is my attempt at fixing various challenges with animation etc. in the code provided by @jaturn68 above:

> **[student code animation working ok](https://arcade.makecode.com/10171-43568-86788-13267)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

In addition to the “fixes” above, I also added _double-jumb_ logic to be able accomplish the long and high jumps in the tilemap, rewrote the sword handling with 2 sprites instead of just on, [inspired by this nice example](https://forum.makecode.com/t/enemies-not-dying/22827) and implemented movement and animation in both left and right directions, to highlight the logic needed for all this in the code.

Because I am a noobie at this myself, I find it is harder to modify other’s code than designing my own from scratch and I did not have unlimited time available for this, my resulting code is by no means especially good, neither in handling nor readability.

But, judging by how hard it was(at least for me) also to search for really good examples of well written code with fluent movement and animations _in many direction and with many actions_(jumps, weapons etc.) on this forum and in the documentation(just small examples), and by how frequently similar questions arise, **it would be great if @richard / the @MakeCode could provide the forum with one such elaborate _best practice_ example for everyone to learn from, like @richard [recently did perfectly for hitboxes](https://forum.makecode.com/t/hitbox/22993/7).**

Here are some examples I did manage to find, with really great movements and animations, some complexity and fairly readable/organized code:

1. [https://makecode.com/\_1mAeq2Rzb7rd](https://makecode.com/_1mAeq2Rzb7rd) (This is just a repost of [this nice game](https://forum.makecode.com/t/sonic-micro-adventure/9934/1) @NotOnefinity with only the blocks pertaining to movement and animation expanded. The game uses the character animation extension and the code is really well organized.)

2. [https://forum.makecode.com/t/castle-crawler/1343](https://forum.makecode.com/t/castle-crawler/1343) (this is the official forum link to @reyhanPanci256’s nice _Castle crawlder_, which is also featured on the [arcade.makecode.com](http://arcade.makecode.com) frontpage in the Community games section. Here all the states and animations are manually programmed without the character animations extensions. Adding, for instance, horizontal movements and club strikes, would complicate the code with manyfold additional state combinations

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [October 9, 2023, 10:03pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506/3 "2023-10-09T22:03:26Z")

</div>

Here’s the updated link to the topic about sprite size vs smooth movements:

> [@Sprites other than 16x16 move unsmoothly?](https://forum.makecode.com/t/sprites-other-than-16x16-move-unsmoothly/23181):
>
> To get some better practice with MakeCode Arcade sprite movements and animations, I have recently been playing around with the [following help request from](https://forum.makecode.com/t/animation-not-working-in-this-code/15506) @jaturn68. What I might have discovered(at least on my computers and browsers), and which baffled me, is that using the regular controller.moveSprite(vx, vy) function on a 16x16 sprite gives silky smooth movement on the x axis with any sprite image and at arbitrary vx speeds, while movement gets choppy for irregularly or different sized sprit…

Perhaps this very thread could work as _the_ best practice example, where @richard / the @makecode team / someone on this forum enhances/optimizes @jaturn68’s code to work fluently with animations in all directions and for all actions, and rewrite the code to be:

- both _efficient and more readable_,
- with _silky smooth movements and animations_, and
- immune to _hang-ups and strange behavior_ resulting from hammering many buttons at the same time?

---

<div class="post-metadata">

**Author:** ![Josef](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/josef/32/12457_2.png) [@Josef](https://forum.makecode.com/u/Josef)\
**Post date:** [March 13, 2024, 6:16pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506/4 "2024-03-13T18:16:07Z")

</div>

The pfps 🤣 🤣 🤣

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [March 15, 2024, 6:12pm UTC](https://forum.makecode.com/t/animation-not-working-in-this-code/15506/5 "2024-03-15T18:12:06Z")

</div>

Tri-ambiguous!
