# Motion path in Actionscript

**URL:** <https://forum.kirupa.com/t/motion-path-in-actionscript/29736>\
**Category:** flash\
**Created:** [August 29, 2003, 11:50pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736 "2003-08-29T23:50:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Digigamer](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digigamer/32/276_2.png) [@Digigamer](https://forum.kirupa.com/u/Digigamer)\
**Post date:** [August 29, 2003, 11:50pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/1 "2003-08-29T23:50:33Z")

</div>

Alright I’m trying to make a circular motion path using Senocular’s tutorial [here](http://proto.layer51.com/d.aspx?f=952).

What I want is a box to move around a circle motion path that is created in AS when the user presses a button. However I’m having trouble decifering all of Sencular’s code because there is a lot of advanced stuff that he puts in that I don’t want and I’m having trouble getting started. If anybody could set me down the right path I would be very grateful.

---

<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 30, 2003, 1:51am UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/2 "2003-08-30T01:51:30Z")

</div>

um…do you want a walkthrough of the whole tutorial or is there one part in partular you dont get. The math isn’t bad and once you get that its pretty straightforward

---

<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 30, 2003, 2:10am UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/3 "2003-08-30T02:10:39Z")

</div>

Check out the [[U]Circular Movement[/U]](http://www.kirupa.com/developer/mx/circular.htm) tutorial, it might be easier for you. 🙂

---

<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 30, 2003, 4:56am UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/4 "2003-08-30T04:56:52Z")

</div>

Kode- I don’t think that would really work for what I’m doing since I want it to move a certain direction depending on what key is pressed. And with that code you can really minipulate it so that it will find what place it is its route around the circle and move the other way if the user pressed the key to move it in the other direction. I don’t know maybe you could get it to work but I think Senocular’s method would be more reasonable for what I’m trying to do.

---

<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 30, 2003, 4:58am UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/5 "2003-08-30T04:58:17Z")

</div>

grandsp5- the main part I don’t get is the part with all the prototypes. I’m not sure what those do. I understand the other part of the code but what is it he is doing with all the prototypes.

---

<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 30, 2003, 11:09am UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/6 "2003-08-30T11:09:27Z")

</div>

so what is it you are trying to do exactly?

---

<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 30, 2003, 3:46pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/7 "2003-08-30T15:46:13Z")

</div>

I want to do something similar to your race car game except the track is a perfect circle and the car (or movieclip) moves along it to the left or the right depending on whether I push the left or right button on my keyboard.

Oh yes, and I don’t need the easing thing you have where you let go of the button and it keeps moving a little bit.

---

<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 30, 2003, 3:58pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/8 "2003-08-30T15:58:01Z")

</div>

[AS]var radius = 80;  
var radians = 0;  
var x = 100;  
var y = 100;  
this.createEmptyMovieClip(“car”, 0);  
car.lineStyle(5, 0xCC0000, 100);  
car.moveTo(0, -5);  
car.lineTo(0, 5);  
car.onEnterFrame = function() {  
radians += (Key.isDown(39)-Key.isDown(37))\*.1;  
this.\_x = Math.cos(radians)\*radius+x;  
this.\_y = Math.sin(radians)_radius+y;  
this.\_rotation = radians_180/Math.PI;  
};[/AS]  
Like that?

---

<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 30, 2003, 4:23pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/9 "2003-08-30T16:23:57Z")

</div>

Ahhh…thanks that is exactly what I wanted. Even though you didn’t use the motion path method I’m sure this is probably better.

Now I just have to get it edited to fit my game…thanks.

---

<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 30, 2003, 4:25pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/10 "2003-08-30T16:25:57Z")

</div>

No problem, Digigamer.

I told you it would be easier… 😉

---

<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 30, 2003, 8:00pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/11 "2003-08-30T20:00:06Z")

</div>

hmm can you please explain how these three lines work?

[AS]  
radians += (Key.isDown(39)-Key.isDown(37))\*.1;  
this.\_x = Math.cos(radians)\*radius+x;  
this.\_y = Math.sin(radians)\*radius+y;  
[/AS]

mainly just the first line and the +x and +y part on the end of the second two lines.

Thanks

---

<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 30, 2003, 8:15pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/12 "2003-08-30T20:15:20Z")

