I have a menu page with one button, which leads to another page by adding a child and removing one. Very simple. From here l have a page which allows many buildings to be clicked, which opens a new page (a mini game page). I have done this with setters and getters as shown below:
Main.as (gets value from the getter in MainPage)
public function onEnterFrame(event:Event):void
{
//Swaping pages with a getter and setter
if (mainPage.stage && mainPage.getNextLevel() == 1)
{
addChild(miniGameOne);
removeChild(mainPage);
}
if (miniGameOne.stage && miniGameOne.getNextLevel() == 2)
{
addChild(mainPage);
removeChild(miniGameOne);
}
}
MainPage.as (with all the buildings)
public function onAddedToStage(event:Event):void
{
doctor.addEventListener(MouseEvent.CLICK, onDoctorClick);
}
public function onDoctorClick(event:MouseEvent):void
{
setNextLevel(1);
}
public function setNextLevel(passLevel:int)
{
nextLevel = passLevel;
}
public function getNextLevel():int
{
return nextLevel;
}
MiniGameOne.as Here it says when the mini game is complete then set the page to 2, which is adding the MainPage.as and removing MiniGameOne.as
public function onEnterFrame(event:Event):void
{
healthbar.meter.scaleY = life/100;
if (life < 1)
{
life = 1;
//Make sure it isn't visiable
healthbar.meter.alpha = 0;
//New Function
gameComplete();
}
}
public function gameComplete()
{
//Level Complete, Set new Level
setNextLevel(2);
}
I have a problem, when l enter a page (clicking on a building) then return to the original page and click on the same building l can't open the same page again, can anyone explain what is happening here? Thanks.
That happens because you have defined and are checking nextLevel
for both MainPage
and MiniGameOne
classes. When you change the level in miniGameOne
instance to 2
it will be 2
until you change it to some other number.
From that point you have:
mainPage.getNextLevel() == 1
and
miniGameOne.getNextLevel() == 2
so in your enterFrame()
both conditions are true and your miniGameOne
is indeen added to stage but in the same frame it is removed and you mainPage
is added again.
As a quickest fix you can change the line:
if (miniGameOne.stage && miniGameOne.getNextLevel() == 2)
to
else if (miniGameOne.stage && miniGameOne.getNextLevel() == 2)
But this would probably leave your mini game only until next frame is reached so you need to reset nextLevel
value when you are back in mainPage
:
if (miniGameOne.stage && miniGameOne.getNextLevel() == 2){
addChild(mainPage);
removeChild(miniGameOne);
miniGameOne.setNextLevel(0);
}
However what you have here is very poor practice.
Fist of all, change only that parts of your program on frame event which you actually need to update on every frame, or you really don't have any other way to determine when it should be updated
Secondly, don't duplicate variables that store you app states.
There are many ways you can apporach this problem but I will show one that is probably closest to what you have already:
1 Define level state in your Main
class, store your levels in array/vector/whatever, pass instance of main to your levels.
/**Current level**/
private var cl:uint = 0;
/**All levels veiews that could be shown.**/
private var levels:Array = [mainPage, miniGameOne];
public function Main() {
for each (var l in levels) l.main = this;
}
public function get level():uint { return cl; }
public function set level(l:uint) {
this.removeChild(levels[cl]);
this.addChild(levels[cl = l]);
}
2 In your level classes define setter for main instance, you now can change current level stored in main.
private var m:Main;
public function set main(v:Main):void { m = v; }
public function onDoctorClick(event:MouseEvent):void{
m.level = 1;
}
public function gameComplete(){
//Level Complete, Set new Level
m.level = 0;
}
3 All other code, especially on frame event, can be removed.
The solution may not be ideal but I hope it will give you some idea.