user1610980
user1610980

Reputation: 55

AS3 Error #1009: Cannot access a property or method of a null object reference

Basically, I'm trying to make a randomly generated character follow a series of waypoints to get to where he needs to go without walking into walls etc on stage.

I'm doing this by passing an Array of Points from the Engine to the Character's followPath function (this will be on a loop, but I haven't gotten to that stage yet).

A part of this followPath function is to detect when the character is close enough to the waypoint and then move on to the next one. To do this, I'm trying to use Point.distance(p1,p2) to calculate the distance between the currently selected waypoint, and a point that represents the Character's current position.

This is where I'm running into this problem. Trying to update the current (x,y) point position of the Character is proving difficult. For some reason, the Point.setTo function does not seem to exist, despite it being in documentation. As a result, I'm using

        currentPos.x = x;
        currentPos.y = y;
        //update current position point x and y

to try and do this, which is where my 1009 error is coming from.

Here is my full Character class so far:

package  {
import flash.display.MovieClip;
import flash.geom.Point;

public class Character extends MovieClip {

    public var charType:String;
    private var charList:Array = ["oldguy","cloudhead","tvhead","fatguy","speakerhead"];
    private var numChars:int = charList.length;
    private var wpIndex:int = 0;
    private var waypoint:Point;
    private var currentPos:Point;
    private var wpDist:Number;
    private var moveSpeed:Number = 5;

    //frame labels we will need: charType+["_walkingfront", "_walkingside", "_walkingback", "_touchon", "_touchonreaction", "_sitting"/"_idle", "_reaction1", "_reaction2", "_reaction3", "_reaction4"]

    public function Character() {
        trace("new character:");
        charType = charList[Math.floor(Math.random()*(numChars))];
        //chooses a random character type based on a random number from 0-'numchars'
        trace("char type: " + charType);

        gotoAndStop(charType+"_walkingfront");

        x = 600;
        y = 240;
    }

    public function followPath(path:Array):void {
        if(wpIndex > path.length){ //if the path has been finished
            gotoAndStop(charType+"_sitting"); //sit down
            return;//quit
        }

        waypoint = path[wpIndex];
        //choose the selected waypoint
        currentPos.x = x;
        currentPos.y = y;
        //update current position point x and y

        wpDist = Point.distance(currentPos,waypoint);
        //calculate distance

        if(wpDist < 3){ //if the character is close enough to the waypoint
            wpIndex++; //go to the next waypoint
            return; //stop for now
        }
        moveTo(waypoint);
    }

    public function moveTo(wp:Point):void {
        if(wp.x > currentPos.x){
            currentPos.x += moveSpeed;
        } else if(wp.x < currentPos.x){
            currentPos.x -= moveSpeed;
        }

        if(wp.y > currentPos.y){
            currentPos.y += moveSpeed;
        } else if(wp.y < currentPos.y){
            currentPos.y -= moveSpeed;
        }
    }

}

}

Can anybody explain to me why this is happening? It's a roadblock that I haven't been able to overcome at this stage.

I'm also curious if anybody can provide information as to why I can't use the phantom Point.setTo method.

Upvotes: 0

Views: 503

Answers (2)

AturSams
AturSams

Reputation: 7942

The problem is that your are not using the Point constructor first. When you create a variable that is not a simple data type (Int, Number, String ...) you must call the constructor first and assign properties to the fields of the object only afterwards. This is because you must initialize an instance of the class Point before accessing it's properties. The same will be true with any other class.

http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29

"In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use.."

Basically, you did not prepare a new Point object.

In this example, during the constructor (public function Character)

public function Character() {
        //add these lines (you can omit the zeroes as the default value is zero)
        //I added the zeroes to show that the constructor can set the initial values.
        wayPoint = new Point(0, 0);
        currentPos = new Point(0, 0);
        trace("new character:");
        charType = charList[Math.floor(Math.random()*(numChars))];
        //chooses a random character type based on a random number from 0-'numchars'
        trace("char type: " + charType);

        gotoAndStop(charType+"_walkingfront");

        x = 600;
        y = 240;
    }

remember every new object identifer references NULL (nothing) until you construct an object or do something like this

var pointA = pointB;
//where pointB is already not null
//You can also check this
if(currentPos != null)
{
   currentPos.x = X;
   currentPos.y = Y;
}

currentPos will not be null if you use a constructor first.

Good luck.

Upvotes: 1

Sr.Richie
Sr.Richie

Reputation: 5740

You're trying to assign x and y properties of a Point object that doesn't exist.

You have to create your Point:

currentPos = new Point ();

and then assign x and y

Upvotes: 3

Related Questions