Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Run Function on enter frame

Explorer ,
Dec 20, 2016 Dec 20, 2016

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?

TOPICS
ActionScript
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2016 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 20, 2016 Dec 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2016 Dec 20, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 20, 2016 Dec 20, 2016

You're probably right.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 21, 2016 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2016 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 23, 2016 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 23, 2016 Dec 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.

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 02, 2017 Jan 02, 2017
LATEST

FYI - you can also use Enter_Frame like you did, but just remove it in the function, and it will only run once:

someObject.addEventListener(Event.ENTER_FRAME, runOnce);

function runOnce(e:Event):void

{

  someObject.removeEventListener(Event.ENTER_FRAME, runOnce);

  trace("runOnce");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 23, 2016 Dec 23, 2016

Kglad, Robdillion - Thanks for your assistance

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 23, 2016 Dec 23, 2016

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 24, 2016 Dec 24, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines