#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0

    Add EventListener on return to 1st frame


    I'm not sure exactly what's going on here (maybe I'm not understanding how the timeline executes?).

    I have a game that has the menu in the first frame, the game in later frames. When the game is won, the user is able to click to return to the menu. Using trace(), I've found that the click reads, and it starts to execute the actions that have been written in the 1st frame. But, when it gets to the line that adds an event listener to a button on the stage using solo.addEventListener(MouseEvent.CLICK,goSolo) (solo is the name of the button on the screen), it returns:

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

    The line executes just fine when the whole thing initializes, so I don't know why it's not working when it restarts. Is the code getting read before the stage is populating when I return the user to the main menu using gotoAndPlay(1) from the click listener? It doesn't make a difference if I use gotoAndStop(1). What's a good workaround?

    Or is it because the button is in a layer lower than the actions? (I've always put my actions in the highest layer, and don't remember ever having this problem before). But why then would it still work at the very beginning but not with the restart?

    Thanks for any suggestions.
    Last edited by ahillecke; May 25th, 2011 at 04:41 PM. Reason: discovered more specific root problem
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Hi, it doesn't matter which layer you put your code on. Try tracing solo and the actual function to see if they both still exist:

    trace("Solo:", solo);
    trace("goSolo:", goSolo);
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0
    this is what it returns when I trace that way:

    solo: null
    goSolo: function Function() {}

    Any idea why solo would become null after gotoAndPlay(1)? When the movie initially goes, it traces (correctly):

    solo: [object SimpleButton]
    goSolo: function Function() {}
  6. #4
  7. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    If you have it on more than one keyframe then make sure it has the instance name "solo" on them all. Does the button even exist when you are adding the event listener later on? If it's not on the stage at that point in time then it will be null.
    Quis custodiet ipsos custodes?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0
    The button is only in the first frame.

    When the user clicks on the stage at the end of the game (pretty much any frame not the 1st), the playerhead is moved back to frame 1. The script trying to add the listener is also in frame 1, and the scripts before it , which are in frame 1, are being run without a hitch (traces still work up to the addEventListener).

    I don't know if this is an indicator of anything, but the visual of the stage doesn't ever get to frame one, even though the frame 1 script is apparently executing. That's why I'm wondering if I misunderstand the timeline execution...does it read the script for the frame before it rerenders the stage, meaning that the button wouldn't be there 'til later?
  10. #6
  11. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    No, it's probably because of the error that it is stopping the loading process for the rest of the frames assets.

    Are you using this.gotoAndStop(1); to return to the first keyframe? That's what you should be doing otherwise it might be playing past the first keyframe faster than you can see.

    I'm like 99.9% sure having the code on a higher layer than the symbols makes no difference but you can always test that by dragging the code layer below the symbols.

    Try wrapping the addListener line in a conditional, just for a quick test to see if the rest of the frame loads as expected graphically:
    Code:
    if(solo != null)
       {
          solo.addEventListener(MouseEvent.CLICK, this.goSolo);
       }
    Quis custodiet ipsos custodes?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0
    Hmm...now that's strange...

    Putting in that little blurb showed that when it comes back to the first screen, the solo button is missing. It doesn't matter if that's the button clicked to go into the game or not...

    I'll have to do some digging to figure out why it's disappearing...

    Thanks for helping me find that.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0

    Disappearing Button...


    So my efforts to find out why the button is disappearing are so far fruitless.

    I made solo keep it on frame 1, generate a pair of cards, and pretty much execute the game in frame 1...I've tried combinations of all matches, no match, match then no match so make the game "finish", and never got it to make the button disappear (all the game code was executed except going to the game frames). I even made it go to the game frames and return to the 1st frame without doing anything else, and the button still stayed.

    Any ideas? It's the ONLY thing that's disappearing.
  16. #9
  17. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Why not keep it there through the animation but set it to be not visible? So have a keyframe with solo on it at the start, then have normal frames till the end. On the second frame, on the ActionScript layer do:

    solo.visible = false;

    That will make it invisible and non mouse responsive. Then on the first keyframe make it visible:

    solo.visible = true;

    That way it should never be removed from the stage.
    Quis custodiet ipsos custodes?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    27
    Rep Power
    0
    Not sure what exactly I did, but I rearranged the scripts a bit, and removed the call to go to the 2nd frame (instead, make the game initialize completely within the starting button rather than letting the frame do it), and it seems to work.

    Now on to the next task ...
  20. #11
  21. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Neat, glad you got it sorted.
    Quis custodiet ipsos custodes?

IMN logo majestic logo threadwatch logo seochat tools logo