开发者

Incrementing a global variable in a function which is called multiple times

I am trying to make a simple mp3 player using flash. The songs are loaded using an XML file which contains the song list. The following code is inserted into a new keyframe.

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 T开发者_JAVA技巧he 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);

The problem over here is in the nextSong() function. I am unable to preserve the value of currentIndex. Although I am incrementing currentIndex every time the function is called, but the value remains unchanged. Please suggest ...


Since your variable is defined in the keyframe, it would re-initiated everytime the keyframe is hit. I bet moving the definition outside the keyframe scope would help. For example, to root of the Flash (although not too good in terms of OOP)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