I am looking for a way to call a function defined in a different frame in ActionScript 2.0. The details are as follows:
As a button event in frame 1, I have:
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.
}
On Frame 2, I have the following script on the frame itself:
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 开发者_StackOverflow社区listHandler:Object = new Object();
listHandler.change = function(evt:Object)
{
vid.load(vidList.getItemAt(vidList.selectedIndex).data);
};
vidList.addEventListener("change",listHandler);
For some reason, the code on frame 2 is working by itself but selecting an index from the list from another frame is not working. In other words, I am not able to successfully call selectVideo()
from frame 1 while it is defined in frame 2.
The objective of the program, as implied, is referring to a certain video from the list from a different frame. The whole code is working without errors, I'm just not able to select a video from the list and play it if it was initially in a different previous frame.
Any ideas, suggestions or solutions are highly appreciated! Thanks in advance for your help!
first: timeline scripting is a mess, try using classes instead.
you could try and set a variable on _root
. something like _root.nextIndex = 5;
and then change the frame to 2.
on (release)
{
_root.nextIndex = 5;
_root.gotoAndStop(2); //Going to Frame 2.
}
then either call the function and pass it the _root.nextIndex
or remove the function completely and just do the
vidList.selectedIndex = _root.nextIndex;
outside of any function.
This is the quick fix for your code (you were lacking the _root to access the main timeline on the function call):
on (release)
{
_root.gotoAndStop(2); //Going to Frame 2.
_root.selectVideo(5); //Calling a function defined on the timeline in Frame 2.
}
But I would try to follow pkyeck's advice and try to limit the script added on different frames in the timeline, as it can get really messy.
精彩评论