Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex 递增函数中被多次调用的全局变量_Apache Flex_Flash_Actionscript 3_Audio_Mp3 - Fatal编程技术网

Apache flex 递增函数中被多次调用的全局变量

Apache flex 递增函数中被多次调用的全局变量,apache-flex,flash,actionscript-3,audio,mp3,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

我正在尝试使用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,但该值保持不变。请建议…

因为您的变量是在关键帧中定义的,所以它将在每次点击关键帧时重新启动。我打赌将定义移到关键帧范围之外会有所帮助。例如,尽管在OOP方面不是很好,但是我不知道你的Flash项目是什么样子,也没有简单的方法发布,我不确定这是否是问题所在。但是你提到上面的代码是附加到关键帧的,对吗?Flash非常有趣,它可以用几乎完全的OOP方式编程,也可以用脚本方式编程。对于附加到关键帧的脚本,每次播放关键帧时都会执行该脚本。即,您的var currentIndex:uint;每次执行并重新初始化。一个快速破解方法是使用_rootglobal var:_root是flash应用程序中的每个帧/对象都可以访问的全局变量,该值将在应用程序的整个生命周期中保持不变。正如我之前提到的,这不是最好的方法,当然你可以有很多其他的方法来实现,但这是另一个大问题,包括重新设计或重构你的程序。我认为这不应该被归类为Flex,因为你似乎根本没有处理Flex框架。