Random movement through action script

I’ve created a movie clip in which the first frame is empty and, for some reason I don’t quite understand, it won’t let me at it. The little white dot that signifies the existance of said move clip only appears when I turn on the outlines and even then it won’t let me click on the @#%$ thing. I’ve tested the scene and the movie clip plays through (I forgot to add the “stop” action, which might be the problem, but I have another similair movie clip where I did add a “stop” action and I’m not allowed at it, either.), so it defienately exists, but Flash just won’t let me at it!!!\r\rDoes anyone know why this might be happening and, more importantly, how I can fix it without undoing half of my project?

For some reason, every time I post on this board I figure out the answer about 5 seconds later. I’d stop, but I can never resolve anything if I don’t post first.\r\rThe movie clip was, unbeknownst to me, inside of another movie clip. There we go.

Could you post a message asking for somebody to explain the random movement then ??? If you can figure it out 5 seconds after, I’m interested.\r\rpom

Posting a question about e-mail, dynamic text fields, and how to make it all work, that 5 second time frame would be a godsend. And, oh, when the answer comes to you 5 seconds later, please post it fer me would ya?\r\rpj

I’ll spend the weekend trying to figure out all those things (though to be honest and this point I haven’t really read your posts in depthly, so I might already know the answer… Probably not, though.). Right now I’m too busy, but just remind me on the weekend.

Aren’t you Phil ?

For Pom…\r\rHere’s the code on Kirupa with my explanations. There’s a few things I didn’t understand as well as a few that I only think I understand, and probably a few things that I got wrong. I’d figure it out in five seconds, but I’m tired and I’m going to bed. I’ve worked too bloody hard today.\r\r//Random Movement: kirupa.com\r//Thanks to Suprabeener for the original code!\r\r\r//Calculates the rise and run of your movement. Remeber Grade 9 math?\r//That’s what this is. rise over run = slope. Run = x2-x1, Rise=y2-y1.\rfunction getdistance (x, y, x1, y1) {\r&nbsp &nbsp &nbsp &nbsp var run, rise;\r&nbsp &nbsp &nbsp &nbsp run = x1-x;\r&nbsp &nbsp &nbsp &nbsp rise = y1-y;\r&nbsp &nbsp &nbsp &nbsp return (hyp(run, rise));\r}\r//I dunno… Formula doesn’t really ring a bell… Man, I should have paid\r//more attention in math class…\rfunction hyp (a, b) {\r&nbsp &nbsp &nbsp &nbsp return (Math.sqrt(aa+bb));\r}\r//As best as I can gather, this tells the movie clip that it should take in\r//to account that it should change its destination.\rMovieClip.prototype.reset = function () {\r//Just variable declaration.\rvar dist, norm, movie_height, movie_width;\r\r\r// movie_height: refers to the height of your movie\r// movie_width: refers to the width of your movie\r\r//---------------------------------\r\r// This is just your movie height and width. Basic enough.\r// It stops things from going offscreen.\r// NOTE: For some reason, when I test this movie it doesn’t show me the\r// full movie. I haven’t tried publishing it, so it might work when I do\r// that, but isn’t that strange? I accidentally removed a bracket \r// before the previous variable declaration and the full screen was shown.\r// I’m not sure what to think of that…\rmovie_height = 550;\rmovie_width = 440;\r\r//---------------------------------\r\r\r//Simply, the speed at which the object travels - a random number from\r//4 and 2. Big numbers go fast. Small numbers go slow. Easy enough.\rspeed = Math.random()4+2;\r//This randomly selects the x and y values of your object’s destination.\r//movie_width-_width takes in to account the size of the screen and\r//subtracts the x value of your object so that the destination isn’t off\r//screen.\rtargx = Math.random()(movie_width-_width);\rtargy = Math.random()*(movie_height-_height);\r//This calculates the distance from where you are to where you’re going.\rdist = _root.getdistance(_x, _y, targx, targy);\r\r//This part’s kind of confusing as it seems to involve some type of\r//physics I’ve never learnt. The norm is a tenth of the time it takes to\r//go from where you are to where you’re headed.\rnorm = speed/dist;\r//At this point it multiplies the distance between where you are and where\r//you’re headed by 1/10 of the time it will take to get there. Why? I \r//dunno. I only took one year of Physics. Hypothetically speaking, it\r//gives you an advanced form of velocity.\rdiffx = (targx-_x)*norm;\rdiffy = (targy-_y)*norm;\r};\r\r\r//This tells the movie clip that declares itself willing\r//(thus the script on the movie clip) to follow the functions stated here.\rMovieClip.prototype.move = function () { \r\r//Just sets the time waited between movements.\r//NOTE: For some reason, setting the cycle to 0 milliseconds does NOT\r//remove the pause… 200 milliseconds seems to be the minimum or \r//something.\rvar cycle;\r\r\r\r// cycle: specifies the number of milliseconds waited \r// between movements\r\r//--------------------------------------------\r\rcycle = 200;\r\r//--------------------------------------------\r\r//This states that if your distance is larger than your\r//speed it will add the difference between your first point and your\r//last point to your first point, which I believe means that your last\r//point would stay the same, thus making this code totally useless. In\r//other words, I HAVE NO IDEA.\r//It’s also possible (and, judging the code, more probable) that your\r//inital point becomes equal to your ending point, but that seems less\r//probable given the performance… There’s no instajump… Unless you\r//take AWAY this code, strangely enough.\rif (_root.getdistance(_x, _y, targx, targy)>speed) {x += diffx;y += diffy;\r} else {\rx = targx;\ry = targy;\r//This calculates how much time has passed.\rif (!this.t) {\rt = getTimer();\r//If the amount of time passed is more than the set cycle the code\r//restarts and finds a new random destination.\r//I’ll admit I’m confused, though. If t=getTimer0 and we’re subtracting\r//t from getTimer, wouldn’t it always result in 0? Oh well.\r}if (getTimer()-t>cycle) {reset();\r//… and sets the timer back to 0.\rt = 0;\r}\r}\r//The new x and y values are set as the official positions of your object.\r_x = x;\r_y = y;\r}

