PDA

View Full Version : She's A Little Confused



rahnefan
August 20th, 2007, 11:15 AM
I've read the tutes and searched the threads, and still can't figure this out. Please help.

In the attached .fla, I've given "seeker" a quasi-random movement (8 directions) with bounds. That works fine. Following seeker around is "fov," representing seeker's field of view. This is partly what I've taken from the pros here and partly my own ugly experiment...sorry if the code is horrendous.

The problem: I want the seeker to pursue any object inside the clip "men," which includes m1, m2, and m3. They are not duplicated clips. I've used a for loop and an array. It wants to work, but some of the time, it goes after the wrong clip! What I mean is, it'll collide with one and pursue another. Why?

Also, is there a way to extract the name of the clip that fov hits so that it can populate the dynamic text field named "target?"

Thank you.

SacrificialLamb
August 21st, 2007, 12:53 AM
i found by moving the if out of the for loop helps. For loops run really fast and seem to get ahead of there self some times so don't put code in there you don't need.

for (i=0; i<numMen; i++) {
dude_choose = target_mc[i];
dude = eval(dude_choose);
if (_root.fov.hitTest(dude)) {
initiate = 1;
_root.mating = 1;
dir = 0;
chase = dude
_root.target = "man"+(i+1);
}
}
if (initiate == 1) {
dude =chase
xspeed = 5;
yspeed = 5;
if (dude._x-thisx>7) {
this._x += xspeed;
} else if (thisx-dude._x>7) {
this._x -= xspeed;
} else {
xspeed = 0;
this._x = dude._x;
}
if (dude._y-thisy>7) {
this._y += yspeed;
} else if (thisy-dude ._y >7) {
this._y -= yspeed;
} else {
yspeed = 0;
this._y = dude._y+5;
}
}
i personally would make it a function, but that might be more hassle than it's worth. But you will need to fix some of the variables up the "chase = dude" then "dude =chase" I used is bad but I was just testing and did not want to find and replace the 'dude's in the if also _root.target (the dynamic text this) is a flash word or some thing and you might not want to use it was a variable to help the readability of your code.
also you missed a " ._y" (or I deleted it accidentally).
Currently it favors the last unit in the array I would suggest it to add the units name to an array in the for loop then find the closest if you are planning to add this to and AI that might have more tan one target, just an idea

rahnefan
August 21st, 2007, 01:44 PM
Thank you Sacrificial!! That helped a lot. But I don't understand what dude = chase / chase = dude does. I left it out.

Now that I have added clips to the array I see a new problem. Once a collision is detected with one clip, it starts to close in, but stops when fov collides with a second target. Any ideas?

I had no idea about target. Thank you.

rahnefan
August 21st, 2007, 03:20 PM
LOL...I added back the chase=dude parts and made some other changes and now she works just fine! Except no matter who she attacks, it tells me she's attacking "man 5". Per your advice, I changed that text field from target to just targ, so now the code reads


if (_root.fov.hitTest(dude)) {
_root.targ = "man "+(i+1);

SacrificialLamb
August 22nd, 2007, 04:54 AM
It should not do that, the loop was not even getting that high, if you have added blank values to the array or made the for loop longer than there are values it might do some thing like this but I don't really see why. And I have been unable to recreate this problem (so it's likely to be some thing you added) but you can change the

_root.targ = "man "+(i+1);
To this;


ptarg = dude_choose
_root.targ = ptarg.slice(6, 9)+ptarg.slice(11)

I am using the string values that you had in the array then getting the shall MC name "men" then adding the anything that comes after the "m" of the MC name you can replace " ptarg.slice(6, 9)" with "man " if you like, the problem is problem seems to be coming for the second half. This is more of a work around rather than a fix

As for the dude =chase all that needs to be done was change the 'dude' var in the if statement that I moved out of the for loop to 'chase' what was happening was that I needed to store the MC name and not have the loop change it to the last it tested so I put the hit MC in to chase but will testing I did not want to change all the 'dude's to 'chase' in the if statement incase I had to move them back.

rahnefan
August 27th, 2007, 02:14 PM
Thank you! Much better!