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

问题描述

当我尝试在班级添加child时,它说无法访问空对象引用的属性或方法.在class_obj()在Main()处出现"错误#1009

when i try to addChild my class it says"Cannot access a property or method of a null object reference.at class_obj() at Main()" error #1009

我需要知道如何创建一个带有游戏循环的类要测试碰撞和东西之类的东西,请帮忙!

i need to know how to have a class that has a gameloopfor testing things like collision and stuff please help!

如果我没有eventlistener,请在对象上输入帧,否则将无法正常工作但是我需要循环的监听器.

and if i dont have eventlistener enter frame on the object ir worksbut i need the listener for the loop.

主班

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Main extends MovieClip
    {
        var mc:class_obj;

        public function Main()
        {
            //constructor
            mc = new class_obj();
            addChild(mc);
        }
    }
}

对象类

package
{

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

    public class class_obj extends MovieClip
    {

        public function class_obj()
        {
            // constructor code
            stage.addEventListener(Event.ENTER_FRAME, loop);
        }
        public function loop(e:Event)
        {
            trace("LOOPED CLASS");
        }
    }
}

推荐答案

为避免在循环中出现任何问题,如果您对舞台对象有一些引用,我认为@Pawel是正确的,因为他说您需要先听:

To avoid any issue in your loop if you have some references to the Stage Object, I think That @Pawel is right when he say that "You need to first listen for :

Event.ADDED_TO_STAGE

为Pawel投票!

每个类名都应该以大写字母开头,就像他所说的那样,当您不再需要事件监听器时,最好删除它.

Each Class name should start with an Uppercase letter as he said, and it's a good practice to remove an Event Listener when You don't need it anymore.

所以您的课程应该是:

主要:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import ClassObj;

    public class Main extends MovieClip {

        var mc:ClassObj;

        public function Main() {
            this.addEventListener(Event.ADDED_TO_STAGE,addClip);
        }

        private function addClip(e:Event):void {
            trace("Class Main instance is added to : " + this.parent);
            mc = new ClassObj();
            addChild(mc);
            e.target.removeEventListener(Event.ADDED_TO_STAGE,addClip);
            trace(this + "hasEventListener(Event.ADDED_TO_STAGE) = " + e.target.hasEventListener(Event.ADDED_TO_STAGE));
        }
    }

}

和ClassObj:

package {
    import flash.display.MovieClip;
    import flash.events.Event;

    public class ClassObj extends MovieClip {

        public function ClassObj() {
            this.addEventListener(Event.ADDED_TO_STAGE,checkClip);
        }

        private function checkClip(e:Event):void{
            trace("ClassObj instance is added to : " + this.parent);
            this.addEventListener(Event.ENTER_FRAME, loop);
            e.target.removeEventListener(Event.ADDED_TO_STAGE,checkClip);
            trace(this + "hasEventListener(Event.ADDED_TO_STAGE) = " + e.target.hasEventListener(Event.ADDED_TO_STAGE));
        }

        private function loop(e:Event):void{
             trace("LOOPED CLASS Stage is : " + e.target.stage);
             trace("LOOPED CLASS parent is : " + e.target.parent);
        }
    }

}

输出将是:

Class Main instance is added to : [object Stage]
ClassObj instance is added to : [object Main]
[object ClassObj]hasEventListener(Event.ADDED_TO_STAGE) = false
[object Main]hasEventListener(Event.ADDED_TO_STAGE) = false
LOOPED CLASS Stage is : [object Stage]
LOOPED CLASS parent is : [object Main]
LOOPED CLASS Stage is : [object Stage]
LOOPED CLASS parent is : [object Main]

...

这是了解ADDED和ADDED_TO_STAGE的有用链接:

Here is a useful link to understand ADDED and ADDED_TO_STAGE :

http://fromzerotoas3hero.blogspot.be/2011/03 /added-vs-addedtostage.html

[/EDIT]

这篇关于错误#1009使用循环的addChld()as3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:43