2011-10-11 169 views
0

我正在尋找一種方法來調用在ActionScript 2.0中的不同框架中定義的函數。細節如下:如何使用ActionScript 2.0從不同的框架調用函數?

如在幀1中的按鈕事件,我有:

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); 

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幀,我已在該幀本身上下面的腳本

出於某種原因,第2幀上的代碼自行工作,但從另一個框架的列表中選擇索引不起作用。換句話說,我無法在第2幀中定義selectVideo(),但在第2幀中定義它。

程序的目的,就像暗示的,指的是來自不同幀的列表中的某個視頻。整個代碼工作沒有錯誤,我只是無法從列表中選擇一個視頻,並播放它,如果它最初在不同的先前幀。

任何想法,建議或解決方案,高度讚賞! 在此先感謝您的幫助!

+0

@Dairo編輯帖子時,請修正所有內容,而不只是一個錯字。 –

回答

1

第一:時間線腳本是一團糟,請嘗試使用類。

你可以嘗試設置一個變量_root。像_root.nextIndex = 5;,然後徹底改變幀2

on (release) 
{ 
    _root.nextIndex = 5; 
    _root.gotoAndStop(2); //Going to Frame 2. 
} 

然後呼叫功能,它傳遞_root.nextIndex或刪除功能,只是做

vidList.selectedIndex = _root.nextIndex; 

任何函數之外。

+0

您的筆記受到高度讚賞。感謝您提供的解決方案。它現在正在完美工作。這是我在整個應用程序中唯一的時間線腳本。上面的代碼僅僅是爲了這個問題。我的源代碼與此非常不同。事實上,使用類是至關重要的。 – CompilingCyborg

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的意見,並儘量限制腳本在時間軸上的不同幀上添加,因爲它會變得非常混亂。

+0

Pkyeck的代碼正在工作,但你的方式導致了我以前面臨的同樣的問題。但是,這是由於我編寫我的小應用程序的方式。但是,你的概念是完全正確的。 (+1幫助你) – CompilingCyborg

相關問題