Gabe Dougherty
Gabe Dougherty

Reputation: 25

Trying to essentially push details through classes

I am working on a game that creates three circles" red, green, and blue who have 3,2,1 health respectively. They get removed from the stage after their health reaches 0 and it is decremented by 1 per click. I am using a Main.mxml file then I have a Target.as file, as well as RedTarget.as, GreenTarget.as, and BlueTarget.as. My question is that I would like to set everything up in my Target.as file, then push the details such as color, health, and if they are dead or not through those functions. I am having trouble doing that though because I am not sure what I would need in the Target.as and then what I would need to code in each of the colored target files. Here is my Target.as file:

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

}

}
}

and then my RedTarget.as file, which is the same as blue and green, except for that they are labeled as such in the variables:

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
}

}
}

Any help on the issue would be great. I have been trying to figure it out throughout the day but have not figured it out.

Upvotes: 0

Views: 47

Answers (1)

Scott
Scott

Reputation: 948

Are you just asking how to pass variables in when you create the Target object?

public class Target extends Image {

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

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

    }

}

Then you instantiate each target like this:

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

etc...

Upvotes: 2

Related Questions