title-decompiled

Flash Vicinity 1

Earlier tonight I was playing around with vicinity in Flash, be it all very basic. What I’m trying to do is get other surrounding objects to interact with each other if they were all in a certain vicinity. And what I thought would be hard, turned out to be a pretty quick process.

First I created a circle movieclip with 2 dynamic text fields, each to hold an x and y value respectively. I then got that text field to output its individual number relationship between itself and the current mouse position, so how far the mouse was away from the individual circle.

A got caught up with this outputting negative numbers, but I just wanted positive numbers. So after a quick search there is functionality in flash that does this nicely.

Math.abs(this.mouseX)

This piece of code converts all numbers into positive ones, so what you get is a solid distance away from the object in any direction.

I then put in a simple if statement to determine if any of the objects were in close vicinity and change the alpha of the background. I was quite surprised how easy it was. But there is a lot of stuff I still want to try, alas that will have to wait till tomorrow.

You can see the working flash and code below.

package
{
	import flash.display.*;
	import flash.events.*
 
	public class Vicinity extends MovieClip
	{
		public function Vicinity()
		{
			this.addEventListener(Event.ENTER_FRAME, enterFr)
		}
 
		private function enterFr(e:Event):void
		{
			var mouseDistanceX = Math.abs(this.mouseX);
			var mouseDistanceY = Math.abs(this.mouseY);
 
			xText.text = String(mouseDistanceX);
			yText.text = String(mouseDistanceY);
 
			if(mouseDistanceX < 130 && mouseDistanceY < 130){
				this.alpha = 0.5;
			} else {
				this.alpha = 1;
			}
		}
	}
}
10 Feb 10 Design ##