0
votes

I have been working on a game in flash as3, which worked perfectly up until I rearranged all of the variables up at the top to make it easier to read. Now the debugger is throwing an Error 1009 for many of the variables during run time, despite their definitions not having changed.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main_fla::MainTimeline/Update()[Main_fla.MainTimeline::frame3:201]

The error points me to:

if (S) {
    legRot = 180;
}

Despite that fact that both S and legRot are defined as;

var legRot:Number = 0;

and

var S:Boolean = false;

with the rest of the variables at the top f the actions on the frame.

Any help is appreciated, Thank You.

Edit
Here are my variable definitions ;

//Numbers
var currentAmmo = 1;
var xVel = 0;
var yVel = 0;
var xCollision = 0;
var yCollision = 0;
var enemySpeed = 4;
var ExplodeOnce = 0;
var EnemyWalking = 0;
var legRot = 0;
var dropxVel: Number = 0;
var dropyVel: Number = 0; 
var E = 0;

//Booleans
var A:Boolean = false;
var W:Boolean = false;
var S:Boolean = false;
var D:Boolean = false;
var SHIFT:Boolean = false;
var Q:Boolean = false;
var dead = false;

And here is where the error is occuring;

    if (W) {
        legRot = 0;
    }
    if (A) {
        legRot = -90;
    }
    if (S) {
        legRot = 180;
    }
    if (D) {
        legRot = 90;
    }
    if (A && W) {
        legRot = -45
    };
    if (W && D) {
        legRot = 45
    };
    if (D && S) {
        legRot = 115
    };
    if (S && A) {
        legRot = -115
    };

    if (!dead){
        player.TopBody.rotation = Math.atan2(mouseY - player.y, mouseX - player.x) * 180 / Math.PI + 90;
        if (!W && !A && !S && !D) {
            player.BottomLegs.gotoAndStop(1);
            player.BottomLegs.rotation = Math.atan2(mouseY - player.y, mouseX - player.x) * 180 / Math.PI + 90;
        } else {
            player.BottomLegs.play();
            player.BottomLegs.rotation += (legRot - player.BottomLegs.rotation) / 4;
        }
    }
1
Please show some more code in frame3 you have. - Benny
Could you show the code of your Update() function ? - akmozo
the error isn't where you think it is. if (S) .. if S were null it would simply skip the conditional. legRot = 180; if legRot were null, it would set it there. To get that error, you must be call someObject.someProperty and someObject is null. I don't believe this has anything to do with your variables moving... more likely player is null... or player.BottomLegs is null. I would do line by line... function by function break points checking everything till you find the null object. You probably changed some other code somewhere and didn't think it affected anything... and it did :) - Jason Reeves
and for your sanity, I have seen my debugger tell me that my error was in the white space located between function blocks... and even once in a comment line. Needless to say that is not so helpful! It happens rarely, but it does happen. - Jason Reeves
Thanks, its nice to know its not just me! I am fairly certain it is not the player or BottomLegs, as they are both movieclip instance names, but I will double check just to be sure. - Jed

1 Answers

0
votes

Found the problem. I accidentally added an Event Listener to a timer before the timer was created up with the variable definitions. Why this was throwing errors in the update function, which is a completely different listener, is beyond me. But its fixed now.
Thank you all!