2013-02-07 42 views
0

我正在製作一個遊戲,創建三個分別爲「紅色,綠色和藍色」的遊戲,分別擁有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 
} 

} 
} 

在這個問題上的任何幫助將是巨大的。我一直試圖弄清楚整個一天,但還沒有弄清楚。

+0

這裏發表回覆:【如何使用類時創建圖形? AS3](http://stackoverflow.com/questions/14756596/how-to-create-graphics-when-using-classes-as3) – khailcs

回答

2

你只是問在創建目標對象時如何傳遞變量?

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

等等