Jump to content
unplugged_web

Randomising Image Movement

Recommended Posts

I wonder if somebody could help me please.I'm working on a site that I want to have letters floating about in, but I'm not sure exactly how to do it. I want the letters to smoothly float around a restriced area.I've got:

m.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};i.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};s.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};h.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};m2.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};a.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};o.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};u.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};l.onEnterFrame = function() {	var maxMoveValue=50;	this._x += (Math.random()-0.5)*maxMoveValue;	this._y +=(Math.random()-0.5)*maxMoveValue};

and it works in so much as the letters randomly move, but they move around the whole of the screen and move in a jagged way. I'd like them to move around an area that is 650 pixels wide by 220 pixel height, but am a bit lost on how to be that precise so would appreciate any help.Thanks

Share this post


Link to post
Share on other sites

I assume you have a finite number of letters here ( 9 I guess), and AS 2.0> make a MovieClip which shows letters ( need not to be a textfield, can be anything, you can draw something letter and covert to movieclip)> double click it to go inside it, click its first frame, and use the following code:

var vx = 1;  //x direction 1 or -1var vy = 1;  //y direction 1 or -1var speedx = 0;  //x speedvar speedy = 0;  //y speedvar theta = Math.random()*6.38; // angle bw 0 and 2piethis._x = Math.random()*Stage.width; // place this thing randomly on stagethis._y = Math.random()*Stage.height;this.onEnterFrame = function() {		if(theta==6.38){theta = 0;} // theta cycles from 0 to 2pie	else {theta += 0.01;} // in steps of 0.01, you can vary it but carefully		// reverse direction(vx and vy) when near edges	if(this._x > Stage.width || this._x < 0 ){ vx = vx*-1;} // x bounce, limits: 0 to stage width	if(this._y > Stage.height || this._y < 0 ){ vy = vy*-1;} // y bounce, limits: 0 to stage height		speedx = Math.sin(theta); // x speed is varied sinusoidally	speedy = Math.cos(theta); // y speed is varied sinusoidally		this._x = this._x + vx*speedx; // update x pos	this._y = this._y + vy*speedy; // update y pos}

you can set other limits than Stage.width etc.now this clip should float around (quite randomly :) ) . make all your letters this way.In fact, you can put this code inside any MovieClip and it will float randomly.In case you need to create many similar things floating around , create a single movieclip like above and in main timeline ( NOT inside the clip) use the following code :

var i = 0;this.onLoad = function() {	for(i = 0; i<= 10; i++){		duplicateMovieClip(this.mymovie,"mymoviecopy" + i,this.getNextHighestDepth());	}}

here mymovie is instance name of the first clip.

Edited by Shade

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...