2010-10-31 28 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定义为一个私有变量,并且它会在你的函数中增加/减少。

这是一个比在动作面板中编写代码更好的方法,它允许更多的灵活性,并且由于与帧相关的代码而不会遇到问题。