2010-10-31 45 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

相關問題