3

我不是代碼天才,而是行動腳本的粉絲。 你能幫我:AS3將變量參數傳遞給一個通用的函數Menu/SubItems

我有一個函數,取決於所選對象,將調用事件監聽器到一組已經在舞臺上的「子項目」(我想重用這個子項目與改變參數點擊,而不是創建幾個實例和幾個代碼)。

所以對於每個選定的「案例」我要diferent變量傳遞給那些「分項目」,像這樣:

function fooMenu(event:MouseEvent):void { 
    switch (event.currentTarget.name) 
    { 
     case "btUa1" : 
      trace(event.currentTarget.name); 
      // a bunch of code goes here 
      //(just cleaned to easy the view) 
      /* 
      HELP HERE <-- 
      here is a way to pass the variables to those subitems 
      */ 

      break; 
    } 
} 

function fooSub(event:MouseEvent):void 
{ 
    trace(event.target.data); 
    trace(event.currentTarget.name); 
    // HELP PLEASE <-> How can I access the variables that I need here ? 
} 

btUa1.addEventListener(MouseEvent.CLICK, fooMenu); 
btUa2.addEventListener(MouseEvent.CLICK, fooMenu); 

btTextos.addEventListener(MouseEvent.CLICK, fooSub); 
btLegislacao.addEventListener(MouseEvent.CLICK, fooSub); 

任何幫助我嗎? 非常感謝。 :)

回答

1

(我不知道我得到了你的問題吧,我也沒有在AS3開發了一段時間。)

如果你想簡單地將在一個被稱爲參數創建功能點擊(或其他事件),你可以簡單地使用:

btUa1.addEventListener(MouseEvent.CLICK, function() { 
    fooMenu(parameters); 
}); 

btUa2.addEventListener(MouseEvent.CLICK, function() { 
    fooMenu(other_parameters) 
}): 

public function fooMenu(...rest):void { 
    for(var i:uint = 0; i < rest.length; i++) 
    { 
    // creating elements 
    } 
} 

如果你想調用分配給別的東西,你可以使用DispatchEvent

btnTextos.dispatchEvent(new MouseEvent(MouseEvent.CLICK)) 

事件監聽器

請記住,您不能使用btTextos.addEventListener(MouseEvent.CLICK,carregaConteudo(「jocasta」));因爲你通過,同時添加事件監聽第二個參數將被視爲功能本身 - 有使用的addEventListener兩個專有方式:

1:

function doSomething(event:MouseEvent):void 
{ 
    // function code 
} 
element.addEventListener(MouseEvent.CLICK, doSomething); //notice no brackets 

2:

element.addEventListener(MouseEvent.CLICK, function() { // function code }); 

所以:

function fooSub(event:MouseEvent, bla:String):void 
{ 
trace(event.currentTarget.name+" - "+bla); 
// bla would be a clip name. 
} 

codebtTextos.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) { fooSub(e, "jocasta") }); 

或者嘗試這樣的事情,如果你想成爲動態生成的內容:

btUa1.addEventListener(MouseEvent.CLICK, function() { 
    createMenu(1); 
}); 

btUa2.addEventListener(MouseEvent.CLICK, function() { 
    createMenu(2); 
}); 

function createMenu(id):void 
{ 
    // Switching submenu elements 
    switch (id) 
    { 
     case 1: 
      createSubmenu([myFunc1, myFunc2, myFunc3]); // dynamically creating submenus in case you need more of them than u already have 
      break; 
     case 2: 
      createSubmenu([myFunc4, myFunc5, myFunc6, myFunc7]); 
      break; 
     default: 
      [ and so on ..] 
    } 
} 

function createSubmenu(...rest):void { 
    for (var i:uint = 0; i < rest.length; i++) 
    { 
     var mc:SubItem = new SubItem(); // Subitem should be an MovieClip in library exported for ActionScript 
     mc.addEventListener(MouseEvent.CLICK, rest[i] as function) 
     mc.x = i * 100; 
     mc.y = 0; 
     this.addChild(mc); 
    } 
} 
+0

謝謝! 這裏清除我需要的東西: 在我的階段我有6個項目 - 對象和3個子項目 - 對象 當點擊項目(fooMenu)我重新排列這3個子項目,並需要給這3個新的點擊事件參數點擊時他們可以調用新的剪輯)。 我想你的解釋,但並沒有得到它的權利......在這裏: 'code' 功能fooSub(事件:的MouseEvent,BLA:字符串):無效 { \t跟蹤(event.currentTarget.name +」 - 「+ BLA); // bla將是一個剪輯名稱。 } in fooMenu case: 'code'btTextos.addEventListener(MouseEvent.CLICK,fooSub(「jocasta」)); – John 2012-07-30 17:48:12

+0

whish我可以投票了(呵呵太新手了) – John 2012-07-30 17:58:56

+0

通知編輯答案;)希望它有幫助。如果你想在你的問題下面添加一些信息到你的問題使用評論,或者編輯它。 – mswiszcz 2012-07-30 18:18:23

0

你的問題是相當模糊的;你想「通過」什麼「變量」? 「將變量傳遞給子項目」是什麼意思?通常「傳遞」是指調用一個函數。

如果你可以更具體地說明你想要做什麼會有幫助。與此同時,這裏有三件事情可以得到你想要的:

你可以使用括號表示法得到任何對象的任何成員。

var mc:MovieClip = someMovieClip; 
var xVal:Number = mc.x;  // The obvious way 
xVal = mc["x"];    // This works too 
var propName:String = "x"; 
xVal = mc[propName] ;  // So does this. 

您可以使用變量

function echo(message:String):void { 
    trace(message); 
} 
echo("Hello");      // The normal way 
var f:Function = echo; 
f("Hello");       // This also works 

你可以調用一個函數使用功能陣列中的所有參數指的是函數。適用

// Extending the above example... 
var fArgs:Array = ["Hello"]; 
f.apply(fArgs); // This does the same thing 

這三件事(和rest參數由另一個海報指出的),你可以寫一些非常靈活的代碼之間。動態代碼肯定會帶來性能成本,但只要調用的頻率是每秒幾百次或更少,您就不會注意到這種差異。

+0

謝謝..很好的解釋,但它一直說'脅迫'的問題。 我仍然有問題,可能是我的「邏輯」我想要實現的是: 在我的舞臺我有6個項目對象和3分項對象時,點擊項目(fooMenu)重新排列這些3子項目,並需要給出這3個新的點擊事件參數(以便他們可以在點擊時調用新的剪輯)。我嘗試了你的解釋,但是還沒有把它解決。 – John 2012-07-30 17:52:41

+0

我可以投票了(呵呵太新手了) – John 2012-07-30 17:59:06

相關問題