Skip to main content
Participant
May 20, 2011
Pregunta

Trying to clear many instances from stage at once

  • May 20, 2011
  • 2 respuestas
  • 665 visualizaciones

I am making a flashcard game with five subjects.  It has buttons for moving forward and back,and buttons for switching to another subject.  The card instances for each subject are stored offstage, in separate frames (five in all), and their names are stored in arrays (five in all).  Each time a button is pressed, the appropriate card tweens onto the stage.

When a user goes back to the menu frame, any instances brought to stage up until that point were still visible (not sure why, since the menu's on it's own frame), so I wrote a function to clear everything offstage whenever the menu button is clicked.

My problem is weird: if you are in the math subject, switching to geography returns this error:

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

at flashcardapp1_fla::MainTimeline/frame3()

at flash.display::MovieClip/gotoAndStop()

at flashcardapp1_fla::MainTimeline/subjectselected()

at flashcardapp1_fla::MainTimeline/selectgeo()

After that, you can still move from card to card, but clicking to go back to the menu freezes everything.

Is there some easier way to clear instances from the stage that I am missing?

I think my error must be in this code here, but I'm at a loss to find it.  Please help!  Still very new to Flash.

function gotomenu (e:MouseEvent):void      {instancehide();      gotomenubeenclicked = true;      gotoAndStop(1);}

function selectgeo(e:MouseEvent):void      {subj = "geo";      relevantframe = 3;      subjectselected();      geobeenclicked = true;      }

function subjectselected():void      {instancehide();      currentcard = subj + limitfaces + currentcard.substr(7,8);      if (currentFrame > 1)           {gotoAndStop(relevantframe);           childindex();           slidefromrightfunc();}}

var grmbeenclicked:Boolean var latbeenclicked:Boolean var matbeenclicked:Boolean var geobeenclicked:Boolean var hisbeenclicked:Boolean function instancehide():void      {if (matbeenclicked == true)           {for (var imatfrnt:uint = 0; imatfrnt<allmat.length; imatfrnt++)                {var matelement:MovieClip = allmat[imatfrnt] as MovieClip;                matelement.x = hiddenx;                matelement.alpha = 0;}           }      if (geobeenclicked == true)           {for (var igeofrnt:uint = 0; igeofrnt<allgeo.length; igeofrnt++)                {var geoelement:MovieClip = allgeo[igeofrnt] as MovieClip;                geoelement.x = hiddenx;                geoelement.alpha = 0;}           for (var igeomaps:uint = 0; igeomaps<allmaps.length; igeomaps++)                {var mapelement:MovieClip = allmaps[igeomaps] as MovieClip;                mapelement.x = hiddenx;                mapelement.alpha = 0;}           }      grmbeenclicked = false;      latbeenclicked = false;      matbeenclicked = false;      geobeenclicked = false;      hisbeenclicked = false;      }

Este tema ha sido cerrado para respuestas.

2 respuestas

relaxatraja
Inspiring
May 23, 2011

If you want to remove everything on stage:

for (var i:int=stage.numChildren-1;i>=0;i--){
    stage.removeChildAt(i);
}

Whatever you added using addchild will be removed.

kglad
Community Expert
Community Expert
May 20, 2011

if you're using actionscript to create objects OR add them to the diplay list (eg, using addChild() ), you'll need to use actionscript to remove them.  otherwise, you only need to create a blank keyframe after the frame(s) that contain the object to have that object no longer appear on the timeline.