2010-10-31 48 views
0

我正在试图制作一个简单的使用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,但值保持不变。请建议...

+0

我不认为这应该归类为Flex,因为您似乎根本就没有处理Flex框架。 – JeffryHouser 2010-10-31 15:17:53

回答

0

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

+0

请你详细说明如何做到这一点? – 2010-10-31 13:58:53

+0

我是新来的闪光灯...我现在很笨... – 2010-10-31 13:59:15

+0

由于我不知道你的闪光灯项目是如何(并没有简单的方式发布),我不太确定这是否是问题所在。但是你提到你的上面的代码被附加到了关键帧上,对吧? Flash非常有趣,它可以以几乎完全的OOP方式编程,也可以脚本方式。对于附加到关键帧的脚本,每次播放关键帧都会执行该脚本。即你的“var currentIndex:uint;”每次都执行并重新初始化。快速入门使用“_root”全局变量:http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary633.html – xandy 2010-10-31 14:04:40

相关问题