Be a Supporter!

Random Motion

  • 237 Views
  • 8 Replies
New Topic Respond to this Topic
Skeddels
Skeddels
  • Member since: Dec. 13, 2008
  • Offline.
Forum Stats
Member
Level 10
Programmer
Random Motion 2009-03-02 18:17:11 Reply

How would I accomplish giving a Movieclip random motion. So that it will wander around aimlessly. I would also like to give it boundaries, so it won't wander off the stage. I searched, but I couldn't find any topics. I also found a few tutorials such as this one, but none in Actionscript 3.0. If anybody knows how to do this, I would be very gracious to learn. Thank you in advance.


Slinky + Escalator = Everlasting Fun!

TheSongSalad
TheSongSalad
  • Member since: Jan. 17, 2009
  • Offline.
Forum Stats
Member
Level 19
Audiophile
Response to Random Motion 2009-03-02 18:24:00 Reply

well, i could do something like that pretty easy in as2, but i don't have as3 as an option, so i never learned to do much with it... sorry.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Random Motion 2009-03-02 18:32:36 Reply

There are several ways to do it, depending mainly on how you want it to look. A very easy way is to give random co-ordinates and have the object move towards it, when it reaches it (or gets near it or on a times interval) change the coordinate to another random location.. loop. Quite simple.. not very effective because it's very linear.


Sup, bitches :)

BBS Signature
Skeddels
Skeddels
  • Member since: Dec. 13, 2008
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Random Motion 2009-03-02 18:56:45 Reply

Hm. I could do something like that. I'll have the Movieclip create a linear angle in a random direction then have it add the cosine of the angle for the x coordinate, and the sine of the angle for the y coordinate, then have it continue in that line for a random amount of time. Then stop and wait in that position for a short, random amount of time, then repeat the process.


Slinky + Escalator = Everlasting Fun!

dercheezle
dercheezle
  • Member since: Sep. 8, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Random Motion 2009-03-02 18:56:59 Reply

At 3/2/09 06:32 PM, liaaaam wrote: There are several ways to do it, depending mainly on how you want it to look. A very easy way is to give random co-ordinates and have the object move towards it, when it reaches it (or gets near it or on a times interval) change the coordinate to another random location.. loop. Quite simple.. not very effective because it's very linear.

But you could apply "slippery" motion (as in a gradation in dx and dy) and have the destination point change once the MovieClip (or Sprite) gets to somewhere between .5 and .75 of the distance to the destination point. That would give it a more natural random movement, but it wouldn't really work in a grid-based game.

What kind of game are you making Skeddels?

Skeddels
Skeddels
  • Member since: Dec. 13, 2008
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Random Motion 2009-03-02 19:05:58 Reply

It's actually a really simple game that should only take a few days. There are two entrances into the stage (one at the top and one at the bottom) where object (not exactly what kind yet) enter and wander around the stage. They come in four different colors. You must drag and drop the objects into the appropriate cells (which line the sides of the stage, two on the left, and two on the right). The red ones go in the red cell, the blue in the blue, etc. While inside the cell, they merely wander around within its boundaries. If you do not put the objects in the correct cell after they have been on the stage for a set amount of time, or you accidentally place one in the wrong cell, you lose. The game gets progressively harder after awhile and the objects enter the stage at a faster rate.

So I guess you could call it a top-down game,


Slinky + Escalator = Everlasting Fun!

RaygunGothic
RaygunGothic
  • Member since: Nov. 9, 2008
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Random Motion 2009-03-02 19:18:37 Reply

If you're using AS3 I find the best way is to use a timer event then generate a random velocity on the object using something along the lines of (Math.random()-.5 )*2*maxVelocity, then just have it do this at whatever time interval you want.

Skeddels
Skeddels
  • Member since: Dec. 13, 2008
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Random Motion 2009-03-02 19:28:35 Reply

Hm. I guess that would work as well. Although I think I might just stick to my idea in the post above. Although yours has less code, I believe mine is a bit more flexible and I can easily set boundaries. On top of that, I can set the average distance they move, and if they cross the boundaries, I would simple multiply the speed by negative one, allowing the objects to continue the rest of their movement in the other direction (giving it a nice little bounce effect before choosing a new direction at random. If it does not work as I am hoping it will, I will consult your idea.


Slinky + Escalator = Everlasting Fun!

Skeddels
Skeddels
  • Member since: Dec. 13, 2008
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Random Motion 2009-03-02 19:51:28 Reply

it's finished. It actually works pretty well, unless anyone has a better way to do it:

package {
	import flash.display.MovieClip;
	import flash.events.*;
	
	public class Circle extends MovieClip {
		private var _root:Object;
		private var angle:Number;
		private var canGo:Boolean=true;
		private var counter:int=0;
		private var wait:int=0;
		private var limit:int=0;
		public function Circle() {
			addEventListener(Event.ADDED, beginClass);
		}
		private function beginClass(event:Event):void {
			addEventListener(Event.ENTER_FRAME, eFrame);
			angle=Math.random()*361*Math.PI/180;
			limit=Math.random()*180;
		}
		private function eFrame(event:Event):void {
			if (canGo) {
				this.x+=Math.cos(angle)*1;
				this.y+=Math.sin(angle)*1;
				counter++;
			}
			if (!canGo) {
				wait++;
			}
			if (wait==100) {
				wait=0;
				canGo=true;
			}
			if (counter==limit) {
				counter=0;
				limit=Math.random()*160;
				limit+=20;
				angle=Math.random()*361*Math.PI/180;
				canGo=false;
			}
		}
	}
}

Slinky + Escalator = Everlasting Fun!