AS3删除孩子不工作和/或给出错误

时间:2014-03-02 05:17:14

标签: actionscript-3 flash

我想要做的,主要是当它离开舞台时删除影片剪辑。 但是removechild代码给了我错误(第62行)。救命! 我是flash btw的新手。提示,或任何非常感谢!

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

var tray:Array = new Array(Legal2_1,Legal2_2,Legal2_3,Legal2_4,Legal2_5,Legal2_6,Legal2_7,Legal2_8,Legal2_9,Legal2_10,Legal2_11,Legal2_12,Legal2_13,Legal2_14,Legal2_15,Illegal2_1,Illegal2_2,Illegal2_3,Illegal2_4,Illegal2_5,Illegal2_6,Illegal2_7,Illegal2_8,Illegal2_9,Illegal2_10,Illegal2_11,Illegal2_12,Illegal2_13,Illegal2_14,Illegal2_15);
var traypos:int;
var goodpos:int;
var badpos:int;

traypos = (stage.stageWidth / 2)-100;
goodpos = ((stage.stageWidth / 3) -100);
badpos = (((stage.stageWidth/3) *2) -100);

Score.text = score.toString();
Combo.text = combo.toString();

var timerT:Timer = new Timer(1000,120);
timerT.addEventListener(TimerEvent.TIMER, traytimerhandler);
timerT.start();

var secondsT:Number = 1;

function traytimerhandler(event:TimerEvent)
{
    //trace("Seconds elapsed: " + seconds);
    SpawnTray();
    secondsT++;
}

function SpawnTray():void
{
    var trayspawn:int;
    trayspawn = int(Math.random() * tray.length);

    //var trayn:MovieClip = new tray[trayspawn]();
    var trayn:MovieClip = new tray[trayspawn];
    trayn.trayTypeIndex = trayspawn;

    addChild(trayn);

    trayn.x = traypos;
    trayn.y = -20;

    trayn.addEventListener(Event.ENTER_FRAME, MoveTray);    
    trayn.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);   
}

function MoveTray(event:Event):void
{   
var trayn:DisplayObject = event.target as DisplayObject;
trayn.y +=  5;
/*if (trayn.y >= stage.stageHeight + 50)
{
    trayn.removeEventListener(Event.ENTER_FRAME, MoveTray);
    trayn.removeEventListener(Event.ENTER_FRAME, MoveTwo);
    trayn.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);    
    //removeChild(trayn);
    //tray.parent.removeChild(trayn);
}   */
if (trayn.y >= stage.stageHeight + 50)
{
    this.removeChild(trayn); //ERROR
}

}

function startDragging(e:MouseEvent):void
{
    e.target.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
    e.target.removeEventListener(Event.ENTER_FRAME, MoveTray);
    // surprise! This object will not be moved via MOUSE_DOWN,;
    // because it's already being moved 
    e.target.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
    e.target.startDrag();
}
// drag;

function stopDragging(e:MouseEvent):void
{
    e.target.stopDrag();
    e.target.addEventListener( Event.ENTER_FRAME, MoveTray);
    e.target.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
    e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
    return;// emergency exit. We don't need to do more
}

2 个答案:

答案 0 :(得分:0)

这意味着您的addChild(trayn);无效。

如果需要传递事件,为什么要调用SpawnTray(null);

SpawnTray(event:Event)设为SpawnTray(),就像这样,

function SpawnTray():void
{

    //I have not added your all lines here for clarity (you add it here)

    var trayn:MovieClip = tray[trayspawn]; //See this modified line


}

并像这样修改traytimerhandler

function function traytimerhandler(event:TimerEvent)
{
    //trace("Seconds elapsed: " + seconds);
    SpawnTray(); // modified
    secondsT++;
}

答案 1 :(得分:0)

此行应该有效:

   trayn.parent.removeChild(trayn); 

但是,正如我在评论中所指出的那样,您似乎并不了解您的方法究竟发生了什么,这会引起混淆。

这是一个糟糕的方法,你需要做的是退后一步,重新思考为什么你想要/需要将参数传递给返回的处理函数。

如果它是一个MovieClip,一种方法是你可以在创建存储给定值时添加一个属性 - 即:

trayn.trayTypeIndex = trayspawn;

现在,只要您使用该实例(如果它被转换为MovieClip),您就可以访问该属性。

您的处理程序中的示例可能是这样的:

var trayn:MovieClip = event.target as MovieClip;

if (trayn.trayTypeIndex <= 14)
{
   // do something
}