# Help understanding code

**URL:** <https://forum.kirupa.com/t/help-understanding-code/2739>\
**Category:** Uncategorized\
**Created:** [April 20, 2002, 11:59pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739 "2002-04-20T23:59:47Z")\
**Posts on this page:** 13\
**Page:** 1

<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:** [April 20, 2002, 11:59pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/1 "2002-04-20T23:59:47Z")

</div>

um… im trying to learn actionscript a little better… and i want to know how to make things catch up to a mouse… i have an fla with a thing that follows the path of the mouse and catches up… but i dont understand any of the code… \r\rhere is a layout of the flash…\r\rthere are 3 layers in scene 1… actions, an invisible cursor, and a movie clip… all of them are extended to frame 3… on frames 2 and 3 are the same code… except 3 has one more line… which is gotoAndPlay(1);… here is that code\r\rcount = Number(count)+1;\rif (Number(count) == 1) {\r&nbsp &nbsp &nbsp &nbsp old\_x = getProperty(“line44”, \_x);\r&nbsp &nbsp &nbsp &nbsp old\_y = getProperty(“line44”, \_y);\r} else {\r&nbsp &nbsp &nbsp &nbsp old\_x = getProperty ( “line” add (count-1), \_x );\r&nbsp &nbsp &nbsp &nbsp old\_y = getProperty ( “line” add (count-1), \_y );\r}\rx\_distance = CursorX-old\_x;\ry\_distance = CursorY-old\_y;\rx\_movement = x\_distance/2.5;\ry\_movement = y\_distance/2.5;\rduplicateMovieClip ("/line", “line” add count, count);\rsetProperty (“line” add count, \_x, Number(x\_movement)+Number(old\_x));\rsetProperty (“line” add count, \_y, Number(y\_movement)+Number(old\_y));\rif (Number(count)\>44) {\r&nbsp &nbsp &nbsp &nbsp count = 0;\r}\rgotoAndPlay (1);\r\rinside the cursor layer, there are 2 layers… an empty graphic i guess it is… and actions layers… the first frame is \r\rstartDrag ("", true);\r…/:CursorX = getProperty("", \_x);\r…/:CursorY = getProperty("", \_y);\r;\r\rand the second frame is \r\r…/:CursorX = getProperty("", \_x);\r…/:CursorY = getProperty("", \_y);\r\ri dont understand how any of it works… um… if you can help that would be great… i can email you the fla if you want… just let me know… but i REALLY need to learn this stuff… because i am going to use it in my final project for one of my classes, thanks\r\r-scott

---

