Help understanding code

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

you can mail me the fla at [email protected]

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:

  \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:

  \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.

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…

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””>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.

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

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:

  \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*

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]

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.

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]

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.

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 = xpinertia+xk;\ryp = ypinertia+yk;\r_x += xp;\r_y += yp;\r};

nevermind that last post… ill start a new topic