Skip navigation
createflash@mail.ru
Currently Being Moderated

How to remove three pages from the library by one closeBtn ?

Sep 19, 2011 3:03 AM

closeBtn.visible = false;

 

var page1:Page1;

var page2:Page2;

var page3:Page3;

 

var pages:Array = new Array(page1,page2,page3);

 

m1_btn.addEventListener(MouseEvent.CLICK, showPage1);

function showPage1(e:MouseEvent):void

{

page1 = new Page1 ;

addChild(page1);

closeBtn.visible = true;

}

 

 

closeBtn.addEventListener(MouseEvent.CLICK, closePages);

function closePages(e:MouseEvent):void

{

 

switch (pages)

{

case pages[0] :

stage.removeEventListener(MouseEvent.CLICK,showPage1);

removeChild(page1);

break;

 

case pages[1] :

stage.removeEventListener(MouseEvent.CLICK,showPage2);

removeChild(page2);

break;

 

case pages[2] :

stage.removeEventListener(MouseEvent.CLICK,showPage3);

removeChild(page3);

break;

 

default :

}

}

m3_btn.addEventListener(MouseEvent.CLICK, showPage3);

function showPage3(e:MouseEvent):void

{

page3 = new Page3 ;

addChild(page3);

closeBtn.visible = true;

TransitionManager.start(page3, {type:Squeeze, direction:Transition.IN, duration:3, easing:Elastic.easeOut, dimension:1});

}

m2_btn.addEventListener(MouseEvent.CLICK, showPage2);

function showPage2(e:MouseEvent):void

{

page2 = new Page2 ;

addChild(page2);

closeBtn.visible = true;

TransitionManager.start(page2, {type:Squeeze, direction:Transition.IN, duration:3, easing:Elastic.easeOut, dimension:1});

}

 

Help me, please... Thank you.

 
Replies
  • Currently Being Moderated
    Sep 19, 2011 4:27 AM   in reply to createflash@mail.ru

    Try the following code (it can still be better but for now it should do the trick):

     

     

     

    closeBtn.visible = false;
    // hold instances in the array
    var pages:Array = [new Page1(), new Page2(), new Page3()];
    // all buttons have a single handler
    m1_btn.addEventListener(MouseEvent.CLICK, showPage);
    m2_btn.addEventListener(MouseEvent.CLICK, showPage);
    m3_btn.addEventListener(MouseEvent.CLICK, showPage);
     
    closeBtn.addEventListener(MouseEvent.CLICK, closePages);
     
    function showPage(e:MouseEvent):void
    {
        var page:Sprite;
        // choose page based on what button is clicked
        switch (e.currentTarget)
        {
            case m1_btn: 
                page = pages[0];
                break;
            
            case m2_btn: 
                page = pages[1];
                break;
            
            case m3_btn: 
                page = pages[2];
                break;
        }
        addChild(page);
        closeBtn.visible = true;
        TransitionManager.start(page, {type: Squeeze, direction: Transition.IN, duration: 3, easing: Elastic.easeOut, dimension: 1});
    }
     
    function closePages(e:MouseEvent):void
    {
        for each (var page:Sprite in pages)
        {
            if (this.contains(page))
                removeChild(page);
        }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 19, 2011 4:31 AM   in reply to createflash@mail.ru

    One way to do this would be to not add the pages to the array until you add them as children to the stage.  Then your close function can loop thru the array and close out each one.  Also, you can remove the showPage# event listeners when you add the page instead of when you remove it.  Tht will prevent someone from adding two of the same pages at the same time.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points