我有3个课程。
一个是具有我的功能的Global类:
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
而另一个名为:Turret
在我呼叫的Minion
课程中:Global.getDistance
将参数设置为:Global.getDistance(this, ?????)
如何从Turret类获得最终参数的Turret对象?
答案 0 :(得分:0)
如果你的游戏中只有一个炮塔,你可以使用单身设计模式。
private static var instance:Turret = null;
public static function getInstance()
{
if(instance === NULL)
instance = new Turret();
return instance;
}
所以你可以调用Turret.getInstance()并使用你的Turret对象。希望能帮助到你。如果你有一个以上的炮塔,你应该有一个带有阵列或矢量的游戏类,并且你的所有炮塔。
答案 1 :(得分:0)
我不知道你究竟需要什么,第二个参数的类型?由于你处理全局函数,我建议只使用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 ) );
}
答案 2 :(得分:0)
如果每个小兵都有一个目标炮塔,那么你应该对Minion
级内的炮塔有一个参考。应该不需要静态函数来获得距离(除非它用于除了小兵/炮塔关系之外的其他东西)。
为了让你的炮塔了解所有小兵(决定攻击哪个小兵),一个好方法是将它们全部存储在一个静态可评估的矢量(数组)中。
以下是你的Minion课程的样本:
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;
}
}
当你创建一个新的Minion - new Minion(turretInstance)
时,它会传入适当的炮塔,并且有一个静态可访问的数组,可以在任何给定时间保存显示列表中的所有爪子。它还增加了一个伤害功能
对于攻击的炮塔,你需要扫描所有爪牙的阵列,并确定哪个近距离攻击,要么攻击所有(如果是炮塔的类型),要么选择一个(通常是最近的一个)攻击。
炮塔类:
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);
}
}