</div>

These lines are explained in the tutorial (see link above).  
[AS]this.\_x = Math.cos(radians)\*radius+x;  
this.\_y = Math.sin(radians)_radius+y;[/AS]  
As for this line…  
[AS]radians += (Key.isDown(39)-Key.isDown(37))_.1;[/AS]  
The Key.isDown method returns true if the specified key is pressed, false if not; which in numbers would be 1 or 0. That is:  
[AS]// if the right arrow is pressed  
1-0 = 1;  
// if the left arrow is pressed  
0-1 = -1;  
// if both arrows are pressed  
1-1 = 0;  
// this number multiplied by .1 and we get .1, -.1, or 0.[/AS]  
Bah… I suck, I can’t explain it. Could you help me here, senocular? :beam:

---

<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 30, 2003, 8:50pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/13 "2003-08-30T20:50:32Z")

</div>

ok it makes more sense now. And I knew those lines where in the tutorial but they didn’t have the +x or +y on the end so I wasn’t sure what that was doing.

Also I want another line to be in the movieclip so I made another movie clip and dublicated the code and changed it to the new movie clip’s instance name but I want this one to move on “a” and “d” instead and I tried tracing getAscii() but when I plugged in the numbers it returned it didn’t work and it still moved when I pressed the left and right buttons. Here is the code:

[AS]var radius = 225;  
var radians = 0;  
var x = 225;  
var y = 225;  
var redxPos = redPaddle.\_x;  
var redyPos = redPaddle.\_y = 225;  
bluePaddle.onEnterFrame = function() {  
radians += (Key.isDown(39)-Key.isDown(37))\*.1;  
bluePaddle.\_x = Math.cos(radians)_radius+x;  
bluePaddle.\_y = Math.sin(radians)radius+y;  
this.\_rotation = radians180/Math.PI;  
};  
redPaddle.onEnterFrame = function() {  
radians += (Key.isDown(97)-Key.isDown(100))_.1;  
redPaddle.\_x = Math.cos(radians)\*radius+x;  
redPaddle.\_y = Math.sin(radians)_radius+y;  
this.\_rotation = radians_180/Math.PI;  
};  
[/AS]

As you can see I’m trying to put the red one on the opposite side of the circle.

---

<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 30, 2003, 9:07pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/14 "2003-08-30T21:07:17Z")

</div>

You need to define a variable for each object. 😉  
[AS]var radius = 225;  
var x = 225;  
var y = 225;  
this.createEmptyMovieClip(“blue”, 0);  
blue.lineStyle(5, 0x0000CC, 100);  
blue.moveTo(0, -5);  
blue.lineTo(0, 5);  
blue.radians = 0;  
blue.onEnterFrame = function() {  
this.radians += (Key.isDown(39)-Key.isDown(37))\*.1;  
this.\_x = Math.cos(this.radians)_radius+x;  
this.\_y = Math.sin(this.radians)radius+y;  
this.\_rotation = this.radians180/Math.PI;  
};  
this.createEmptyMovieClip(“red”, 1);  
red.lineStyle(5, 0xCC0000, 100);  
red.moveTo(0, -5);  
red.lineTo(0, 5);  
red.radians = Math.PI;  
red.onEnterFrame = function() {  
this.radians += (Key.isDown(68)-Key.isDown(65))_.1;  
this.\_x = Math.cos(this.radians)\*radius+x;  
this.\_y = Math.sin(this.radians)_radius+y;  
this.\_rotation = this.radians_180/Math.PI;  
};[/AS]

---

<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 30, 2003, 9:28pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/15 "2003-08-30T21:28:04Z")

</div>

thanks man. Your my new god. Btw, what was I doing wrong with the key thing. how did you come up with 68 and 65 when I got like 100 and 97?

---

<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 30, 2003, 9:33pm UTC](https://forum.kirupa.com/t/motion-path-in-actionscript/29736/16 "2003-08-30T21:33:50Z")

</div>

You’re welcome. 😉

And you can see the key code values in the Flash help files, but I usually throw this code in the movie, and press the key to see the code value:  
[AS]this.onKeyDown = function() {  
trace(Key.getCode());  
};  
Key.addListener(this);[/AS]
