mayhew
mayhew

Reputation: 5

Error playing a frame for the third time

I have a flash file where the first frame contains two buttons, one of which takes the user to the second frame, and the other takes them to the third frame. At each of those frames, various text fields and variables can be manipulated via SimpleButtons. Both Frame 2 and Frame 3 have "back" buttons to take them back to Frame 1.

Currently when the user navigates back to Frame 1 for the second time (thus playing it for a third time), my second button appears to no longer exist, and I receive an error. Both buttons on Frame 1 were placed via the Flash IDE. Why is my button popping out of existence, when it did perfectly fine the previous two times? Below is my code for Frame 1. The "back" buttons simple remove event listeners and then call gotoAndStop(1)

var inited:Boolean;
var cache:SharedObject;
var libsans:Font = new libsansreg();

this.addEventListener(Event.ENTER_FRAME, frameEnter);
stats.addEventListener(MouseEvent.CLICK, statsclicked);
modules.addEventListener(MouseEvent.CLICK, modsclicked);

function initcache():void
{
    this.cache = SharedObject.getLocal("RPG_Shooter")
}

function frameEnter(e:Event):void
{
    if (!inited)
    {
        inited = true
        initcache()
        this.gotoAndStop(1)
    }
}

function statsclicked(e:MouseEvent):void
{
    this.removeEventListener(Event.ENTER_FRAME, frameEnter)
    stats.removeEventListener(MouseEvent.CLICK, statsclicked)
    modules.removeEventListener(MouseEvent.CLICK, modsclicked)
    this.gotoAndStop(2)
}

function modsclicked(e:MouseEvent):void
{
    this.removeEventListener(Event.ENTER_FRAME, frameEnter)
    stats.removeEventListener(MouseEvent.CLICK, statsclicked)
    modules.removeEventListener(MouseEvent.CLICK, modsclicked)
    this.gotoAndStop(3)
}

Upvotes: 0

Views: 52

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82028

I actually had similar problem at one point. It has to do with garbage collection which is not the best in Flash to begin with, but the IDE's compiler settings make it so much more insane. There are a couple of tricks you can try which might help.

  • Make sure you remove all listeners before leaving the frame.
  • Go to a "blank" frame (something which still has a background and styling, but no interact-able components) for even a 5/1000 of a second
  • Set the variable names to "null" on frame one (so if your component on frame 3 is named "foo" put foo = null on frame one)
  • put var foo:MovieClip on frame 1. repeat for all MovieClips which you might be using that are named ahead of time.

Upvotes: 1

Related Questions