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

Run Function on enter frame

Explorer ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

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

Views

2.0K

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

You're probably right.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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");

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Kglad, Robdillion - Thanks for your assistance

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

you're welcome.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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