apache-flex - 在多次调用的函数中增加全局变量

标签 apache-flex flash actionscript-3 audio mp3

我正在尝试使用 Flash 制作一个简单的 mp3 播放器。歌曲是使用包含歌曲列表的 XML 文件加载的。下面的代码被插入到一个新的关键帧中。

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLRequest; 
import flash.xml.*;

/* Defining The Elements and Variables of the Music Player */
var MusicLoading:URLRequest;                        
var music:Sound = new Sound();
var sc:SoundChannel;
var currentSound:Sound = music;
var CurrentPos:Number;                              
var xml:XML;
var songlist:XMLList;                               
var currentIndex:uint;                              
var XMLLoader:URLLoader = new URLLoader();          
/* ------------------------------------------------------ */


/* --------------------Load songs from the list-------------------- */
function success(e:Event):void 
{
    xml = new XML(e.target.data);
    songlist = xml.song;    //For adding a new song in XML File.
    MusicLoading = new URLRequest(songlist[0].file);
    music.load(MusicLoading);
    currentIndex = 0;
}
//If XML File Load successfully, it will be voided this actions:
XMLLoader.addEventListener(Event.COMPLETE, success);

//The Name of the XML File.
XMLLoader.load(new URLRequest("playlist.xml"));

//Play The Song
function playSong(e:Event):void 
{
    if(sc != null)
        sc.stop();

    sc = currentSound.play(CurrentPos);
}

//Pause The Song
function pauseSong(e:Event):void 
{
    CurrentPos = sc.position;
    sc.stop();
}


//Next Song Functions
function nextSong(e:Event):void 
{
    sc.stop();

trace(currentIndex + " ");

    currentIndex = currentIndex + 1;

    trace(currentIndex);

    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);
}

PauseBtn.addEventListener(MouseEvent.CLICK, pauseSong);
PlayBtn.addEventListener(MouseEvent.CLICK, playSong);
NextBtn.addEventListener(MouseEvent.CLICK, nextSong);

这里的问题在于 nextSong() 函数。我无法保留 currentIndex 的值。虽然每次调用函数时我都会增加 currentIndex,但值保持不变。请建议...

最佳答案

由于您的变量是在关键帧中定义的,因此每次命中关键帧时都会重新启动它。我敢打赌,将定义移到关键帧范围之外会有所帮助。例如,到 Flash 的根目录(虽然在 OOP 方面不太好)

关于apache-flex - 在多次调用的函数中增加全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063317/

相关文章:

css - mx :panel 上的 Flex 3 css 样式

python - BlazeDS Livecycle Data Services 有什么作用,而 PyAMF 或 RubyAMF 之类的东西不能做什么?

javascript - 有什么方法可以测试移动浏览器是否支持使用网络技术的 Flash?

actionscript-3 - 将类作为参数传递,然后从类实例化

apache-flex - 如何在 Flex/AS3 中使用基于 cookie 的 session 来测试服务

apache-flex - 在 Flex 和 AIR 之间共享代码

actionscript-3 - 如何在 Flash Builder 中使用 TLFTextField?

android - 在 air 应用程序中加载嵌入式 swf 文件的问题

apache-flex - 如何在Flash CS4中管理带有链接类的库符号以在Flash Builder 4中进行编译/调试?

android - 空气 : Which event is triggered when barcode reader scanning?