Forum Topic: Closest movie clip from an array

(168 views • 8 replies)

This topic is 1 page long.

<< < > >>
None

ZeroUltimax

Reply To Post Reply & Quote

Posted at: 1/8/09 08:29 PM

ZeroUltimax DARK LEVEL 12

Sign-Up: 05/16/08

Posts: 42

Hey people, here's where I'd need your help. I have an array called _global.namelist. Everytime i duplicate a MC, i made it so that his name is added in there.
Now, what I need to do is figure out which MC the closest to my mouse using this array. After that, i want to trace his name. I'm really stuck on this one. Any suggestions are welcome.


None

korded

Reply To Post Reply & Quote

Posted at: 1/8/09 11:16 PM

korded EVIL LEVEL 06

Sign-Up: 02/06/00

Posts: 1,094

I'll take a stab.. but only pseudo, not going to try and write it all out in proper syntax.

I'd start by running a for loop that runs through each mc in your array and checks the distance from the x/y of each mc to the x/y of the mouse, using the Pythagorean theorem. If you are okay with it checking the center point of your mcs, it will be easier.. if you want it to check to the outside edge of the mcs, that would be tougher. You'd have to perform a series of hit tests I'd imagine along the path between the mouse and movieclip until one equated to true.

I would create two variables, one for the current distance being checked and one for your highest distance. The first time the for loop plays, it would enter the first distance checked as your current highest value. Each subsequent time it runs, it would need to check if the current distance between movieclip and mouse is higher than the value of the highest distance variable; if so, it would replace that variable with the current distance checked.

After the for loop completes, you'd need to run a check to see what the highest value was..

Alternatively - though I don't know if this is possible - you could store all the distances from the for loop mentioned above in a separate array.. if you can then reorganize the array in order from lowest value to highest, that would be perfect, you can then just trace the last value in the array. I'm pretty sure you can sort arrays alphabetically, so this may just be possible. Definitely would be cleaner.

Keep in mind this is all just theory, so someone else could maybe chime in if I'm off here.


Elated

pawlakt

Reply To Post Reply & Quote

Posted at: 1/9/09 01:14 AM

pawlakt DARK LEVEL 09

Sign-Up: 04/04/08

Posts: 130

