【问题标题】:removeChild dont delete child of objectremoveChild 不删除对象的子对象
【发布时间】:2019-05-12 20:00:43
【问题描述】:

我有一个由 blue_robot 调用的影片剪辑,该影片剪辑有一个由 right_hand 调用的子项,我想删除此 right_hand 子项,但删除子项不要删除它。 这是我的动作脚本代码:

var robot:blue_robot=new blue_robot();
addChild(robot);
removeChild(robot.right_hand);

我试试这个但给我错误类型错误:错误#2007:参数子必须是非空的。

【问题讨论】:

    标签: actionscript-3 actionscript


    【解决方案1】:

    由于right_handrobot 的孩子,你必须使用机器人的remove child 方法:

    robot.removeChild(robot.right_hand);
    

    这告诉robot 实例如果是子对象,则删除一个,即robot.right_hand 中引用的对象。

    之前,您告诉父上下文(示例代码中的this)删除right_hand,但right_hand 不是this 的子级,它是robot 的子级。


    您发布的错误意味着引用 robot.right_hand 不存在。

    仔细检查您实际上有一个名为right_hand 的实例名称或属性,它存在并且在第一帧(如果创建了时间线)或构造函数(如果创建了代码)上有一个值。

    【讨论】:

    • 我试试这个但给我错误 TypeError: Error #2007: Parameter child must be non-null.
    【解决方案2】:

    试试这个:

    robot.removeChild(robo.getChildByName("right_hand"));
    

    但最好从 blue_robot 类内部移除右手。您应该使用面向对象的功能。这意味着 blue_robot 必须自己做任何与它自己相关的事情。所以结果应该如下所示:

    class blue_robot extends MovieClip{
       private var myRightHand:MovieClip ;
       public blue_robot()
       {
          super();
          myRightHand = this.getChildByName("right_hand");
       }
    
       public function removeRightHand():void
       {
          this.removeChild(myRightHand);
       }
    }
    

    ↑对于blue_robot类和

    var robot:blue_robot=new blue_robot();
    addChild(robot);
    robot.removeRightHand();
    

    ↑ 移除机器人父级的右手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 2012-06-13
      • 2021-11-11
      • 2019-11-25
      • 2021-07-23
      • 2020-07-30
      相关资源
      最近更新 更多