2010-10-31 27 views
0

變量currentIndex是全局聲明的,並用一個特定的值「0」初始化。如何保存每次調用函數時遞增的currentIndex的值?在給定的代碼中,每次調用該函數時,都會重新初始化該值。如何增加函數內的全局變量?

 
function nextSong(e:Event):void 
{ 
     sc.stop(); 

currentIndex = currentIndex + 1; 

var nextSongFunc:URLRequest = new URLRequest(songlist[currentIndex].file); 
var nextTitle:Sound = new Sound(); 
nextTitle.load(nextSongFunc); 
currentSound = nextTitle; 
sc = currentSound.play(); 

sc.addEventListener(Event.SOUND_COMPLETE, nextSong); 
} 

NextBtn.addEventListener(MouseEvent.CLICK, nextSong); 

回答

1

您需要聲明變量功能。你如何做到這一點取決於上下文。這個函數在哪裏被定義?在Flash的時間軸上的「動作窗口」中?或者Flex裏面的<script>塊?或者別的地方?

它看起來像你在Flash工具中的操作窗口。如果是這樣,那麼就做這樣的:

var currentIndex:int = 0; 

function nextSong(e:Event):void { 
    sc.stop(); 
    currentIndex = currentIndex + 1; 

    var nextSongFunc:URLRequest = new URLRequest(songlist[currentIndex].file); 
    var nextTitle:Sound = new Sound(); 
    nextTitle.load(nextSongFunc); 
    currentSound = nextTitle; 
    sc = currentSound.play(); 

    sc.addEventListener(Event.SOUND_COMPLETE, nextSong); 
} 

NextBtn.addEventListener(MouseEvent.CLICK, nextSong); 

如果不行,讓我知道一些細節,我們將整理出來。

0

如果您使用的是Flash CS,則應該利用DocumentClass。在這種情況下,你可以將currentIndex定義爲一個私有變量,並且它會在你的函數中增加/減少。

這是一個比在動作面板中編寫代碼更好的方法,它允許更多的靈活性,並且由於與幀相關的代碼而不會遇到問題。