Random Motion

Hi everyone,\rI’ve been working endlessly to figure out if there is a method to make an object move randomly. I know there is a way, because I have seen many sites using it. Yet, I am unable to create anything besides specified movement in the x and y values. I can’t make anything move randomly. \r\rAny help will be highly appreciated!\r\rThanks,\rKirupa

what kind of random motion do you want? vibration? choosing a random destination and moving there?\r\ryou could make a movie that will gravitate towards a given coordinate, and then set new coordinates with Math.random(); whenever it gets there.\r\rputting this in a movie will create random vibration:

 \r\ronClipEvent(load){\r\r&nbsp &nbsp &nbsp &nbsp sx = guitar._x;\r\r&nbsp &nbsp &nbsp &nbsp sy = guitar._y;\r\r}\r\r\r\ronClipEvent(enterFrame){\r\r&nbsp &nbsp &nbsp &nbsp guitar._x = sx + Math.random()*6-3;\r\r&nbsp &nbsp &nbsp &nbsp guitar._y = sy + Math.random()*6-3;\r\r}

\r\rwhat do you need?

Hey supra,\rThanks for replying. I need to make a movie clip reach a random destination and continue moving to random destinations. I’m trying to make a small square randomly move around a flash movie.\r\rThanks!\rKirupa

Edited to update for mx\r<embed src=“http://members.shaw.ca/rlinton/eg/randomzombie.swf” quality=high width=100 height=100>\r\rrandomness is fun… ; )\r\rput a few functions in _root, frame 1:

//// functionsfunction getdistance(x,y,x1,y1){	var run,rise;	run = x1 - x;	rise = y1 - y;	return(_root.hyp(run,rise));}function hyp(a,b){	return(Math.sqrt(a*a + b*b));}MovieClip.prototype.reset = function(){	var dist,norm;	this.x = this._x;	this.y = this._y;	this.speed = Math.random()*4+2;	this.targx = Math.random()*100;	this.targy = Math.random()*100;	dist = _root.getdistance(this.x,this.y,this.targx,this.targy);	norm = this.speed/dist;	this.diffx = (this.targx-this.x)*norm;	this.diffy = (this.targy-this.y)*norm;}MovieClip.prototype.move = function(){	if(_root.getdistance(this.x,this.y,this.targx,this.targy)>this.speed){		this.x += this.diffx;		this.y += this.diffy;	}else{		this.x = this.targx;		this.y = this.targy;		if(!this.t){this.t=getTimer();}		if(getTimer()-this.t>1000){this.reset();this.t=0;}	}	this._x = this.x;	this._y = this.y;}

\rand then in each clip you want to behave like this:

\ronClipEvent(enterFrame){	move();}

\rsince reset and move have been defined as methods, you can’t use them as variable names.\r\rcheers.

Thanks supra. This is truly amazing! I am creating a section in which Flash techniques that don’t warrant a tutorial will be posted. Would you mind if I include this in one of my upcoming tip sections? Your name will appear on the page highlighting that this is your idea!\r\rThanks,\rKirupa

kirupa, you have carte blanche to use anything i post as tips / tuts.\r\rcredit is always appreciated. i should credit the internet as a whole for my flash education.\r\ra few notes i left off:\r\rmove and reset are not the only variables created in each movie by those functions, there’s diffx/y, targx/y, t, and speed.\r\rin the reset method, the 100’s define the range of possible placement. perhaps that’s obvious, but if you’ve put this in a larger movie and are wondering why everything happens in the top left corner, that’ll be why.\r\rcheers.

Instead of making a wimpy tips section like the other sites, I decided to make a tutorial altogether. Here’s how the tutorial looks: www.kirupa.com/developer/…random.asp \r\rThanks again supra. I did modify your code slightly to make it easier for users who are not proficient in actionscript to modify the width, height, and speed to cpmplete.\r\rCheers,\rKirupa

hey kirupa,\r\rgreat tut. the variable “cycle” represents the number of milliseconds it will wait between movements. strictly speaking, it should probably be set in the if (!this.t) { block. right now it’s being set every frame! for the speed, adjust how “speed” is set in the reset() function.\r\rif a movie’s registration point (the crosshairs) is in the center, there’s no reason to subtract its _width/_height from the scope of the randomness. i have a habit of moving the registration point to the top left of all my movies. that way it works like the stage (0,0 represents the top left corner). that’s why i did it. actually i had subtracted half the _widht/_height, effectively making it behave as if the registration point was in the centre (so why did i move it in the first place? oh well…).\r\ri don’t mean to nitpick, i’m just thrilled to see something i did up there.\r\rthanks!

Hey supra,\rI did not know that. I’ll fix the terminology for cycle in a few hours :slight_smile: \r\rThanks,\rKirupa

I want to thank the both of you, kirupa, and supra both. I got into flash back when only version 3 was out. Then i quit using it, and then I hopped back on when version 5 was out, and so much had changed. I came here “www.kirupa.com” , and was totally impressed with the detail in the tutorials. It really got be back on track. Then I met supra, and he has helped me a lot with actionscript. So thanks to the both of you for helping me out. \r\rCheers, Brian

Glad to see you back into it. Flash 5.0 is an amazing step forward for sw objects.\r\rSupra is in fact, the best a/s’er I’ve had the pleasure to bulletin board with. Thank you Supra… you rule!!!

Supra does rock! Supra, are you Colin Moock in disguise lol j/k? Thanks for the nice comments zao, and upuaut8, nice to see you again. You took a long hiatus from the board and half the posts were empty hehe.


hi there\r\ri’m relatively new to flash and i need a little help! i am trying to achieve something similar to the random motion actionscript that supra has posted, and i’m not sure how to go about it. i’d like to have lots of shapes floating around randomly, such as in the examples in the tute, with the added ability to click on the shapes (with each linked to a separate page/url). can anyone let me know how i should go about this or indeed if it is possible? also on my wishlist (although probably very tricky) is for the shapes to bounce off each other when a collision is detected.\r\ryour wisdom would be very much appreciated!\r\rsoumali :slight_smile:

Concerning your first question, just read Kirupa’s tutorial about randomness, and put buttons inside the movie clips that are randomly moving.\rConcerning your second question, tricky tricky… You’d better wait for Supra or Thoriphes or Dan or Sinfiniti or Upu or Phil (just kidding)…\r\rpom 0]

lol, wuts with all the old posts being drugged back up to the top of the list. 11/11/01!

you can use a hitTest() with each of the movieclips, then what you can do is make them go the opposite direction. i won’t go into detail right now cause it’s late.

is this all the code thats needed to vibrate a clip? Is “guitar” then name of the instance?

yes, that’s right.\r\ri cut and paste that code form a project and neglected to anonymise it.\r

Hey folks, \rgreat code and ideas… but i have been trying it in MX now and it doesnt work, whereas it works when i export i for the flash 5 player… can u tell me why?