本文介绍了使用removeChild(AS3)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个pacman风格的游戏。我试图使用removeChild()删除一个MovieClip的实例。当MovieClip实例box击中MovieClip实例circle时,环将从舞台上移除。

我在下面收到以下错误:
ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子项。
at flash.display :: DisplayObjectContainer / removeChild()
在Move / eatCircle()

  package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

public class Move扩展MovieClip {

var ScoreObjects:Array = new Array(); //创建ScoreObjects数组


private var inertia:int = 8; //摩擦量

var score_field:String;
// var point:MovieClip;


//构造函数----------------------------------- ---------------------------------
public function Move(){
init() ;
}

//函数init --------------------------------- --------------------------------
函数init():void {

//stage.frameRate=60;
var score_field:String =;

ScoreObjects [0] = new Circle();
ScoreObjects [0] .amount = 1; //点数
ScoreObjects [0] .name =circle;


stage.addEventListener(Event.ENTER_FRAME,frameloop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpEvent);

box.addEventListener(Event.ENTER_FRAME,eatC​​ircle);
wall.addEventListener(Event.ENTER_FRAME,hitWall);

stage.addChild(ScoreObjects [0]); //将Score对象添加到舞台------------------------------
trace(ScoreObjects [0]);

ScoreObjects [0] .x = 105;
ScoreObjects [0] .y = 233;

}

// function eatCircle ----------------------------- ---------------------------------
函数eatCircle(event:Event):void {

if(box.hitTestObject(ScoreObjects [0])){
trace(我吃了圆);
removeChild(ScoreObjects [0]);
// calcScore();
} else {
trace(我没有吃圆圈);




$ b} //课程结束
} //包结束
{
stage.removeChild(ScoreObjects [0]);
}


I am creating a pacman-style game. I am trying to remove an instance of a MovieClip using removeChild(). When the MovieClip instance "box" hits the MovieClip instance "circle" --circle will be removed from the stage.

I am receiving the following error below:ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Move/eatCircle()

package {
       import flash.display.Sprite;
       import flash.display.MovieClip;
       import flash.events.Event;
       import flash.events.KeyboardEvent;
       import flash.ui.Keyboard;

       public class Move extends MovieClip {

       var ScoreObjects:Array = new Array(); // creates ScoreObjects array


          private var inertia:int=8; //amount of friction

       var score_field:String;
       //var point:MovieClip;


     // Constructor--------------------------------------------------------------------
          public function Move() {
             init();
          }

     // function init -----------------------------------------------------------------
       function init():void {

             //stage.frameRate=60;
        var score_field:String="";

      ScoreObjects[0] = new Circle();
      ScoreObjects[0].amount = 1; // amount of point
      ScoreObjects[0].name = "circle";


             stage.addEventListener(Event.ENTER_FRAME, frameloop);
             stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

        box.addEventListener(Event.ENTER_FRAME, eatCircle);
        wall.addEventListener(Event.ENTER_FRAME, hitWall);

        stage.addChild(ScoreObjects[0]); // add Score Objects to stage ------------------------------
        trace(ScoreObjects[0]);

        ScoreObjects[0].x = 105;
        ScoreObjects[0].y = 233;

          }

     // function eatCircle --------------------------------------------------------------
     function eatCircle(event:Event):void {

      if (box.hitTestObject(ScoreObjects[0])) {
        trace ("I ate the circle");
        removeChild(ScoreObjects[0]);
        //calcScore();
       } else {
        trace ("I didn't eat the circle");
       }
     }



       }// end of class
    }// end of package
解决方案
if((ScoreObjects[0] as Circle)&&((ScoreObjects[0] as Circle).parent!=null))
{
   stage.removeChild(ScoreObjects[0]);
}

这篇关于使用removeChild(AS3)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:10