动态变量名称 - Actionscript 3

发布于 2024-10-28 00:05:16 字数 744 浏览 2 评论 0原文

我想基于数组动态创建元素。我想只输入数组名称并附加 _panel ...这样 menu_item_panel 将成为 settings_panel、info_panel 等。我不知道该怎么做?

var menu_items:Array = new Array("settings","info","debug","Feedback");
var menu_items_count:Number = menu_items.length;

var menu_height:Number = c.height / menu_items_count;
var menu_height_int:Number = 0;

for (var i:int=0; i<menu_items_count; i++)
{
    var menu_item_panel:Sprite = new Sprite();
menu_item_panel.graphics.beginFill(0x000000, 1);
menu_item_panel.graphics.drawRect(0, menu_height, c.width, menu_height);
menu_item_panel.graphics.endFill();
menu_panel.addChild(menu_item_panel);
menu_panel.addEventListener(MouseEvent.CLICK,menu_panel_click);
menu_height_int = menu_height_int + menu_height;
}

I would like to dynamically create elements based on the array. I would like to just put the array name and append _panel ...so menu_item_panel will become settings_panel, info_panel,etc. I cannot figure out how to do that?

var menu_items:Array = new Array("settings","info","debug","Feedback");
var menu_items_count:Number = menu_items.length;

var menu_height:Number = c.height / menu_items_count;
var menu_height_int:Number = 0;

for (var i:int=0; i<menu_items_count; i++)
{
    var menu_item_panel:Sprite = new Sprite();
menu_item_panel.graphics.beginFill(0x000000, 1);
menu_item_panel.graphics.drawRect(0, menu_height, c.width, menu_height);
menu_item_panel.graphics.endFill();
menu_panel.addChild(menu_item_panel);
menu_panel.addEventListener(MouseEvent.CLICK,menu_panel_click);
menu_height_int = menu_height_int + menu_height;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离笑几人歌 2024-11-04 00:05:17

试试这个

for (var i:int=0; i<menu_items_count; i++)
{
    var menu_item_panel:Sprite = new Sprite();
    menu_item_panel.graphics.beginFill(0x000000, 1);
    menu_item_panel.graphics.drawRect(0, menu_height, c.width, menu_height);
    menu_item_panel.graphics.endFill();
    menu_panel.addChild(menu_item_panel);
    menu_panel.addEventListener(MouseEvent.CLICK, function(){});
    menu_height_int = menu_height_int + menu_height;
    // Store the new clip in a var
    this[menu_items[i]+"_panel"] = menu_item_panel;
}
// get the clips
trace(this.settings_panel);
trace(this.info_panel);
trace(this.debug_panel);
trace(this.Feedback_panel);

Try this

for (var i:int=0; i<menu_items_count; i++)
{
    var menu_item_panel:Sprite = new Sprite();
    menu_item_panel.graphics.beginFill(0x000000, 1);
    menu_item_panel.graphics.drawRect(0, menu_height, c.width, menu_height);
    menu_item_panel.graphics.endFill();
    menu_panel.addChild(menu_item_panel);
    menu_panel.addEventListener(MouseEvent.CLICK, function(){});
    menu_height_int = menu_height_int + menu_height;
    // Store the new clip in a var
    this[menu_items[i]+"_panel"] = menu_item_panel;
}
// get the clips
trace(this.settings_panel);
trace(this.info_panel);
trace(this.debug_panel);
trace(this.Feedback_panel);
指尖上得阳光 2024-11-04 00:05:17

简单:

for (...)
{
    //...
    menu_item_panel.name = menu_items[i]+"_panel";
    menu_panel[menu_item_panel.name] = menu_panel.addChild(menu_item_panel);
    //...
}

这是因为两件事:

// This line set the name of the item to "settings_panel", "info_panel", etc
menu_item_panel.name = menu_items[i]+"_panel";

// This line add menu_item_panel as a child of menu_panel
// and set the property called "settings_panel", "info_panel", etc
// to the right panel
menu_panel[menu_item_panel.name] = menu_panel.addChild(menu_item_panel);

所以当您运行此行时:

menu_panel.settings_panel;

您将获得名为“settings_panel”的菜单项

这种类型的设置:

object[string] = property;

适用于声明为动态的所有对象(MovieClip< /code> 和 Object动态)。这是在运行时向对象添加(动态)属性的方法。

Simple:

for (...)
{
    //...
    menu_item_panel.name = menu_items[i]+"_panel";
    menu_panel[menu_item_panel.name] = menu_panel.addChild(menu_item_panel);
    //...
}

That's because two things:

// This line set the name of the item to "settings_panel", "info_panel", etc
menu_item_panel.name = menu_items[i]+"_panel";

// This line add menu_item_panel as a child of menu_panel
// and set the property called "settings_panel", "info_panel", etc
// to the right panel
menu_panel[menu_item_panel.name] = menu_panel.addChild(menu_item_panel);

So when you run this line:

menu_panel.settings_panel;

You are obtaining the menu item called "settings_panel"

This type of setting:

object[string] = property;

Works for all objects that are declared dynamic (MovieClip and Object are dynamic). That's the way to add (dinamically) properties to an object in run-time.

对不⑦ 2024-11-04 00:05:17

为什么不创建一个类并扩展精灵类,然后您就可以根据需要使其动态化。

Why not make a class and extend the sprite class then you can make it as dynamic as you need.

转瞬即逝 2024-11-04 00:05:17

我最终添加了以下内容,它似乎有效。我不知道为什么我必须把“这个”放在任何地方。

this[menu_items[i]+"_panel"] = menu_item_panel;
    menu_panel.addChild(this[menu_items[i]+"_panel"]);

我可以通过以下方式使其可见:

 this.settings_panel.visible = false;

I ended up adding the following and it appears to work. I'm not sure why I have to put "this" anywhere.

this[menu_items[i]+"_panel"] = menu_item_panel;
    menu_panel.addChild(this[menu_items[i]+"_panel"]);

And I can make it visible by saying:

 this.settings_panel.visible = false;
凉墨 2024-11-04 00:05:16

我建议您将 Sprite 对象的 name 属性设置为菜单项的名称。

menu_item_panel.name = menu_items[i];

然后在您的 click 处理程序中检查 event.targetname 属性,并根据该属性执行适当的操作。

I would recommend you set the name property of the Sprite object to the name of the menu item.

menu_item_panel.name = menu_items[i];

Then in your click handler you check the name property of event.target and perform the appropriate action based on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文