动态变量名称 - Actionscript 3
我想基于数组动态创建元素。我想只输入数组名称并附加 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
Try this
简单:
这是因为两件事:
所以当您运行此行时:
您将获得名为“settings_panel”的菜单项
这种类型的设置:
适用于声明为动态的所有对象(
MovieClip< /code> 和
Object
是动态
)。这是在运行时向对象添加(动态)属性的方法。Simple:
That's because two things:
So when you run this line:
You are obtaining the menu item called "settings_panel"
This type of setting:
Works for all objects that are declared
dynamic
(MovieClip
andObject
aredynamic
). That's the way to add (dinamically) properties to an object in run-time.为什么不创建一个类并扩展精灵类,然后您就可以根据需要使其动态化。
Why not make a class and extend the sprite class then you can make it as dynamic as you need.
我最终添加了以下内容,它似乎有效。我不知道为什么我必须把“这个”放在任何地方。
我可以通过以下方式使其可见:
I ended up adding the following and it appears to work. I'm not sure why I have to put "this" anywhere.
And I can make it visible by saying:
我建议您将
Sprite
对象的name
属性设置为菜单项的名称。然后在您的
click
处理程序中检查event.target
的name
属性,并根据该属性执行适当的操作。I would recommend you set the
name
property of theSprite
object to the name of the menu item.Then in your
click
handler you check thename
property ofevent.target
and perform the appropriate action based on that.