0

「getURL」を含む Actionscript ループを書きたいと思います。しかし、私が見ることができることから、g​​etURL は変数名の連結を許可していませんか?

ムービークリップ名を値として持つ変数 textholder0、textholder1、textholder2 と、Web サイトのアドレスを値として持つ link0、link1、link2 があります。

this["textholder" + 0].onRelease を使用できますが、 getURL("link"+ 0) は「未定義」を返します

textholder0.onRelease = function()
{   
    getURL(link0);
}

textholder1.onRelease = function()
{
    getURL(link1);
}
textholder2.onRelease = function()
{
    getURL(link2);
}

上記のループを作成できるように、これを行う方法はありますか?


これがテストです。残念ながら、それでも URL に「undefined/」が表示されます。簡単にするために、インスタンス textholder0、textholder1、textholder2 を含む 3 つのムービー クリップを作成しました。メイン タイムラインにループを配置します。

var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];

for(var i:Number=0; i<links.length; i++){
    this["textholder" + i].linkURL = links[i];
    this["textholder" + i].onRelease = function() { 
        getURL(linkURL); 
    }    
}

これはデバッガウィンドウからの出力です

Variable _level0.links = [object #1, class 'Array'] [
    0:"http://www.google.ca",
    1:"http://www.google.com",
    2:"http://www.google.ru"   ] 
Variable _level0.i = 3 
Movie Clip: Target="_level0.textholder0" 
Variable _level0.textholder0.linkURL = "http://www.google.ca" 
Variable _level0.textholder0.onRelease = [function 'onRelease'] 
Movie Clip: Target="_level0.textholder1" 
Variable _level0.textholder1.linkURL = "http://www.google.com" 
Variable _level0.textholder1.onRelease = [function 'onRelease'] 
Movie Clip: Target="_level0.textholder2" 
Variable _level0.textholder2.linkURL = "http://www.google.ru" 
Variable _level0.textholder2.onRelease = [function 'onRelease']

ループ内で onRelease をまったく使用できないと考え始めています。

4

2 に答える 2

0

さて、ここでのループの問題は、ボタンがクリックされる前に「i」変数がインクリメントされていたことだと思います。

http://www.senocular.com/flash/tutorials/faq/#loopfunctions

Senocular.com は、「関数の作成時にその値を表す新しい一意の変数を定義し、関数がその値を参照するようにする必要がある」と述べています。

したがって、ループは次のようになります

var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];

var curr_button;

for(var i=0; i<=links.length; i++){
    curr_button = this["textholder"+i];
    //note creation of an extra variable "num" below to store the temp number 
    curr_button.num = i;
    curr_button.onRelease = function() { 
        getURL(links[this.num]); 
    }    
}
于 2013-09-05T19:50:23.630 に答える