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.
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);
}
}
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.
North America
Europe, Middle East and Africa
Asia Pacific