2 个具有相同实例名称的影片剪辑
我在时间线上有同一个影片剪辑的 2 个副本,并且我需要它们都执行完全相同的操作,所以我想我应该给它们相同的实例名称。
我在舞台上有一个事件侦听器,用于侦听鼠标单击,然后使用 switch 语句检查单击的内容,但 switch 语句仅拾取影片剪辑的一个实例,另一个实例作为默认值出现。
我主要要问的是,是否可以在时间轴上使用相同的实例名称制作影片剪辑?
public function Main() {
stage.addEventListener(MouseEvent.CLICK, doStuff);
}
public function doStuff(e:MouseEvent):void {
switch (e.target) {
case myMC1 :
//do stuff
break;
case myMC2 :
//do stuff
break;
case myMC3 :
//do stuff
break;
default :
//do stuff
}
}
I have 2 copys of the same Movieclip on the timeline and I need them both to do the exact same thing, so I figured I'd give them the same instance name.
I have an event listener on the stage that listens for a mouse click and then checks what has been clicked using a switch statment, but the switch statment only picks up one instance of the movieclip, the other one comes up as the default.
Mainly what i'm asking is, is it possible to have to movieclips on the timeline with the same instance name?
public function Main() {
stage.addEventListener(MouseEvent.CLICK, doStuff);
}
public function doStuff(e:MouseEvent):void {
switch (e.target) {
case myMC1 :
//do stuff
break;
case myMC2 :
//do stuff
break;
case myMC3 :
//do stuff
break;
default :
//do stuff
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为实例指定两个不同的名称(永远不要对两个对象使用相同的名称,真的:))并以这种方式更改 switch 语句:
通过以这种方式格式化它,您可以为两种不同的情况执行相同的代码
Give the instances two different names (NEVER USE THE SAME NAME FOR TWO OBJECT, REALLY :)) and change the switch statement this way:
By formatting it this way, you can execute the same code for two different cases
请改用 e.currentTarget。
e.target 将为您提供调度事件的对象,该对象可能是 MovieCLip 的子级
Use e.currentTarget instead.
e.target will give you the object that dispatched the event which could be a child of your MovieCLip