|
-
Flash Incompetent
[F8] Locating closet enemy...
alright so I just started back up with this space invader style game and my "heat-seeking" missile isn't working out exactly how it should. I think the problem is that it calls a function to locate the closest enemy which isn't actually finding the closest enemy. I am not going to put the actual code cause it will be confusing but the basic code is as follows:
Code:
function LocClos(xcoord, ycoord, bult){ //xcoord ycoord and name of the missile
closest=9000;
var closestName:String;
if (getHighestEnemyName()<0){
return;
}
highest = getHighestEnemyName()
for (i=0;i<=highest;i++){
if (xcoord>this["enemy"+i]._x){
tempx = xcoord-this["enemy"+i]._x;
} else if (this["enemy"+i]._x>xcoord){
tempx = this["enemy"+i]._x-xcoord;
}
if (ycoord>this["enemy"+i]._y){
tempy = ycoord-this["enemy"+i]._y;
} else if (this["enemy"+i]._y>ycoord){
tempy = this["enemy"+i]._y-ycoord+1000;//+1000 because the missile cannot go backwards
}
tempDistance=tempy + tempx;
if(tempDistance<closest){
closest=tempDistance;
this[bult].closestName="enemy"+i;
}
return(this[bult].closestName);
}
}
//the following is in the bullet movement code:
this[me]._y-=this[me].speed;
//check for different bullet movement types
if(this[me].movType == 1){
LocClos(this[me]._x, this[me]._y, this[me]._name);
if (this[me]._x>this[LocClos(this[me]._x, this[me]._y, this[me]._name)]._x){
this[me]._x-=this[me].speedx;
}else if(this[LocClos(this[me]._x, this[me]._y, this[me]._name)]._x>this[me]._x){
this[me]._x+=this[me].speedx;
}
}
ugh this code looks so messy... anyways hopefully this is understandable? If anything needs clarification, let me know... Thanks in advance,
ChaseNYC
-
Please, Call Me Bob
say what now? i thought you could get the distance of an object using the basic
a squared plus b squared equals c squared thing
remember the pythagoream theorem?
you dont even need to use square root
just put:
var distance=Math.pow(thing._x-_x,2)+Math.pow(thing._y-_y,2);
if(distance<distance2){
var followname=["thing"];
var distance2=distance;
}
into a loop, including of course an initial statement that makes sure the value of distance2 resets itself
not to mention your objects' names will definately have a number added to the "thing"
-
Flash Incompetent
i knew i should have paid attention in high school... so, I changed the code, it seems to be working a lot better, ESPECIALLY once I realized the return was in the forloop instead of outside it... its looking better now and seems to find the right target...
now if i could only figure out how to stop making the missile vibrate while it is on target....
Last edited by ChaseNYC; 01-19-2007 at 07:41 PM.
mmm signature
-
President
Cool you got it, I just finished and example but you already solved it so...
 This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
This is my Blog!... The gaming Process
Please check out my site: Giddel Creations
-
Please, Call Me Bob
to stop it from vibrating, whenever its _rotation is close to correct, MAKE IT correct
it vibrates because it continuously curves toward and away from its target
goddamn, i feel so young here, and im older than most in my grade too
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|