here... I wrote a program that creates a number of circles in an array. When you click somewhere on the screen, it traces the distance to all the circles. I wasn't able to figure out how to check which one was the closest... but the theory above sounds like it would work! I just wasn't able to figure it out :(

dist_check.fla

hope this helps!

BBS Signature

None

The-titan

Reply To Post Reply & Quote

Posted at: 1/9/09 08:13 AM

The-titan LIGHT LEVEL 18

Sign-Up: 07/23/05

Posts: 222

At 1/9/09 01:14 AM, pawlakt wrote: here... I wrote a program that creates a number of circles in an array. When you click somewhere on the screen, it traces the distance to all the circles. I wasn't able to figure out how to check which one was the closest... but the theory above sounds like it would work! I just wasn't able to figure it out :(

dist_check.fla

hope this helps!

its pretty simple to check for the shortest one, you just need another variable (i call it "zDist") it will store the distance only if the distance is less than the last one you added,

it goes in the "for loop"

if(dist<zDist){
	zDist = dist
	trace(zDist);
}

also don't forget to declare the variable it self:

var zDist:Number = 1000;
BBS Signature

None

StaliN98

Reply To Post Reply & Quote

Posted at: 1/9/09 02:39 PM

StaliN98 LIGHT LEVEL 10

Sign-Up: 07/27/07

Posts: 690

The distance is calculated using Pythagoras' Theorem:

distance = Math.sqrt(Math.pow(xdistance,2)+Math.pow(ydistance,2));

None

pawlakt

Reply To Post Reply & Quote

Posted at: 1/9/09 05:09 PM

pawlakt DARK LEVEL 09

Sign-Up: 04/04/08

Posts: 130

At 1/9/09 08:13 AM, The-titan wrote: its pretty simple to check for the shortest one, you just need another variable (i call it "zDist") it will store the distance only if the distance is less than the last one you added,

it goes in the "for loop"

if(dist<zDist){
zDist = dist
trace(zDist);
}

also don't forget to declare the variable it self:

var zDist:Number = 1000;

I tried using that method... The only problem is that the var zDist doesnt get any bigger when you move away from the zDist movieclip. So in order for you to get a new closest result, you have to get the mouse closer to the movieclip than zDist is stored...

BBS Signature

None

StaliN98

Reply To Post Reply & Quote

Posted at: 1/9/09 07:26 PM

StaliN98 LIGHT LEVEL 10

Sign-Up: 07/27/07

Posts: 690

Then reset zdist to a high number every time you want to check through your array.


None

ZeroUltimax

Reply To Post Reply & Quote

Posted at: 1/9/09 07:43 PM

ZeroUltimax DARK LEVEL 12

Sign-Up: 05/16/08

Posts: 42

well people, i thank you all for your help. I'm "compiling all of this in a single code. I'll post it here when I'm done


None

ZeroUltimax

Reply To Post Reply & Quote

Posted at: 1/9/09 08:23 PM

ZeroUltimax DARK LEVEL 12

Sign-Up: 05/16/08

Posts: 42

Here's the whole code i used. It's for a bullet to aim to the monster closest to it.
Anotations included. It tested it a lot of times and it worked 100%. If you like it, just drop by my page and say thanks.

onClipEvent (load) {
	var rot;
	//used to test the angle to the monster
	var dr;
	//used to make the bullet aim right on the monster
	var starting = 0;
	//delay before bullet starts turning
	var rotpow = 45;
	//how fast the bullet turns
	var test;
	//stores the monster to be tested
	var tar;
	//target on the bullet (I didn't use target because it's a property)
	if (_global.monsterlist.length>0) {
		var tar = _global.monsterlist[0];
	}
	//initiates the tar varriable only if there is something to be targetted
}
onClipEvent (enterFrame) {
	for (a=1; a<_global.monsterlist.length; a++) {
		test = _global.monsterlist[a];
		if (Math.sqrt(Math.pow(_root[tar]._x-_parent._x, 2)+Math.pow(_root[tar]._y-_parent._y, 2))>Math.sqrt(Math.pow(_root[test]._x-_parent._x, 2)+Math.pow(_root[test]._y-_parent._y, 2))) {
			//test if the distance to tar is bigger than the distance to the the tested MC. 
			//If true, makes the target the tested MC
			tar = test;
		}
	}
	if (_global.monsterlist.length>0) {
		if (starting>1) {
			rot = ((Math.atan2(_parent._y-_root[tar]._y, _parent._x-_root[tar]._x))/(Math.PI/180))-90;
			//finds the angle from bullet to monster
			dr = rot-_parent._rotation;
			//difference between current rotation and  rot (the rotation we want)
			if (dr>180) {
				dr -= 360;
			}
			if (dr<-180) {
				dr += 360;
			}
			// those lines of code are there to make dr be between -180 and 180 degrees
			if (dr<rotpow & dr>-rotpow) {
				_parent._rotation = rot;
				//in case the rotation difference(dr) is smaller than the rotation power (rotpow)
				//stores the target rotation(rot)as the MC's rotation
			} else {
				//in the other case, it modifys the MC's rotation by the rotpow so it gets 
				//closer to the target rotation
				if ((dr<180 & 0<dr) | (dr>-360 & dr<-180)) {
					_parent._rotation += rotpow;
				} else {
					_parent._rotation -= rotpow;
				}
			}
		} else {
			starting++;
			//makes the bullet rotate only after few frames
		}
	}
}

All times are Eastern Standard Time (GMT -5) | Current Time: 02:27 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!