我正在寻找一种方法来调用ActionScript 2.0中不同框架中定义的函数。详情如下:
作为第1帧中的按钮事件,我有:
on (release)
{
_root.gotoAndStop(2); //Going to Frame 2.
selectVideo(5); //Calling a function defined in Frame 2.
//Calling this function is not working in the way i wrote it above.
}
在第2帧,我在框架上有以下脚本:
var vlist:XML = new XML(); //Creating an XML variable
vlist.ignoreWhite = true; //Ignoring the white spaces in the xml
vlist.load("videoList.xml"); //Loading a list of videos from the xml.
vlist.onLoad = function()
{
var videos:Array = this.firstChild.childNodes;
for (i = 0; i < videos.length; i++)
{
vidList.addItem({label:videos[i].attributes.desc, data:videos[i].attributes.url});
}
vid.load(vidList.getItemAt(0).data);
vidList.selectedIndex = 0;
//Notes:
//vid: is an instance of (FLVPlayback) component.
//vidList: is an instance of a (List) component.
};
function selectVideo(index:Number)
{
//Here is the function that i want to call from frame 1 but it is not getting called.
vidList.selectedIndex = index;
}
var listHandler:Object = new Object();
listHandler.change = function(evt:Object)
{
vid.load(vidList.getItemAt(vidList.selectedIndex).data);
};
vidList.addEventListener("change",listHandler);
由于某种原因,第2帧上的代码本身是有效的,但从另一帧的列表中选择索引是行不通的。换句话说,在第2帧中定义时,我无法从第1帧成功调用selectVideo()
。
该节目的目的,如暗示,是指来自不同帧的列表中的某个视频。整个代码工作没有错误,我只是无法从列表中选择一个视频,如果它最初位于不同的前一帧中,则播放它。
任何想法,建议或解决方案都非常感谢! 在此先感谢您的帮助!
答案 0 :(得分:1)
首先:时间轴脚本是一团糟,请尝试使用类。
您可以尝试在_root
上设置变量。像_root.nextIndex = 5;
这样的东西,然后将框架更改为2.
on (release)
{
_root.nextIndex = 5;
_root.gotoAndStop(2); //Going to Frame 2.
}
然后调用该函数并将其传递给_root.nextIndex
或完全删除该函数并执行
vidList.selectedIndex = _root.nextIndex;
在任何功能之外。
答案 1 :(得分:1)
这是您的代码的快速修复(您缺少_root来访问函数调用的主时间轴):
on (release)
{
_root.gotoAndStop(2); //Going to Frame 2.
_root.selectVideo(5); //Calling a function defined on the timeline in Frame 2.
}
但我会尝试遵循pkyeck的建议并尝试限制在时间轴中添加到不同帧上的脚本,因为它可能会变得非常混乱。