wow… \r\rthe formula that doesn’t ring a bell is grade 9 math too, pythagorus theorem: a^2 + b^2 = c^2.\r\rthe idea was to choose a random coordinate, and then move towards it at a steady, moderately random speed. there’s no physics, i dropped out of physics. ; )\r\rthe point of pythag and norm is that the clips are effectively travelling along a hypontenuse. norm was how many steps it would take to get to the destination. which i then divided by the distance to get the amount needed to travel each frame. that is stored in the clip as xdiff and ydiff. sure, that’s a form of velocity, probably not so advanced though, pixels per frame. ; )\r\rthe if statement in the move() function checks to see if the clip is closer to its destination than it would move that frame (than its speed). if it is, then rather than adding the x and ydiffs, it just assumes that position and gets another set of random coordinates. if it’s not that close yet, it adds its x and ydiffs, continuing on its merry way.\r\rthe pause happens because getTimer() returns the number of milliseconds since the movie started, not the same result each time. so once the difference between a getTimer and the first getTimer is greater than cycle (ie. a fifth of a second has passed), the clip will reset. setting it to zero should remove the pause(?), bizarre.\r\rcheers.

Thanks for your posts, guys, especially Liveacoustic and Supra, but the problem was not in the math nor in the physics involved (not so difficult after all). The thing is (you can see it in another post called radom movement) :\rthe movies always appear on the upper left corner and on the life of me I can’t figure out where their position is initialized. I tried to put in the MC \ron (load) {_x = 150 ; _y = 150 ;} \rbut I guess the function reset must change them somehow. Just can 't find how.\r\rpom

ilyaslamasse,\r\rthe points all go to 0,0 right off because none of the variables have been initialised. thus x += xtrav and y += ytrav both evaluate to 0 (null += null = null) .\r\rto prevent this behaviour, have the clips reset on a load, initialising the variables.

 \r\ronClipEvent(load){\r\r   reset();\r\r}\r\ronClipEvent(enterFrame){\r\r   move();\r\r}

Awesome. Even though you gave different variable names than in the tutorial, which gave me a half an hour head ache.\r\rAnother small problem : my movies always want to go up, outside the screen. The thing is I didn’t even change the width and height, whereas my scene is now twice as big. Any idea ?\r\rpom

one thing might help is removing the -_width/2 and -_height/2 from the constraints in the reset() function.\r\ri did that because i tend to move the registration point of all my movies up to the top left (like _root). it’s in the centre of the movie by default, and if yours are in the centre, there’s no reason to subtract half the height and width.\r\rbut that wouldn’t make them fly off the screen, just make it possible for them to go off the top and left.\r\rlet me know how it goes.

I don’t know, what do you think ? (I removed the - _width which does make them go not so much to the left but still…).\r\rpom

if you put your fla somewhere i can look at it, i’ll take a look.\r\ror mail it to me … click on my username and send me mail through the profile page.

Thank you, it’s there :\rwww.multimania.com/ilyasl…plode2.zip\r\rpom

in reset() this line:\r\rspeed = Math.random()*4+2;\r\rshould read:\r\rspeed = Math.random()*4-2;\r\ryou’re getting a number between 2 and 6 instead of between -2 and 2.\r\rwhy don’t computers ever know what you mean! ; )\r\ra small point … it’s not doing any harm in this case, but it’s generally a bad practice to define your methods more than once. when the “reset animation” button sends the movie back to frame 1, all the methods are defined - again. easiest thing would be to put your definitions in frame 1, leave frame 2 blank, and put everything else in 3.\r\rbut it’s not really an issue here, there are only 2 definitions. ; )