【问题标题】:How to remove event listener from childs inside parent movieclip in as3如何从 as3 中的父影片剪辑中的子项中删除事件侦听器
【发布时间】:2011-03-13 11:38:22
【问题描述】:

我已经为特定的movieclip 创建了事件监听器。在这个movieclip 内部有很多对象。当我点击父movieclip 时,事件监听器调用子对象的函数。我试过removeEventLIstener()

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

var info:MovieClip=new MovieClip();

info.graphics.beginFill(0x000000,0.35);
info.graphics.drawCircle(0,0,300);
var mc:MovieClip=new MovieClip();
mc.graphics.beginFill(0x000000,0.5);
mc.graphics.drawCircle(0,0,30);
this.addChild(info);
mc.x=0;
mc.y=0;
info.x=stage.stageWidth/2
info.y=stage.stageHeight/2;
info.addChild(mc);
mc.addEventListener(MouseEvent.CLICK,msclick);
function msclick(e:MouseEvent):void{
    e.target.removeChild(e.target.parent);}

我想删除 mc 的父级

【问题讨论】:

  • 您必须更清楚问题陈述。通常最好添加相关代码的摘录。
  • @Akhil 您可能需要显示更多代码,因为您的问题仍然有些不清楚。我理解您的问题的方式是,当您单击父显示对象时,会为其所有子对象调用 onMouseOver() 事件处理程序?另外,您可能需要考虑修改您选择用于编写代码的命名约定。例如bubble_arr 可以是bubbleArraybubble_mc 可以是bubblebubbleMCbubbleMovieClip
  • @Akhil 另一个建议是将事件对象的目标引用存储在局部变量中可能更简单,如下所示:var bubble:MovieClip = e.target as MovieClip;。这样您就可以简单地访问您的目标引用,例如 bubble.addChild(infoGrd)bubble.name
  • 现在我已经编辑了代码,以便您可以轻松理解
  • @Akhil,目前还不清楚您的要求和问题所在。您的应用程序中实际发生了什么,您希望发生什么?有一个疯狂的刺,你可能想看看使用'mc.mouseChildren = false'或'currentTarget'而不是'target'。我还建议在您的代码中添加一些跟踪语句(例如,trace(e.target);trace(e.currentTarget);trace(e.target.parent);trace(e.currentTarget.parent))并进行比较与您期望的回报相反。

标签: flash actionscript-3 actionscript


【解决方案1】:

要从其父对象中删除 mc 显示对象,您可以执行以下操作:

function onMCClick(e:MouseEvent):void
{
    var target:DisplayObject = e.target as DisplayObject;
    var parent:DisplayObjectContainer = target.parent;

    parent.removeChild(target);

}// end function

您还可以通过执行以下操作删除添加到目标对象的事件侦听器:

function onMCClick(e:MouseEvent):void
{
    var target:DisplayObject = e.target as DisplayObject;

    target.removeEventListener(MouseEvent.CLICK,onMouseClick);

}// end function

【讨论】:

    【解决方案2】:

    我终于得到答案了......谢谢大家............

    导入 flash.display.MovieClip; 导入 flash.events.Event; 导入 flash.events.MouseEvent;

    var info:MovieClip=new MovieClip();
    
    info.graphics.beginFill(0x000000,0.35);
    info.graphics.drawCircle(0,0,300);
    var mc:MovieClip=new MovieClip();
    mc.graphics.beginFill(0x000000,0.5);
    mc.graphics.drawCircle(0,0,30);
    
    this.addChild(info);
    mc.x=0;
    mc.y=0;
    info.x=stage.stageWidth/2
    info.y=stage.stageHeight/2;
    mc.name="222";
    info.addChild(mc);
    
    mc.addEventListener(MouseEvent.CLICK,msclick);
    function msclick(e:MouseEvent):void{
    trace(info.getChildByName("222"));
    info.getChildByName("222").x+=100;
    trace(e.target.name)
        var ob:Object=getChildByName(e.target.name);
        //this.addChild(ob);
        info.removeChild(getChildByName("222"));
        }
    

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 2011-07-13
      • 2012-04-13
      • 2012-05-25
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2023-02-09
      相关资源
      最近更新 更多