<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:** [April 21, 2002, 12:56am UTC](https://forum.kirupa.com/t/help-understanding-code/2739/2 "2002-04-21T00:56:02Z")

</div>

you can mail me the fla at [jedick1@binghamton.edu](mailto:jedick1@binghamton.edu)

---

<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:** [April 21, 2002, 1:19am UTC](https://forum.kirupa.com/t/help-understanding-code/2739/3 "2002-04-21T01:19:33Z")

</div>

hi scott,\r\ri think that code is making a mountain of a molehill.\r\rhere’s a method which is a little cleaner.\r\rfirst we build an array that will contain the last however many (in this case 30)mouse positions, and set it up to update itself as the movie goes. this code goes on frame 1:

```auto
  \r\rpos = new Array();\r\rvar delay = 30;\r\rwhile(delay--){\r\r pos[delay] = {x:_xmouse,y:_ymouse};\r\r}\r\r_root.onEnterFrame = function(){\r\r pos.push({x:_xmouse,y:_ymouse});\r\r pos.shift();\r\r}

```

\rthis code goes on a movie which you want to follow the mouse:

```auto
  \r\ronClipEvent(enterFrame){\r\r _x = _root.pos[0].x;\r\r _y = _root.pos[0].y;\r\r}

```

\rthe movies have to be in \_root, or convert the coordinates to their parent.\r\r\* edit\*\rbeware, assigning the onEnterFrame is mx dependant code.

---

<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:** [April 21, 2002, 7:19pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/4 "2002-04-21T19:19:07Z")

</div>

um… i didnt write the code… i downloaded the fla… thats great that you found an easier way… but it would help if i knew what the commands did and how to use them… i wish someone would write a tutorial on stuff catching up to the mouse… heh… if you can break down what the commands do and how to use them that would be great…

---

<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:** [April 22, 2002, 5:32pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/5 "2002-04-22T17:32:21Z")

</div>

i never accused you of writing the code! ; )\r\ri think all you need do to understand this code, is to bone up on arrays. there’s \<a href=“http://“[http://pub40.ezboard.com/fkirupafrm24.showMessage?topicID=5.topic](http://pub40.ezboard.com/fkirupafrm24.showMessage?topicID=5.topic)””\>a moderately thorough explanation\</a\> in the “best of” forum.\r\rbasically, the while statement fills the array with the current mouse position. this only happens once to get things started.\r\rthen in the onEnterFrame action, the “push” method adds the current mouse position to the end of the array, while the “shift” method removes the first element of the array.\r\rthe code on the movie is setting its position based on the first element of the array.\r\rshort, sweet, and simple.

---

<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:** [April 23, 2002, 1:08am UTC](https://forum.kirupa.com/t/help-understanding-code/2739/6 "2002-04-23T01:08:55Z")

</div>

i know a bit in C++, and im very familiar with arrays but its the functions i dont know how to use… im not sure what parameters to give the Duplicate Movie Clip function, and i dont know how to use the push or shift functions… your explanation helped a little and i can kinda see how your code works… but im still a little lost… its not your fault though… thanks for helpin… i got an explanation of the code from someone else… that didnt help either… im just kind of a slow learner… i guess i dont know…\r\rso is this how it goes…? first i make a 2 dimensional array… (not sure how to do this but…) i take the mouse positions and put them in there… and then i assign them to a movie clip or object of some sort… ? if thats right how do i make the delay though…\r\r-scott

---

<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:** [April 23, 2002, 4:44pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/7 "2002-04-23T16:44:54Z")

</div>

yup, you’ve got it.\r\rmake an array with:\r\rmyArray = new Array();\r\rpopulate with as many of the current mouse position as you want a delay. i did that with a while loop which i’ll assume you’re also familiar with. also you might notice that the positions are stored as objects with an x and y property. that’s what the curly brackets are about.\r\rsince positions are added to the end (push) and referenced from the beginning, the length of the array is your delay. if you want less delay, you can either shorten the array, or reference a higher place in the array.\r\ryou can make a “snake” by referencing different points in the array. this code is an example of that:

```auto
  \r\rpos = new Array();\r\rvar delay = 30;\r\r\r\rj = delay;\r\rwhile(j--){\r\r_ _ _ _ pos[j] = {x:_xmouse,y:_ymouse};\r\r}\r\r_root.onEnterFrame = function(){\r\r_ _ _ _ pos.push({x:_xmouse,y:_ymouse});\r\r_ _ _ _ pos.shift();\r\r}\r\r\r\rj = delay/3;\r\rwhile(j--){\r\r_ _ _ _ _root.createEmptyMovieClip("f"+j,j);\r\r_ _ _ _ mc = _root["f"+j];\r\r_ _ _ _ mc.beginFill("0x00cccc");\r\r_ _ _ _ mc.lineTo(5,8);\r\r_ _ _ _ mc.lineTo(-5,8);\r\r_ _ _ _ mc.endFill();\r\r_ _ _ _ mc.place=j*3;\r\r_ _ _ _ mc.onEnterFrame = function(){\r\r_ _ _ _ _ _ _ _ this._x = _root.pos[this.place].x;\r\r_ _ _ _ _ _ _ _ this._y = _root.pos[this.place].y;\r\r }\r\r}

```

\rcut and paste that into an empty, single frame movie fla to see it in action (mx).\r\r\* edited to fix italics\*

---

<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:** [April 23, 2002, 10:11pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/8 "2002-04-23T22:11:29Z")

</div>

This last bit of code isn’t totally clear for me. Let’s see if I get this. \_root.onEnterFrame runs all the time in the \_root., and mc.onEnterFrame runs for each mc, is that it ?\rOh, and what about that lineTo thing ? I see what it does but you only draw 2 lines for a triangle…\r\rpom 0]

---

<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:** [April 23, 2002, 10:36pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/9 "2002-04-23T22:36:57Z")

</div>

ily,\r\rfirstly, that’s right, each clip has it’s own enterframe event. it could be managed all from one event in \_root, but this is just as easy.\r\rre: only two lines. when you call endFill() it closes the shape, so there’s no need to specify a lineTo() for your last line.

---

<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:** [April 24, 2002, 10:29am UTC](https://forum.kirupa.com/t/help-understanding-code/2739/10 "2002-04-24T10:29:18Z")

</div>

I’m not totally sure I had my answer, so here’s the question again : the \_root.onEnterFrame makes the \_root do something, while the mc.onEnterFrame makes the clip do something, right ?\r\rpom 0]

---

<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:** [April 24, 2002, 2:02pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/11 "2002-04-24T14:02:20Z")

</div>

well, the onClipEvent attached to \_root isn’t really doing anything with \_root, but you’ve got to attach it to something. each frame, it adds the current mouse position to end of “pos” and removes the first item.\r\ryes, the enterFrameEvents on each clip are affecting their respective clips.

---

<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:** [April 24, 2002, 9:45pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/12 "2002-04-24T21:45:26Z")

</div>

hey… im tryin to make a mouse swarm effect… where all these little dots swarm around the mouse… im using the physics spring tutorial that pom made… and trying to modify the code so if the distance between the object and the mouse is less than a given value, the inirtia is changed to 1 so it will constantly move… i tried doing that with an if statement cuz thats the only way i can think of doin it… but it doesnt work… um… and secondly, when that gets workin, i want the inirtia to get to a set speed so it wont be shooting off the screen or anything without slowing down… should i set that manually or what… or will it just slow down by itself when it shoots out of the distance range?.. anyway heres the code that i modified\r\rin object:\ronClipEvent (enterFrame) {\r this.move(\_root.\_xmouse, \_root.\_ymouse, 0.95, 0.1);\r}\r\rin actions frame: \r\rMovieClip.prototype.move = function (centerx, centery, inertia, k) \r{ \rx = -this.\_x+centerx;\ry = -this.\_y+centery;\r\rif(x \> 100 or y \> 100){\rinirtia = 1;};\r\rxp = xp_inertia+x_k;\ryp = yp_inertia+y_k;\r\_x += xp;\r\_y += yp;\r};

---

<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:** [April 24, 2002, 11:52pm UTC](https://forum.kirupa.com/t/help-understanding-code/2739/13 "2002-04-24T23:52:29Z")

</div>

nevermind that last post… ill start a new topic
