0
votes

Main.as

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

public class Main extends MovieClip {
    public var _root:MovieClip;

    public var monsterContainer:MovieClip = new MovieClip();


    public var delay = 30;

    public function Main(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        addChild(monsterContainer);
        delay -= 1;
        if(delay <= 0){
            var spawn:Slime = new Slime();
            spawn.x = startPoint.x;
            spawn.y = startPoint.y;
            monsterContainer.addChild(spawn);
            delay = 30;
        }

    }
}

Arrow.as

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

public class Arrow extends MovieClip {
    public var _root:MovieClip;

    public var facingID;

    public function Arrow(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        trace(_root.monsterContainer == null);

    }
}

and in the output it's appear like this :

false 
false
false
false
false
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Arrow/enterFrameEvents()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Arrow/enterFrameEvents()

at first it say false because trace(_root.monsterContainer == null), but it's suddenly said error 1009, when i used ctrl+shift+enter, it's pointing to trace(_root.monsterContainer == null);

i was try tis for 2 days, please help

the other code :(it's actually maybe not related)

Enemy.as

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

public class Enemy extends MovieClip {
    public var _root:MovieClip;

    //Status
    public var monsterSpeed;
    public var facing = "Right";

    //CallingArrow
    public var down:Down = new Down();

    public function Enemy(){
        addEventListener(Event.ADDED, beginClass);
        addEventListener(Event.ENTER_FRAME, enterFrameEvents);
    }

    function beginClass(e):void{
        _root = MovieClip(root);
    }

    function enterFrameEvents(e):void{

        //Facing Movement
        if(_root.pausing == false){
            if(facing == "Right"){
                this.x += monsterSpeed;
            }else if(facing == "Left"){
                this.x -= monsterSpeed;
            }else if(facing == "Down"){
                this.y += monsterSpeed;
            }else if(facing == "Up"){
                this.y -= monsterSpeed;
            }
        }


    }
}

Down.as

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


public class Down extends Arrow {

    public function Down(){

        facingID = "Down";
    }
}

Slime.as

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


public class Slime extends Enemy {

    public function Slime(){

        monsterSpeed = 5;

    }
}

and there is no additional code on timeline just stop();

if u need the file (cs6): http://www.mediafire.com/download/hz5tptkgftwdipw/Tower_Defense.rar

1
It looks like somehow _root is becoming null. If monsterContainer were null, it wouldn't throw an error, you would just get a trace of true. - Marcela
yes it should be just traced as true, but idk why suddenly error 1009 appear, and that's so annyoing, any idea ? - Yoga Adya Pratama
I suspect you are not showing the full code here. Are you removing the arrow instance? Where are you even instantiating it? It is not visible anywhere... - Fygo
@Fygo the other code is not related to this, umm and i don't remove arrow instance, so arrow is just like a .as to tracing the monsterContainer from Main.as - Yoga Adya Pratama
@YogaAdyaPratama The code where you are manipulating and instantiating the arrow IS related to your question because it is the object that is throwing you the error. It is very unlikely that this would happen unless you changed the '_root', 'monsterContainer, or the arrow instances/references. You must be definitely doing one of that. - Fygo

1 Answers

0
votes

Change it to this:

public function Arrow(){
    addEventListener(Event.ADDED, beginClass);
}

function beginClass(e):void{
    _root = MovieClip(root);
    addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}

In your code _root will be NULL, because your object may not be added to the stage in the same frame so

function enterFrameEvents(e):void{

    trace(_root.monsterContainer == null);

}

will try to access the monsterContainer property of a NULL object. Hence the good old #1009