# Making a Character move with the mouse

**URL:** <https://forum.code.org/t/making-a-character-move-with-the-mouse/23408>\
**Category:** CS Discoveries\
**Created:** [December 12, 2018, 7:27pm UTC](https://forum.code.org/t/making-a-character-move-with-the-mouse/23408 "2018-12-12T19:27:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsaylor](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/lsaylor/32/2149_2.png) [@lsaylor](https://forum.code.org/u/lsaylor)\
**Post date:** [December 12, 2018, 7:27pm UTC](https://forum.code.org/t/making-a-character-move-with-the-mouse/23408/1 "2018-12-12T19:27:38Z")

</div>

I have a student that wants to connect the mouse to her Hero Sprite instead of using the counter pattern. She made it work, but she can’t figure out how to make his legs move and have him face a different direction when connected to the mouse. Is this possible, and if so how?

---

<div class="post-metadata">

**Author:** ![edavis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/edavis/32/4927_2.png) [@edavis](https://forum.code.org/u/edavis)\
**Post date:** [December 12, 2018, 9:44pm UTC](https://forum.code.org/t/making-a-character-move-with-the-mouse/23408/2 "2018-12-12T21:44:01Z")

</div>

Could you post the code?

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mike/32/5229_2.png) [@mike](https://forum.code.org/u/mike)\
**Post date:** [December 12, 2018, 10:05pm UTC](https://forum.code.org/t/making-a-character-move-with-the-mouse/23408/3 "2018-12-12T22:05:11Z")

</div>

If I’m understanding correctly, it sounds like she wants the sprite to change directions automatically based on the direction of movement. One way of doing this would be to create a variable to track the sprite’s previous x position and use conditionals to continuously check if the x position is increasing and decreasing. Here’s an example built onto on the bubbles from lesson 13:

> **[Check out what I made](https://studio.code.org/projects/gamelab/4C0a1IpME6QdQSj7EZie8dUIAbSA73GOypMDzZ4KZno/view)**
>
> I wrote the code myself with Code.org

  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![lsaylor](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/lsaylor/32/2149_2.png) [@lsaylor](https://forum.code.org/u/lsaylor)\
**Post date:** [December 12, 2018, 10:31pm UTC](https://forum.code.org/t/making-a-character-move-with-the-mouse/23408/4 "2018-12-12T22:31:53Z")

</div>

that helps alot!! She will appreciate that! In addition, when the character moves in the draw loop using the counter pattern or velocity is uses both of the small animations in the animation tab, so the legs look like it is constantly moving. Is there a way to keep that movement when the sprite is “attached” to the mouse?

Lindsey Saylor  
Edna Hill Middle School  
7th/ 8th Grade Computer Science
