【问题标题】:Actionscript 3: Trouble with removing child from parentActionscript 3:从父母中移除孩子的麻烦
【发布时间】:2014-02-27 16:13:32
【问题描述】:

我正在为学习目的编写代码,但遇到了一个明显无法解决的问题。

我将首先向您介绍我的代码。

这是我的主类中的一个函数

public function confirm_route(evt:MouseEvent):void
    {
        var route = new Route(this, Airport.return_ROUTE);
        Airport.return_ROUTE = new Array();
    }

"Airport.return_ROUTE" 只是一个数组,它的来源与问题并不真正相关。在 Main 类中,我还声明了我的背景变量,并将其作为公共静态变量。由于我需要从其他两个类访问此背景,因此我没有看到其他选择,而是将其声明为那样,即使我认为这并不理想。我将在稍后的解释中回到这一点。

我的 Route 类在屏幕上动态创建新航班,因此得名。这基于 Airport.return_ROUTE 数组。我需要将这些航班作为孩子添加到背景中,如前所述,这是在 Main 类中创建的。这也是我在调用路由函数时添加“this”作为参数的原因。

this.myparent = pMyParent;

我使用上面的行来引用主实例。由于 Route 实例不是电影剪辑,我想这是能够引用它的唯一方法。

如前所述,Route 实例动态创建 new_flights。

new_flight = new Flight(infoarray);
myparent.background_mc.addChild(new_flight);

这显然是为了将 new_flight 添加到背景影片剪辑中。

然后让我们看看 Flight 类,因为这就是问题所在。

由于我的游戏概念,我需要在 new_flight 到达背景影片剪辑上的某个点时将其删除。

此功能旨在完成这项工作:

private function deleteThis():void
    {
        this.parent.removeChild(this)
    }

这将返回TypeError: Error #1009: Cannot access a property or method of a null object reference.

我真的不明白如何以不同的方式解决这个问题。


编辑:根据要求,我将发布我的路线课程。

package 
{

import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import Main;

public class Route extends Main
{
    public var income:Number;
    public var routePoints:Array;
    private var routeTimer:Timer;
    private var new_flight:Flight;

    // ------------

    private var myparent:Main;

    public function Route(route_array:Array, pMyParent)
    {
        this.myparent = pMyParent;
        this.routePoints = route_array;
        routeTimer = new Timer(2000);// 2 second
        routeTimer.addEventListener(TimerEvent.TIMER, route_function);
        routeTimer.start();
    }
    private function route_function(event:TimerEvent):void
    {
        for (var counter:uint = 0; counter < routePoints.length - 1; counter ++)
        {   
            trace("Coords: ", routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]);
            new_flight = new Flight(myparent, routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]);
            myparent.bg_image.addChild(new_flight);

            var checkTimer:Timer = new Timer(15);// 1 second
            checkTimer.addEventListener(TimerEvent.TIMER, check_function);
            checkTimer.start();

            function check_function(event:TimerEvent):void
            {
                if (new_flight.finished = true)
                {
                    checkTimer.stop();
                }
            }
        }
    }

}

}

编辑 2:同时发布 Flight 类

package 
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.controls.Button;
import flash.display.DisplayObject;

public class Flight extends MovieClip

{
    public static var speed:uint = 1
    public var finished:Boolean = false;

    protected var absvector:Number;
    protected var vector:Array;
    protected var myTimer:Timer;
    //protected var parentContainer:MovieClip;

    protected var utgangspunkt_x;
    protected var utgangspunkt_y;
    protected var destinasjon_x;
    protected var destinasjon_y;

    protected var vector_x;
    protected var vector_y;

    private var myparent:Main

    public function Flight(pMyParent, utgangspunkt_x, utgangspunkt_y, destinasjon_x, destinasjon_y):void
    {
        addEventListener(Event.ADDED_TO_STAGE, init);           

        this.myparent = pMyParent;

        this.utgangspunkt_x = utgangspunkt_x;
        this.utgangspunkt_y = utgangspunkt_y;

        this.x = utgangspunkt_x;
        this.y = utgangspunkt_y;    

        this.destinasjon_x = destinasjon_x + 10;
        this.destinasjon_y = destinasjon_y + 10;

        this.vector_x = Math.abs(this.destinasjon_x-this.utgangspunkt_x);
        this.vector_y = Math.abs(this.destinasjon_y-this.utgangspunkt_y);

        this.height = 20;
        this.width = 20;
    }

    public function init(evt:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        trace(this.parent)
        trace("---------------------------------------------------")
        if (utgangspunkt_x < destinasjon_x)
        {
            this.rotation = -(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180;
        }
        else
        {
            this.rotation = 180-(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180;
        }

        absvector = Math.sqrt(Math.pow((destinasjon_x - utgangspunkt_x),2) + Math.pow((utgangspunkt_y - destinasjon_y),2));
        vector = [(destinasjon_x - utgangspunkt_x) / absvector,(utgangspunkt_y - destinasjon_y) / absvector];

        stage.addEventListener(Event.ENTER_FRAME, movement)
    }

    private function movement(evt:Event):void
    {
        if (this.vector_x > this.vector_y)
        {
            if (destinasjon_x>utgangspunkt_x)
            {
                if (this.x < destinasjon_x)
                {
                    this.x +=  speed*vector[0];
                    this.y -=  speed*vector[1];
                }
                else
                {
                    deleteThis()
                }
            }
            else if (destinasjon_x<utgangspunkt_x)
            {
                if (this.x > destinasjon_x)
                {
                    this.x +=  speed*vector[0];
                    this.y -=  speed*vector[1];
                }
                else
                {
                    deleteThis()
                }
            }
        }
        else
        {
            if (destinasjon_y>utgangspunkt_y)
            {
                if (this.y < destinasjon_y)
                {
                    this.x +=  speed*vector[0];
                    this.y -=  speed*vector[1];
                }
                else
                {
                    deleteThis()
                }
            }
            else if (destinasjon_y<utgangspunkt_y)
            {
                if (this.y > destinasjon_y)
                {
                    this.x +=  speed*vector[    0];
                    this.y -=  speed*vector[1];
                }
                else
                {
                    deleteThis()
                }
            }
        }
    }

    private function deleteThis():void
    {
        finished = true;
        this.parent.removeChild(this)
    }
}
}

【问题讨论】:

  • 在 Flight init 函数中跟踪“this.parent”时,它返回“[object Image]”。这真的让我很沮丧,因为错误消息表明 this.parent == null。
  • 你能展示你的路由类吗? “this.parent”不起作用,您需要在变量中引用父级

标签: actionscript-3 class null parent-child removechild


【解决方案1】:

我怀疑你的 deleteThis 方法被多次调用。这可以解释为什么你有 [object Image] 然后什么都没有...... 这个方法是被某种事件调用的吗?如果是这种情况,请确保不会多次触发此事件。

【讨论】:

  • 我现在将 Flight 类添加到问题描述中
猜你喜欢
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多