for-loop - 如何获得一个循环来加载带有事件监听器的影片剪辑
问题描述
我希望场景加载 5 个不同的影片剪辑(名为 B1-B5)。每个影片剪辑都放置在特定的 x 和 y 上。每个影片剪辑在滚动/滚动时都会增长/缩小....
我通过输入所有内容并每次复制每个部分来使代码正常工作,但这很混乱,我想通过循环来清理代码(如果可能的话?)。
这是有效的代码,但我必须在每个电影剪辑中复制它(更改明显的位)......
var scene1:MovieClip = new B1();
addChild(scene1);
scene1.x = 170.30;
scene1.y = 231.15;
scene1.addEventListener(MouseEvent.MOUSE_OVER, onRollOverEvent1);
scene1.addEventListener(MouseEvent.MOUSE_OUT, onRollOutEvent1);
function onRollOverEvent1(e:MouseEvent) {
scene1.width=25.9;
scene1.height=25;
}
function onRollOutEvent1(e:MouseEvent) {
scene1.width = 20.9;
scene1.height = 20;
}
以下是我尝试过但已经卡住了很长一段时间...
for (var i:int=1; i<5; i++){
var scene[i]:MovieClip = new "B"+i();
addChild("scene"+i);
//var scene[i]:MovieClip = new B[i]();
scene[i].addEventListener(MouseEvent.MOUSE_OVER, onRollOverEvent);
scene[i].addEventListener(MouseEvent.MOUSE_OUT, onRollOutEvent)
function onRollOverEvent(e:MouseEvent) {
scene[i].width=25.9;
scene[i].height=25;
}
function onRollOutEvent(e:MouseEvent) {
scene[i].width = 20.9;
scene[i].height = 20;
}
}
scene1.x = 170.30;
scene1.y = 231.15;
scene2.x = 284.30;
scene2.y = 250.75;
scene3.x = 377.30;
scene3.y = 280.15;
scene4.x = 444.30;
scene4.y = 321.15;
scene5.x = 196.30;
scene5.y = 172.15;
解决方案
首先,让我们回顾一下你的错误。
new "B"+i();
最好的情况是调用一个数字i作为函数并将结果作为String添加到 "B" 。但即使new "B1"()也与new B1()不同。事实上,有一个方法getDefinitionByName(..)允许通过其名称来寻址一个类,但我不建议使用它,因为它是高级主题。
var scene[i]:MovieClip
您不能以这种方式定义变量scene1、scene2等。您实际上可以设计的最接近的东西是方括号表示法:this["scene" + i] = ...。
addChild("scene"+i);
参数必须是DisplayObject实例,而不是String。
for (...)
{
...
function onRollOverEvent(e:MouseEvent)
...
}
不要在其他函数或循环中定义函数。
scene[i].width = 20.9;
scene[i].height = 20;
在你的循环结束时i将等于5,那么,你认为这样的记录会解决什么问题?
然后,解决方案。
当您将工作解决方案扩展到多个实例时,您将采用算法。Loops 和Array是你的朋友。
// Lets devise a list of classes and (x,y) coordinates.
var Designs:Array = [
null, // the 0-th element
{id:B1, x:170, y:230},
{id:B2, x:285, y:250},
];
for (var i:int = 1; i < Design.length; i++)
{
// Retrieve a record for the future object.
var aDesign:Object = Designs[i];
// Get a reference to the object's class.
var aClass:Class = aDesign.id;
// Create the object. Yes, you CAN omit () with
// the "new" operator if there are no mandatory arguments.
var aThing:Movieclip = new aClass;
// Set coordinates from the design record.
aThing.x = aDesign.x;
aThing.y = aDesign.y;
// Add to the display list.
addChild(aThing);
// Subscribe the event handlers.
aThing.addEventListener(MouseEvent.MOUSE_OVER, onOver);
aThing.addEventListener(MouseEvent.MOUSE_OUT, onOut);
// Save the object's reference for the later use.
// If you'd need to address, say, 3rd object,
// you do it as following:
// Designs[3].instance
aDesign.instance = aThing;
}
function onOver(e:MouseEvent):void
{
// You subscribed all of the objects to this one event handler.
// This is the correct way to learn, which one of the objects
// is under the mouse and is dispatching the said event.
var aThing:MovieClip = e.currentTarget as MovieClip;
// Change the object's size.
aThing.width = 26;
aThing.height = 25;
}
function onOut(e:MouseEvent):void
{
// Get the source of the dispatched event.
var aThing:MovieClip = e.currentTarget as MovieClip;
// Change the object's size.
aThing.width = 21;
aThing.height = 20;
}
推荐阅读
- python - Python清除dict中的每个列表
- flutter - Flutter:将下载的图像传递到第二个屏幕
- angular - Rxjs Subject/Observable 订阅列表数组
- python - pandas df用for循环填充列名作为变量
- php - 如何将包括类属性“size-full”替换为
- wordpress - 如何在 Wordpress 中更改 get_site_url() 的 url?
- python - 闭包如何“知道”它需要保留状态信息?
- redis - Redis 从 3.0.7 升级到 5
- android - Messenger InstantGames - Web 请求在 Messenger 网页上运行良好,但在通过 Messenger 移动应用程序播放时失败
- regex - 如何在路径中间使用随机字符串删除HDFS中的文件