试图通过类推送细节

时间:2013-02-07 22:46:32

标签: actionscript-3 flex class push

我正在制作一个游戏,创造三个圈子“红色,绿色和蓝色,分别有3,2,1点生命值。他们在健康状态达到0后从舞台上移除,每次点击减少1次。我正在使用Main.mxml文件,然后我有一个Target.as文件,以及RedTarget.as,GreenTarget.as和BlueTarget.as。我的问题是我想在Target.as文件中设置所有内容然后通过这些功能推送颜色,健康等细节,如果它们已经死了或者没有。我无法做到这一点,因为我不确定在Target.as中我需要什么,然后我需要什么每个彩色目标文件中的代码。 这是我的Target.as文件:

package com.multiClicker {
//import the needed classes
import flash.display.Shape;
import flash.events.MouseEvent;

import spark.components.Image;


public class Target extends Image {

public function Target() {

    //add event listeners
    this.addEventListener(MouseEvent.CLICK, onClick);

}

//sets the hp of the target
public function hp():Number { 
    return hp;
}

//get function that returns false if alpha is <= 0
public function dead():Boolean {

    if(alpha <= 0){
        return false;
    }

    return true;
}

//subtracts one from targets HP when clicked
public function onClick(e:MouseEvent = null):void {


    //subtracts one from hp each click
    hp --;

    if(hp <=0) {
        this.addEventListener(onEnterFrame);
    }

}

//subtracts .1 from the classes alpha
public function onEnterFrame():void{
    this.alpha =- .1;


}

//draws the target
public function drawTarget(color):void {

    var circle:Shape = new Shape();

    circle.graphics.beginFill(color);
    circle.graphics.drawCircle(0,0,30);

}

}
}

然后是我的RedTarget.as文件,它与蓝色和绿色相同,除了它们在变量中标记为:

package com.multiClicker {
import flash.events.MouseEvent;
public class RedTarget extends Target{

private var redHP:Number = 3;
private var redDead:Boolean = false;
private var redColor:String = "red";

public function RedTarget()
{

    redHP = hp;
    redDead = dead;
    redColor = color;

    //include the super function
    super();
}

//subtracts one from targets HP when clicked
override public function onClick(e:MouseEvent=null):void {

    super.onClick(e);

    //push all to super
}

}
}

对此问题的任何帮助都会很棒。我一直试图弄清楚它,但还没想出来。

1 个答案:

答案 0 :(得分:2)

您是否只是在询问如何在创建Target对象时传入变量?

public class Target extends Image {

    public function Target(hp:Number, dead:Boolean, color:String) {

        this.hp = hp;
        this.dead = dead;
        this.color = color;

    }

}

然后你像这样实例化每个目标:

var redTarget:Target = new Target(3, false, "red");
var greenTarget:Target = new Target(2, false, "green");

等...