问题
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 gameloop for testing things like collision and stuff please help!
and if i dont have eventlistener enter frame on the object ir works but i need the listener for the loop.
Main class
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);
}
}
}
object class
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");
}
}
}
回答1:
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
So upvote for 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.
So Your classes should be :
Main :
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));
}
}
}
and 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);
}
}
}
Output will be :
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]
...
[EDIT] Here is a useful link to understand ADDED and ADDED_TO_STAGE :
http://fromzerotoas3hero.blogspot.be/2011/03/added-vs-addedtostage.html
[/EDIT]
回答2:
You probably don't have access to the stage (this is only object that you use so no other can be null obviously.
You need to first listen for
Event.ADDED_TO_STAGE
because movieclip has no access to stage before it's added to display list.
Edit
In your simple case you don't event need to use use stage object. You can listen for frame event on your class_object instance itself:
this.addEventListener(Event.ENTER_FRAME, loop);
来源:https://stackoverflow.com/questions/42889404/error-1009-addchld-as3-with-loop