# Distance Travelled

**URL:** <https://forum.kirupa.com/t/distance-travelled/260709>\
**Category:** flash\
**Created:** [May 19, 2008, 5:26am UTC](https://forum.kirupa.com/t/distance-travelled/260709 "2008-05-19T05:26:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikrant29nov](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@vikrant29nov](https://forum.kirupa.com/u/vikrant29nov)\
**Post date:** [May 19, 2008, 5:26am UTC](https://forum.kirupa.com/t/distance-travelled/260709/1 "2008-05-19T05:26:06Z")

</div>

My car is moving on a maze.

The movement of car is through Arrow keys.

\*\*I have to calculate KM. \*\*i.e., the distance travelled.

Current frame rate is 12 and 1km =40px

Please help.

Here is the script

onClipEvent (enterFrame) {

```
    with (_root.car) {
        if (Key.isDown(40)) {
            _y = _y+3;
         
        }
        // end if               
        if (Key.isDown(38)) {
            _y = _y-3;
          
        }
        // end if               
        if (Key.isDown(37)) {
            _x = _x-3;
          
        }
        // end if               
        if (Key.isDown(39)) {
            _x = _x+3;
         
        }
        // end if               
        // end if 
        if (walls.hitTest(getBounds(_root).xMax, _y, true)) {
            _x = _x-3;
        }
        // end if               
        if (walls.hitTest(getBounds(_root).xMin, _y, true)) {
            _x = _x+3;
        }
        // end if               
        if (walls.hitTest(_x, getBounds(_root).yMax, true)) {
            _y = _y-3;
        }
        // end if               
        if (walls.hitTest(_x, getBounds(_root).yMin, true)) {
            _y = _y+3;
        }
        // end if               
        if (_root.goal.hitTest(_x, getBounds(_root).yMax, true)) {
            _root.gotoAndStop(3);
        }
        // end if               
    }
    // End of with

```

}

---

<div class="post-metadata">

**Author:** ![vikrant29nov](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@vikrant29nov](https://forum.kirupa.com/u/vikrant29nov)\
**Post date:** [May 20, 2008, 6:11am UTC](https://forum.kirupa.com/t/distance-travelled/260709/2 "2008-05-20T06:11:44Z")

</div>

Can any one help me…

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [May 20, 2008, 9:59am UTC](https://forum.kirupa.com/t/distance-travelled/260709/3 "2008-05-20T09:59:47Z")

</div>

search the forums bag head:P

pythagorus is your friend

---

<div class="post-metadata">

**Author:** ![ShadowViper](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@ShadowViper](https://forum.kirupa.com/u/ShadowViper)\
**Post date:** [May 20, 2008, 3:59pm UTC](https://forum.kirupa.com/t/distance-travelled/260709/4 "2008-05-20T15:59:57Z")

</div>

Here’s a hint, the distance traveled each frame is equal to:

```auto
xDif = Math.abs(old_x_position-this._x);
yDif = Math.abs(old_y_position-this._y);
distance = Math.sqrt((xDif*xDif)+(yDif*yDif))

```

---

<div class="post-metadata">

**Author:** ![vikrant29nov](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@vikrant29nov](https://forum.kirupa.com/u/vikrant29nov)\
**Post date:** [May 24, 2008, 4:31pm UTC](https://forum.kirupa.com/t/distance-travelled/260709/5 "2008-05-24T16:31:46Z")

</div>

Thanks for the hint.

---

<div class="post-metadata">

**Author:** ![vikrant29nov](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@vikrant29nov](https://forum.kirupa.com/u/vikrant29nov)\
**Post date:** [June 2, 2008, 8:52am UTC](https://forum.kirupa.com/t/distance-travelled/260709/6 "2008-06-02T08:52:03Z")

</div>

[quote=ShadowViper;2328688]Here’s a hint, the distance traveled each frame is equal to:

```auto
xDif = Math.abs(old_x_position-this._x);
yDif = Math.abs(old_y_position-this._y);
distance = Math.sqrt((xDif*xDif)+(yDif*yDif))

```

[/quote]

Can you give me the complete code.

---

<div class="post-metadata">

**Author:** ![ShadowViper](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@ShadowViper](https://forum.kirupa.com/u/ShadowViper)\
**Post date:** [June 2, 2008, 2:56pm UTC](https://forum.kirupa.com/t/distance-travelled/260709/7 "2008-06-02T14:56:13Z")

</div>

I realize that the forums are a place to ask for help, but they aren’t a place for people to make your game for you. Look up some source code examples on the net, search the forums and experiment. I learned this way and I feel relatively comfortable with my grasp of Flash. If you really want to learn how to use Flash, you’ll be able to do it this way. If all you want is for us to make your game, then sorry I think your out of luck.

Not trying to be too harsh, but the best way to learn is through doing. IF you do come here and show that you have taken what I gave you and tried to apply it, I’ll be happy to help you fix problems and work out bugs.

~Cody

---

<div class="post-metadata">

**Author:** ![vikrant29nov](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@vikrant29nov](https://forum.kirupa.com/u/vikrant29nov)\
**Post date:** [June 3, 2008, 3:54am UTC](https://forum.kirupa.com/t/distance-travelled/260709/8 "2008-06-03T03:54:50Z")

</div>

I apologize for not showing the code and simply asking for help.

Here is the code:

> onClipEvent (enterFrame) {  
> with (\_root.car) {  
> xDif = Math.abs(old\_x\_position-this.\_x);  
> yDif = Math.abs(old\_y\_position-this.\_y);  
> distance = Math.sqrt((xDif_xDif)+(yDif_yDif));  
> if (Key.isDown(40)) {  
> \_y = \_y+3;  
> trace(\_root.i);  
> \_root.srt.\_visible = false;  
> }  
> // end if  
> if (Key.isDown(38)) {  
> \_y = \_y-3;  
> }  
> // end if  
> if (Key.isDown(37)) {  
> \_x = \_x-3;  
> }  
> // end if  
> if (Key.isDown(39)) {  
> \_x = \_x+3;  
> }  
> // end if  
> }  
> }
