# Making a sprite (mc) face the direction it is moving

**URL:** https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479
**Category:** flash
**Created:** [August 18, 2004, 9:34pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479 "2004-08-18T21:34:48Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Calydon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/calydon/32/682_2.png) [@Calydon](https://forum.kirupa.com/u/Calydon)
#### Post date: [August 18, 2004, 9:34pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/1 "2004-08-18T21:34:48Z")

</div>

Enjoy the fish:

[[color=#0000ff]http://www.tankbangers.com/LAYOUT1.swf[/color]](http://www.tankbangers.com/LAYOUT1.swf)

peruse the FLA:

[[color=#0000ff]http://www.tankbangers.com/LAYOUT1.fla[/color]](http://www.tankbangers.com/LAYOUT1.fla)

Notice how the fish flee from an advancing cursor (much like real fish). I have tried using atan2 and other tricks to get the fish to face AWAY from the pursuing mouse cursor, but what happens is that when the fish are moving from \_Xpositive to \_Xnegative (right to left) they appear to be swimming upside down. I have abandoned atan2 for the time being. Is there a way to get a movie clip to reflect along the y axis depending on which side of the mouse cursor it is on? That might be one solution.

Here is something else that would earn partial credit:  
If you can corner one of the fish you will notice that when you click on it, you can cause it some distress. (meanie!) I would like to get the fish to ‘gulp’ some bubbles, like in this clip,  
[[color=#0000ff]http://www.tankbangers.com/LAYOUT1b.swf[/color]](http://www.tankbangers.com/LAYOUT1b.swf)  
[[color=#0000ff]http://www.tankbangers.com/LAYOUT1b.fla[/color]](http://www.tankbangers.com/LAYOUT1b.fla)  
but without destroying the random movement and cursor avoidance behaviour of the first clip. Can you figure it out?! cuz I can’t

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 18, 2004, 9:50pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/2 "2004-08-18T21:50:35Z")

</div>

lmao thats good i like that!! lol ill hav a look! 😃

Prophet.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 18, 2004, 10:21pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/3 "2004-08-18T22:21:56Z")

</div>

do you want them to face away in 360 degs or just face away as in left or right?

```auto
if(xdiff>0){
	var dir=-100
}
else{
	var dir=100
}
this._xscale = dir

```

in your avoid mouse prototype will do the 2 way thing…

ill give the 360 deg thing more thought…

Prophet.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 18, 2004, 10:45pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/4 "2004-08-18T22:45:33Z")

</div>

ok - i’ll give what you’ve written here so far a run-through when i get home.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 19, 2004, 8:31am UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/5 "2004-08-19T08:31:02Z")

</div>

hehe - thanks Prophet - that looks pretty cool now. I wish I had a little more money so I could hire someone like you 😕  
I extended the constraining box so that the fish have a fighting chance, and now, to try and make them draggable and maybe add a little score tag in the corner (10 pts every time you manage to ‘grab’ a fish)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 19, 2004, 10:16am UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/6 "2004-08-19T10:16:47Z")

</div>

I had to do a similar thing for a game I’m working on.

for the most part, it was the same as the snippet prophet posted, but to get the angle working as well, I had to use Math.atan2 at the same time.

putting both methods together should sort you out. It will require some testing/tweaking though.

I’d have a look through your code, but I’m at work atm, so I can’t really be much more help.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 19, 2004, 4:14pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/7 "2004-08-19T16:14:17Z")

</div>

interesting- ok, i’ll do some more experimenting tonight

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 19, 2004, 6:02pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/8 "2004-08-19T18:02:37Z")

</div>

hi. I’ve figured out how to do it. here it is:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 8:20pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/9 "2004-08-20T20:20:00Z")

</div>

thanks, cyberathlete, but I need the clip to avoid the mouse, not follow it. There are quite a few Scottish flashers on this forum - coincidence? 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 10:37pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/10 "2004-08-20T22:37:37Z")

</div>

It’s no coincidence, all scots are pro. 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 10:39pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/11 "2004-08-20T22:39:01Z")

</div>

I amn’t sure how to actionscript code to make something avoid the mouse. if someone tells me how, i can easily edit the code to include this.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 10:52pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/12 "2004-08-20T22:52:48Z")

</div>

ah, ok i got the fla off your site. i’m looking at it now.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 11:23pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/13 "2004-08-20T23:23:26Z")

</div>

hmmm… i just cut an pasted the code from cyberathletes into calydons…

dont look too pretty… just looks wrong… maybe i have an idea though! 😉

Prophet.

EDIT:- LOL how about adding

```auto
		if(dir>0){
			this._rotation = ydiff*-0.06
		}
		else{
			this._rotation = ydiff*0.06
		}

```

just beneath the code i suggested earlier!! lol pretty neat…

watu think??

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 20, 2004, 11:27pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/14 "2004-08-20T23:27:31Z")

</div>

yeah i tried cutting and copying too. it went doolally.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2004, 3:08am UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/15 "2004-08-27T03:08:20Z")

</div>

Been too busy to reply to this promptly. Just wanted to thank Prophet and Cyberathlete for their help - as a gift to the community I’m making the FLA available for download if you want to try something similar.

[http://www.tankbangers.com/LAYOUT\_init2b.fla](http://www.tankbangers.com/LAYOUT_init2b.fla)

Oh, and Prophet - I thought your angle effect was a bit too subtle so I changed the ydiff\*-0.06 to ydiff\*-0.15. Thanks again!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2004, 10:31pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/16 "2004-08-27T22:31:27Z")

</div>

cool!  
dont thank me, it was a lot of fun2do!!! 😃

Prophet.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2004, 11:23pm UTC](https://forum.kirupa.com/t/making-a-sprite-mc-face-the-direction-it-is-moving/61479/17 "2004-08-27T23:23:00Z")

</div>

glad you got it resolved 🙂
