Actionscript 3 AS3传递对象

Actionscript 3 AS3传递对象,actionscript-3,Actionscript 3,我有三节课 一个是具有my函数的全局类: 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; } 然后我有一个MovieClip类:

我有三节课

一个是具有my函数的全局类:

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;
}
然后我有一个
MovieClip
类:
Minion
和另一个名为:
tarta

在我呼叫的
Minion
类中:
Global.getDistance

并将参数设置为:
Global.getDistance(this,???)


如何从塔楼类中获取最终参数的塔楼对象

如果游戏中只有一个炮塔,可以使用单体设计模式

private static var instance:Turret = null;

public static function getInstance()
{
    if(instance === NULL)
        instance = new Turret();
    return instance;
}

因此,您可以调用转塔.getInstance()并使用您的转塔对象。希望能有帮助。如果你有一个以上的炮塔,你应该有一个游戏类,里面有一个数组或向量,以及你所有的炮塔。

如果你的游戏中只有一个炮塔,你可以使用单体设计模式

private static var instance:Turret = null;

public static function getInstance()
{
    if(instance === NULL)
        instance = new Turret();
    return instance;
}

因此,您可以调用转塔.getInstance()并使用您的转塔对象。希望能有帮助。如果你有一个以上的炮塔,你应该有一个游戏类,里面有一个数组或向量,里面有你所有的炮塔。

我不知道你到底需要什么,第二个参数的类型是什么?由于您处理的是全局函数,我建议只使用
DisplayObject
,因为两者都有x/y属性

public static function getDistance(clip1:DisplayObject, clip2:DisplayObject):Number     
{ 
   return Math.sqrt( ( clip1.x - clip2.x ) * ( clip1.x - clip2.x ) + ( clip1.y - clip2.y ) * ( clip1.y - clip2.y ) );
}

我不知道你到底需要什么,第二个参数的类型?由于您处理的是全局函数,我建议只使用
DisplayObject
,因为两者都有x/y属性

public static function getDistance(clip1:DisplayObject, clip2:DisplayObject):Number     
{ 
   return Math.sqrt( ( clip1.x - clip2.x ) * ( clip1.x - clip2.x ) + ( clip1.y - clip2.y ) * ( clip1.y - clip2.y ) );
}

如果每个仆从都有一个它瞄准的炮塔,那么你应该在你的
仆从
职业中保留一个对炮塔的引用。应该不需要静态函数来获取距离(除非它用于除仆从/炮塔关系之外的其他用途)

为了让你的炮塔知道所有的仆从(决定攻击哪一个仆从),一个好方法是将他们全部存储在一个静态评估向量(数组)中

以下是您的仆从课程示例:

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;
    }
}

如果每个仆从都有一个它瞄准的炮塔,那么你应该在你的
仆从
职业中保留一个对炮塔的引用。应该不需要静态函数来获取距离(除非它用于除仆从/炮塔关系之外的其他用途)

为了让你的炮塔知道所有的仆从(决定攻击哪一个仆从),一个好方法是将他们全部存储在一个静态评估向量(数组)中

以下是您的仆从课程示例:

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;
    }
}


.. 如果你想延长炮塔,你就完蛋了。一个今天就是一个明天。我不推荐单件,这是一个有问题的设计模式。数据管理器中的静态实例是可以的,但不要强迫只拥有一个,您可能需要变化或倍数。这就是我所说的。如果他只有一个炮塔,他应该使用它。如果没有的话,应该有一个StageManager或其他东西,它引用了所有的塔楼和仆从。我会有多个塔楼,而且多个仆从“waves”单体也有它们的用途,当你有多个开发人员在一个项目上,并且只想保护一个实例时,这是一个有用的工具,但是,在这种情况下,我不会使用一个@MarkKnol单例类非常有用。MinionManager/Totalta Manager类可以是单例。有了这个类,它可以控制仆从,并在更多的仆从被建造/摧毁时为每一个仆从更新目标。。。如果你想延长炮塔,你就完蛋了。一个今天就是一个明天。我不推荐单件,这是一个有问题的设计模式。数据管理器中的静态实例是可以的,但不要强迫只拥有一个,您可能需要变化或倍数。这就是我所说的。如果他只有一个炮塔,他应该使用它。如果没有的话,应该有一个StageManager或其他东西,它引用了所有的塔楼和仆从。我会有多个塔楼,而且多个仆从“waves”单体也有它们的用途,当你有多个开发人员在一个项目上,并且只想保护一个实例时,这是一个有用的工具,但是,在这种情况下,我不会使用一个@MarkKnol单例类非常有用。MinionManager/Totalta Manager类可以是单例。有了这个类,它可以控制仆从,并在更多的仆从被建造/摧毁时更新每一个仆从的目标。在给出有用的答案之前,你需要更多关于你的程序应该如何工作的信息。在你的游戏中只有一个米尼翁和炮塔实例吗?或者每个都有多个实例?它们之间的关系如何?(炮塔中的一颗子弹击中了一个仆从?)我有三个炮塔,三个仆从同时出现,每个都朝着各自的炮塔前进,你使用getDistance功能的目的是什么?Distance将用于告诉仆从何时停止和开始攻击塔楼。在给出有用的答案之前,需要更多关于你的程序应该如何工作的信息。在你的游戏中只有一个米尼翁和炮塔实例吗?或者每个都有多个实例?它们之间的关系如何?(炮塔中的一颗子弹击中了一个仆从?)我有三个炮塔,三批仆从同时出现,每一批都朝着各自的炮塔前进。另外,你使用getDistance功能做什么?Distance将用来告诉仆从何时停止并开始攻击塔。这基本上就是我使用的,我需要的是,当我在仆从类中时,如何获得炮塔对象?所以我可以比较距离。这基本上就是我正在使用的,我需要的是当我在仆从类中时如何获得炮塔对象?所以我可以比较一下距离。啊,我知道你在那里做了什么。如果你听说过“传奇联盟”这个游戏,基本上我正在制作一个flash版本。我没听说过,但我对塔防类型的游戏很熟悉。如果你的仆从是不断移动的,并且需要检查距离,那么你应该在它自己的范围内保留尽可能多的地方所需的东西(为了效率,以及