Skip to main content
Participating Frequently
April 11, 2023
Answered

Need help with code issues

  • April 11, 2023
  • 1 reply
  • 509 views

I'm making a simple platformer game for a project, and have everything working fairly well, but when I added a section to respawn the character when they fall off the map, I get an error "TypeError: Error #1009:Cannot access a property or method of a null object reference. at DRAFTEVERYTHING_fla::MainTimeline/fl_MoveInDirectionOfKey()" and it makes the code repeat, causing the game to go in 2x speed, and it increases every time you fall off. My main code segment looks like this

 

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;

var xSpeed:Number = 0;
var jumpMulti = 25;
var xSpeedMulti:Number = 4.5;
var xSpeedDiv:Number = .75;

var backPosX:Number = 0;
var backPosY:Number = 800;

var sideCheckHit:Boolean = false;
var leftRightSwitch:Boolean;

var ySpeed:Number = 0;
var gravityMulti:Number = 2;
var groundCheckHit:Boolean = false;


var death:Boolean = false;

player.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed && groundCheckHit == true)
{
ySpeed -= jumpMulti;
}
if (downPressed)
{
background.y -= 5;
}
if (leftPressed)
{
xSpeed -= xSpeedMulti;
}
if (rightPressed)
{
xSpeed += xSpeedMulti;
}

groundCheck.x = player.x -10;
groundCheck.y = player.y +80;

xSpeed *= xSpeedDiv;



backPosX -= xSpeed;
backPosY -= ySpeed;

background.x = backPosX;
background.y = backPosY;

hall.x = backPosX * .2;
hall.y = backPosY * .2;

if(background.collision.hitTestPoint(groundCheck.x, groundCheck.y, true))
{
groundCheckHit = true;
}
else
{
groundCheckHit = false;
}
if(groundCheckHit)
{
ySpeed =- 0;
}
else
{
ySpeed += gravityMulti;
}

if (background.collision.hitTestPoint(player.x, player.y, true))
{
sideCheckHit = true;
}
else
{
sideCheckHit = false;
}
if(sideCheckHit)
{
if(leftRightSwitch)
{
backPosX -= 7;
xSpeed = 0;
}
else
{
backPosX +=7;
xSpeed = 0;
}
}

if(background.respawn.hitTestPoint(player.x, player.y, true))
{
death = true;
}
else
{
death = false;
}
if(death ==(true))
{
MovieClip(root).gotoAndStop(1, "Death Scene");
}
}

Any suggestions? I'm not very good with code.

    This topic has been closed for replies.
    Correct answer kglad

    How would I go about doing that? 


    start by ticking "permit debugging" in the publish settings so your error message indicates that object that no longer exists.  then, a sloppy way of fixing things, is to use

    if(!whateverfailstoexist){

    return;

    }

     

    in the problematic function.

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 11, 2023

    Hi.

     

    This error is possibly happening when you navigate to the "Death Scene" and the enter frame event keeps trying to access an instance that is not available anymore.

     

    So you should remove the enter frame event and other events everytime you go to a different frame or scene.

     

    Please let us know.

     

    Regards,

    JC

    Respawn29Author
    Participating Frequently
    April 11, 2023

    I removed the enter frame event and it caused everything to stop working, I would need to change the enter frame to a different event so it plays once, but I have no idea how to do that

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 11, 2023

    You can keep the event if it is still needed but you have to make sure that the event handler function (fl_MoveInDirectionOfKey) won't try to access an instance that doesn't exist in the death scene.