|
metadata
In my unit class, I have a script that automatically focuses this.MC to the nearest enemy MC and shoots at it, but with multiple enemies it seems that I have to write a new function for every separate enemy. Am I approaching this the wrong way? Or is there a way to make dynamically created functions with a loop. Help meh
|
|
metadata
Yes you are approaching it wrong. As a general rule if you are ever writing a function which serves a very similar purpose to another function you are violating the “Don’t Repeat Yourself” rule of programming.
Luckily, it will be easy to fix. If I understand correctly you need to focus your Unit class on the nearest enemy and shoot correct? Sounds like a TD game :) If that is indeed your goal, the following steps should help you.
1. Get your enemies into an array. Because you are probably generating enemies for the duration of the game, you will either need to either add enemies to the array when they are created (and destroy when they die/exit) or you will need a dynamic way to get enemies such as creating them all as the only children of a particular movie clip, then looping through that movieclip’s children.
2. From inside your Unit class where you want to find the nearest enemy, loop through all the enemies in your enemy array, and test how far away they are from the Unit. You can get their distance using the Pythagorean theorem like this.
`
var uPos:Point = this.localToGlobal();
var ePos:Point = enemy.localToGlobal();
var distance:Number = Point.distance(uPos, ePos);
`
Note: If the unit and enemy share different parent’s then you will need to use their global coordinates instead of local. That can be done like this:
3. Store the minimum distance, and which enemy it came from as you loop through all the enemies. Once you get the minimum enemy, you should call a function like ShootAt(enemy); which will shoot at the given enemy which you know to be the closest.
I hope this gives you some direction, but please ask if you still have questions.
|
|
metadata
alright i never really understood arrays, il look into it. thank you :D
|
|
metadata
i still dont understand how this helps me. i have to define separate variables for each enemy unit, like health, and the functions wouldnt be flexible to that. also, wouldnt i have to make a separate if/else function for every enemy unit to compare its distance to another enemy unit? can you give an example please
|
|
metadata
A single object would have a variable called Health (for example). This exact object, however, is never actually in the game. What’s in the game are “instances” of that object, each with their own variable, called “Health”. In the same way, every instance has its own X, Y (and Z if this is 3d) coordinate.
The different actions, such as shooting one of these many enemies would simply call a function (or cause the enemy to call a function, depending on how you code it), which would adjust that particular enemy’s Health accordingly.
In a nutshell, you write one function and have instances call that single function whenever they need to get some kind of information from it (like, “What’s my new health?”)
You could use an array, but then you run into problems when enemies are destroyed and you suddenly have gaps in the array. Arrays are typically just used for cycling through a grid of information.
|
|
metadata
ok so i have a health variable defined in the class? even so i still have to single out that specific enemy, im looking for a method where i call the variable from that specific enemy, not all the enemies of its kind. am i not getting something?
|
|
metadata
`
public class enemy extends Sprite{
private var health:int;
public function enemy(health:Number = 100){
this.health = health. //bad practice on this line where I've used the same var name for a class scope var and a function scope var.
}
public function takeDamage(damage:int):void{
this.health -= damage; //reduces health by the amount of damage taken.
}
}
`
now you can use the takeDamage function to reduce the health of any enemies.
|