2012-03-28 58 views
1

我一直在試圖爲小遊戲製作一個分數計數器,用戶在舞臺上點擊一個按鈕,每次用戶按下按鈕時分數增加10,但是我無法獲得得分以顯示在動態文本字段上。我究竟做錯了什麼?動作腳本3.0分數計數器

var score:uint; 
//scoreCounter is the instance name of the dynamic text box 

    function updateScore():void{ 
    score += 10; 
    scoreCounter.text = score.toString(); 
} 
+0

您需要顯示其餘代碼,包括按鈕事件回調。 – 2012-03-28 19:00:20

+0

//事件收聽者 var score:uint; 函數updateScore():void { \t score + = 10; scoreCounter.text = score.toString();函數stonePoint(e:MouseEvent):void { if(MouseEvent.MOUSE_DOWN){ updateScore(); scoreCounter.text = score.toString(); }} // 功能 hatPoint(E:MouseEvent)方法:無效{ 如果(的MouseEvent.MOUSE_DOWN){ updateScore(); scoreCounter.text = score.toString(); } } – 2012-03-28 19:16:01

+0

對不起,我不能回答我自己的問題,我需要至少100個聲望或等待8小時 – 2012-03-28 19:17:29

回答

2

很難分辨出問題所在,因爲您確實沒有發佈足夠的代碼。確保你的方法看起來像這樣。此代碼已經過測試並可正常工作

package{ 
import flash.display.Sprite; 
import flash.events.MouseEvent; 
import flash.text.TextField; 

public class Test extends Sprite{ 
    private var txt:TextField; 
    private var score:uint = 0; 
    public function Test() 
    { 
     //creating my text field 
     txt = new TextField(); 
     addChild(txt); 

     //drawing a black rectangle to use as a "button" 
     var spt:Sprite = new Sprite(); 
     spt.graphics.beginFill(0x000000); 
     spt.graphics.drawRect(0, 0, 50, 50); 
     spt.graphics.endFill(); 
     addChild(spt); 
     spt.y = 50; 

     //adding the click event to the "button" 
     spt.addEventListener(MouseEvent.CLICK, handleClick); 

    } 

    protected function handleClick(event:MouseEvent):void 
    { 
     //adding 10 to score 
     score += 10; 
     //setting the txt text field to score 
     txt.text = score.toString(); 


    } 
} 
} 
2

請確保您的文本字段中包含嵌入字體。以測試其工作是否可以完成:

scoreCounter.embedFonts = false; 

看看它是否顯示。