1119: Access of possibly undefined property monster through a reference with static type Enemy. AS3

不羁岁月 提交于 2019-12-11 22:00:56

问题


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);

    }
}

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();

I got 1119 error, when i want to access a movieClip inside slime, i give it monster for the instance name, please help !

Download Link : http://www.mediafire.com/download/hz5tptkgftwdipw/Tower_Defense.rar

It's only 15KB and using CS6 Please help !


回答1:


Turn on Debugging

The code you're sharing is more than you probably need (.rar file included). To find the cause of the problem you (and those on StackOverflow) need to know what line you're programming is running into this error. If you're using Flash IDE CS6, the can be enabled by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...

null object reference at myDocument/doSomething()

...to a much clearer...

null object reference at myDocument/doSomething() package\myClass.as:20

...which now denotes which line in your code to look for your issue.


Use the Debug Console

Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.


Enemy.monster

This is the crux of the issue: somewhere, you're calling on Enemy.monster, and there is no property on your Enemy class that's called that (method or otherwise).



来源:https://stackoverflow.com/questions/19762607/1119-access-of-possibly-undefined-property-monster-through-a-reference-with-sta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!