Actionscript 3 AS3将变量参数传递给通用功能菜单/子项

Actionscript 3 AS3将变量参数传递给通用功能菜单/子项,actionscript-3,events,variables,drop-down-menu,parameter-passing,Actionscript 3,Events,Variables,Drop Down Menu,Parameter Passing,我不是代码天才,而是动作脚本的粉丝。 你能帮我吗 我有一个函数,根据所选的对象,它将调用事件侦听器,以调用已在后台的一组“子项”(我希望在单击时使用已更改的参数重用此子项,而不是创建多个实例和多个代码) 因此,对于每个选定的“案例”,我必须将不同的变量传递给这些“子项”,如下所示: function fooMenu(event:MouseEvent):void { switch (event.currentTarget.name) { case "btUa1" :

我不是代码天才,而是动作脚本的粉丝。 你能帮我吗

我有一个函数,根据所选的对象,它将调用事件侦听器,以调用已在后台的一组“子项”(我希望在单击时使用已更改的参数重用此子项,而不是创建多个实例和多个代码)

因此,对于每个选定的“案例”,我必须将不同的变量传递给这些“子项”,如下所示:

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);
功能菜单(事件:MouseEvent):无效{
开关(event.currentTarget.name)
{
案例“btUa1”:
跟踪(event.currentTarget.name);
//这里有一堆代码
//(刚刚清洁以便于查看)
/*
这里的帮助(我不确定你的问题是否正确,而且我已经有一段时间没有在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
  }
}

请记住,您不能使用btTextos.addEventListener(MouseEvent.CLICK,carregacontudo(“jocasta”);因为在添加Eventlistener时传递的第二个参数将被视为函数本身-使用addEventListener有两种正确的方法:

1:

2:

因此:


如果希望动态生成内容,请尝试以下操作:

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);
    }
}
btUa1.addEventListener(MouseEvent.CLICK,function()){
创建菜单(1);
});
btUa2.addEventListener(MouseEvent.CLICK,function()){
创建菜单(2);
});
函数createMenu(id):无效
{
//切换子菜单元素
开关(id)
{
案例1:
createSubmenu([myFunc1,myFunc2,myFunc3]);//动态创建子菜单,以防您需要的子菜单比您已有的多
打破
案例2:
创建子菜单([myFunc4,myFunc5,myFunc6,myFunc7]);
打破
违约:
[等等]
}
}
函数createSubmenu(…rest):void{
对于(变量i:uint=0;i
您的问题相当模糊;您希望“传递”什么“变量”?以及“将变量传递给子项”是什么意思?通常“传递”意味着调用函数

如果你能更具体地说明你到底想做什么,那会很有帮助。同时,这里有三件事可能会达到你的目的:

可以使用括号表示法获取任何对象的任何成员

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
可以使用function.apply使用数组中的所有参数调用函数

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

在这三件事之间(以及另一张海报上标注的
rest
参数)您可以编写一些非常灵活的代码。动态代码当然要以性能为代价,但只要调用频率为每秒几百次或更少,您就永远不会注意到差异。

谢谢!!!以下是我需要的:在我的阶段中,当单击项(fooMenu)时,我有6个项对象和3个子项对象我重新排列了这3个子项,需要给这3个新的click事件参数(以便它们在单击时可以调用新剪辑)。我尝试了您的解释,但没有正确理解…这里:
code
function fooSub(event:MouseEvent,bla:String):void{trace(event.currentTarget.name+“-”+bla);//bla将是剪辑名称。}在fooMenu案例中:
code
btTextos.addEventListener(MouseEvent.CLICK,fooSub(“jocasta”));注意编辑后的答案;)希望有帮助。如果你想在你的问题下面添加一些信息,请在你的问题下面使用评论,或者进行编辑。哈!!非常感谢切夫!!:)谢谢..很好的解释,但它一直在说“强制”问题。我仍然有问题,可能我的“逻辑”我试图实现的是:在我的阶段,我有6项对象和3项子对象items对象单击项(fooMenu)时,我重新排列了这3个子项,并需要提供这3个新的单击事件参数(以便它们可以在单击时调用新剪辑)。我尝试了您的解释,但尚未正确理解。
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);
    }
}
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