Skip to main content
Inspiring
December 20, 2016
Question

Run Function on enter frame

  • December 20, 2016
  • 6 replies
  • 2252 views

When I use the following code:

addEventListener(Event.ENTER_FRAME, ImportantFunction);

 

function ImportantFunction(event:Event):void // Main Function

{

    //Function in here

}       

I get the following error.

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

The program still works but the error concerns me.  I can call the same function using a button click or object change with no problems.

What am I doing wrong?

This topic has been closed for replies.

6 replies

robdillon
Participating Frequently
December 24, 2016

You're welcome, but the thanks goes to Ken, not me. He did all the work.

StivushkaAuthor
Inspiring
December 23, 2016

Kglad, Robdillion - Thanks for your assistance

kglad
Community Expert
Community Expert
December 23, 2016

you're welcome.

StivushkaAuthor
Inspiring
December 23, 2016

Kglad, Thanks that makes sense.  I understand the problem now.  I mistakenly thought ENTER_FRAME would run the code once and the never again but it doesn't so my code runs constantly when the conditions are met (I think thats whats happening)

Is there a Even Listener than will run the code only when the frame opens?

kglad
Community Expert
Community Expert
December 23, 2016

yes.

var alreadyExecuted:Boolean;

if(!alreadyExecuted){

alreadyExecuted=true;

runOnceF();

}

function runOnceF():void{

the code you want to run once.  or this can be placed within the above if-statement.

}

StivushkaAuthor
Inspiring
December 21, 2016

kglad - You may have a point the error doesn't actually point to that line.  It points to an if statement in the function.  However the program worked fine before I tried to run the function on frame load and it works fine when I remove the line so whatever the debugger says I'm sure its that line.

robdillion - I tried targeting a object or using ".this" still the same result.  All the other calls of the functions (change combo box click on button add text to field etc) work fine its just this one.

kglad
Community Expert
Community Expert
December 21, 2016

that means the object mentioned doesn't always (while your enterframe is running) exist.  ie, it may have existed when that frame first plays but the playhead may leave that frame and that object no longer exists.  meanwhile, you're enterframe loop continues to reference an object that no longer exist.

if you don't understand what's undefined and no longer exists, what's the line of code mentioned in the error message?

robdillon
Participating Frequently
December 20, 2016

You don't have that addEventListener() attached to anything. You need to reference to a named instance on the stage. That's what your null object reference is referring to. Here's a reference that should help: AS3: Event Handing

kglad
Community Expert
Community Expert
December 20, 2016

i don't see any problem using the implicit 'this'.

robdillon
Participating Frequently
December 21, 2016

You're probably right.

kglad
Community Expert
Community Expert
December 20, 2016

the code you posted wouldn't trigger that error.

click file>publish settings>swf>and tick 'permit debugging'.  retest.

the line number of the incorrect reference will be in the error message allowing you to find that code that is problematic.