Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:32:41+00:00 2026-06-11T12:32:41+00:00

I have 3 Classes. One is a Global class that has my function: public

  • 0

I have 3 Classes.

One is a Global class that has my function:

public static function getDistance(ObjOne, ObjTwo)
{
    var distance = Math.sqrt( ( ObjOne.x - ObjTwo.x ) * ( ObjOne.x - ObjTwo.x ) + ( ObjOne.y - ObjTwo.y ) * ( ObjOne.y - ObjTwo.y ) );
    return distance;
}

Then I have a MovieClip that is the class: Minion and another called: Turret

In the Minion class I am calling: Global.getDistance

And setting the args to: Global.getDistance(this, ?????)

How can I get the Turret Object from the Turret class for the final param?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:32:42+00:00Added an answer on June 11, 2026 at 12:32 pm

    If each minion has a single turret that it is targeting, then you should hold a reference to the turret inside of your Minion class. There should be no need for a static function to get distance (unless it’s used for other things besides the minion/turret relation).

    For your turrets to be aware of ALL minions (to decide which minion to attack), a good way to do this is to store them all in a statically assessable vector (array).

    Here would be a sample of your Minion class:

    public class Minion {
        public static var allMinions:Vector<Minion> = new Vector<Minion>(); //create a static array to store all your minions
    
    
        public var turret:Turret;
    
        public function Minion(targetTurret:Turret):void {
            turret = targetTurret;
            this.addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);
            this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage,false,0,true);
        }
    
        private function addedToStage(e:Event):void {
            allMinions.push(this); //add this minion instance to the array when it's added to the display list
        }
    
        private function removedFromStage(e:Event):void {
            //remove this minion instance from the array of all minions when it's removed from the display list
            var index:int = allMinions.indexOf(this);
            if(index >= 0){
                allMinions.splice(index,1);
            }
        }
    
        private function move():void { //whatever function you use to move the minion along
           //get distance here, using your local turret var
           getDistance(this,turret); //this could be a local function, or some static function somewhere - local is faster, especially if being called everyframe or in a loop.
        }
    
        //a function to take damage, returns true if still alive after the damage
        public function takeDamage(amount:Number):Boolean {
            this.health -= amount * armor;  //armor could be a value between 1 (no armor) and 0 (invincible)
            if(health <= 0){ 
                 //die
                 return false;
            }
            return true;
        }
    }
    

    This passes in the appropriate turret when you create a new Minion – new Minion(turretInstance), and has a statically accessible array that holds all the minions that are on the display list at any given time. It also adds a function to take damage


    For the turrets to attack, you’d want to scan the array of all minions, and determine which are close enough to attack, either attack all (if that’s the type of turret) or pick one (the closest one usually) to attack.

    Turret class:

    public var range:Number = 200; //how close to the turret a minion needs to be before it can attack
    public var attackPower:Number = 10; //how much damage does this turret cause
    
    public function attack():void {
    
        //this for loop goes through all the minions and finds the closest one
        var closestMinion:Minion;
        var tmpDistance:Number; //
        var distance:Number;
        for each(var minion:Minion in Minion.allMinions){
            distance = Math.sqrt( ( minion.x - this.x ) * ( minion.x - this.x ) + ( minion.y - this.y ) * ( minion.y - this.y ) ); //find the distance between the current minion in the loop and this turret
            if(distance < range && isNaN(tmpDistance) || distance < tmpDistance){  //if the distance of this minion is less than the current closest, make the current closest this one
                closestMinion = minion;
                tmpDistance = distance;
            }
    
        }
    
        //attack the closest one
        if(closestMinion){
            closestMinion.takeDamage(attackPower);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, one that inherits from the other. The base class is
I have 2 classes that inherits from a father class. The father class has
I have following Java code: import java.io.*; class Global{ public static int a =
I have two classes (MVC view model) which inherits from one abstract base class.
I have some classes that, for one reason or another, cannot be or need
In one of my projects, I have some classes that represent entities that cannot
I have this code (two classes) class url { private $profile_id; private $short; public
I have two classes one of which inherits from the other. Im trying to
I have 2 classes where I have one to send commands to a socket,
Starting to learn Canvas and have two classes so far (main one to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.