2011-11-24 85 views
0

所以我創造一個遊戲,我想添加的是一個不斷增加的計數器,直到玩家輸掉比賽。Flash遊戲得分不起作用AS3,請幫忙?

創建我的分數類,它看起來像這樣:

package 
{ 

    import flash.display.MovieClip; 
    import flash.display.Stage; 
    import flash.text.TextField; 
    import flash.events.Event; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 

    public class Score extends MovieClip 
    { 
     public var second:Number = 0; 
     public var timer:Timer = new Timer(10); 
     private var stageRef:Stage; 


     public function Score(stageRef:Stage) 
     { 
      x = 537.95; 
      y = 31.35; 
      this.stageRef = stageRef; 
      timer.addEventListener(TimerEvent.TIMER, clock); 
      timer.start(); 

     } 

     function clock(evt:TimerEvent):void 
     { 
      second += 1; 
      scoreDisplay.text = String("Score: " +second); 
     } 

    } 
} 

,這是我的引擎類,它添加到舞臺:

package { 
//list of our imports these are classes we need in order to 
//run our application. 
import flash.display.MovieClip; 
import flash.display.Stage; 
import flash.events.Event; 

public class Engine extends MovieClip{ 

    private var enemyList:Array = new Array(); 
    private var ourBoat:Boat; 
    private var score:Score; 


    public function Engine() : void{ 
     //create an object of our ship from the Ship class 
     ourBoat = new Boat(stage); 
     score = new Score(stage); 
     //add it to the display list 
     stage.addChild(ourBoat); 
     stage.addChild(score); 

這樣在舞臺上創建一個定時器並不斷增加,但是當我編譯時,我沒有得到任何錯誤,並且由於某種原因我的timmer不起作用,它只是顯示隨機數字,請幫助!如果有更好的方法,請啓發我。

回答

0

我假設scoreDisplay是舞臺上的命名對象。您可能會發現將trace()添加到每個調用的函數中的腳本是有用的。這樣你就可以看到哪些被正確調用。例如trace(「Engine Instantiated。」);和跟蹤(「收到計時器事件」);會告訴你的類是否正確實例化。如果是這樣並且觸發器不工作,那麼你知道你的問題在這兩點之間。然後朝代碼執行的中間努力,直到找到問題。

您也可以添加一個事件監聽器到舞臺上以輸入框架事件並使用它來觸發您的計數功能。這個事件總是廣播,所以使用應該使用比添加定時器更少的資源。

0

首先,您不需要將舞臺類傳遞給MovieClip的孩子,一旦他們被添加到舞臺上,就可以使用this.stage屬性訪問舞臺。

二,Timer類延遲參數被描述爲以下文檔中:

延遲:Number - 計時器事件間的延遲,以毫秒爲單位。建議不要使用延遲時間低於20毫秒的 。定時器頻率 限制爲每秒60幀,這意味着延遲時間低於16.60毫秒會導致運行時問題。

因此,如果你真的跟蹤秒,你的計時器應該是:

public var timer:Timer = new Timer(1000); 

編輯:

這是我將如何實現你的註釋中描述的比分:

public class Score extends MovieClip 
{ 
    public var second:Number = 0; 

    var pointsPerSecond : Number = 10; 

    private var stageRef:Stage; 


    public function Score(stageRef:Stage) 
    { 
     x = 537.95; 
     y = 31.35; 
     this.stageRef = stageRef; 
     this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
    } 

    function clock(evt:Event):void 
    { 
     second += pointsPerSecond/stage.frameRate; // Note that if the game is running slow (flash cant keep the frameRate you asked for), the score will also grow slowly 
     scoreDisplay.text = String("Score: " +second); 
    } 
} 
+0

那麼我怎麼能夠做一個計時器,以我在例子中所做的速度來計算呢? – BoneStarr

+0

你不會,你可以做的是驗證自上次調用以來經過了多少時間並使用它。爲什麼你想要一些東西增加得如此之快呢?它不像用戶能夠看到它(文本將只根據frameRate刷新)。 – felipemaia

+0

我想要一個類似於這樣的計時器:http://www.youtube.com/watch?v=Cx_r2ga7ipQ&feature=related – BoneStarr

0

你確定scoreDisplay夠大嗎?你的號碼會每秒增加100,如果你的文本字段只有2個字符長,你會看到隨機數。

+0

是的,我確實使它更長,仍然得到同樣的問題 – BoneStarr