-1

Basically I wanted to make a simple test game using the flashpunk framework but when I just started I encountered this runtime error : [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference. Fault, HandleInput() at Player.as:47

I know that means that I am accesing a method from a null object , but I initialized the player object in Main.as and I dont see any error in the code , so whats causing this ?

here is the player class :`import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.Graphic; import net.flashpunk.graphics.Image;

/**
 * ...Player class , manages physics , input and other stuff
 * @author Mohamed Atef
 */
public class Player extends PhysicsEntity
{
    //Input Variables

    //Movement Variables
    /**
     * The velocity by which the Player will move in the x-axis .
     */
    var movementVelocity : Number;

    //Embedding .
    [Embed(source = "../../Assets/Sprites/Player/Head.png")]
    private const sprite_IMG : Class;

    public function Player() 
    {
        x = 10;
        y = 10;
        graphic = new Image(sprite_IMG);
        enableGravity = true;
    }

    private function HandleInput():void
    {
        if (Input.check(Key.LEFT))
        {
            velocity.x = -movementVelocity;
        }
        else if (Input.check(Key.RIGHT))
        {
            velocity.x = movementVelocity;
        }
        else
        {
            velocity.x = 0;
        }
    }

    override public function update():void 
    {
        HandleInput();
        HandlePhysics();
    }

}`

And here is the Main.as where I created the player and added it to the world .

import Classes.Entitys.Player;
import Classes.Levels.Level;
import flash.display.Sprite;
import flash.events.Event;
import net.flashpunk.Engine;
import net.flashpunk.FP;
import net.flashpunk.World;

/**
 * ...
 * @author Mohamed Atef
 */
public class Main extends Engine
{
    var player : Player;

    public function Main():void 
    {
        super(800, 600, 60, false);
        player = new Player();
        trace (player);
        FP.world = new Level();
    }

}

Iam new to ActionScript 3.0 and flashpunk and this is the first project , but Iam not new to programming though .

2 Answers2

0

Its seems your error is trying to use a method Input.check - check if the object or class Input is not null, yo

0

I found the solution , some variables were null , the method and the player or the Input objects has nothing to deal with it .

I kinda got misleaded from the error thinking that this means the "Function" itself is being called from
a null object , I think because my first AC3 project and new with FlashDevelop IDE ( coming from C# visual studio ) .