2013-03-04 59 views
1

我想知道如何使用動作在點擊時更改變量。在點擊動作時更改變量

我:

 private var test:int = 0; 

    public function thisIsTest():void{ 
     test = test + 1; 
     } 

    <mx:Image left="10" bottom="10" source="@Embed(source='Assets/blabla.png')" click="thisIsTest()" buttonMode="true"/> 

我想1我每次點擊按鈕「布拉布拉」添加到變量測試。

問題是它只能工作一次。

謝謝您的幫助

+0

什麼是輸出當您添加' trace(test)''在'test = test + 1'這行之後? – James 2013-03-05 12:12:51

回答

2

最簡單的方法是使用一個MouseEvent監聽器。您將聆聽到任何你想被點擊,並告訴該功能時,觸發一個事件,執行監聽器:

var test:int = 0; 

image.addEventListener(MouseEvent.CLICK, thisIsTest); 
// Will 'listen' for mouse clicks on image and execute thisIsTest when a click happens 

public function thisIsTest(e:MouseEvent):void 
{ 
    test = test + 1; 
    trace(test); 
} 

// Output on subsequent clicks 
// 1 
// 2 
// 3 
// 4 

這是否意味着像你想監聽連接需要可顯示對象,就像一個精靈或動畫片段,但如果你使用Flash,這應該不成問題。

編輯:註釋中註明的進一步行動。

將圖像導入到Flash,並用它來生成SpriteMovieclip,並給它一個ActionScript鏈路ID(如類名):

Import image into library

// Add the image to the stage 
var img:myImage = new myImage(); 
addChild(img); 

// Assign the mouse event listener to the image 
img.addEventListener(MouseEvent.CLICK, thisIsTest); 
+0

謝謝!我的想法是每次點擊時將流的緩衝區增加一秒。所以我應該這樣做:public function thisIsTest(e:MouseEvent):void { test = test + 1; (測試); trace(test); netStream.bufferTime = test; }? – Marcolac 2013-03-04 15:33:07

+0

我不熟悉流緩衝區,但如果你的語法是正確的,那麼是'bufferTime'會每次點擊增加1。您可以將「trace(test)'行取出,只需將該變量輸出到控制檯,以便您可以看到它自己在增加。 – 2013-03-04 15:49:55

+0

非常感謝你!我會嘗試你的解決方案並將其標記! – Marcolac 2013-03-04 15:55:05