Copy link to clipboard
Copied
I am trying to make a game right now. When you successfully finished the current level it should unlock the next level. I've been googling since this morning and tried using SharedObject but it won't solve my problem. I really need help please! Can someone please give me an idea on how I can do this? >.< Thank you.
It depends how you load your levels.
if you attach an event listener to Buttonbs that load your level , you could
add sth. like
function init():void{
var maxLevels:int= int(showCookie());
var totalLevels:int = 4
//say it returns "2"
for (var i:int = 1; i<=totalLevels;i++){
if(i<=maxLevels){
getChildByname("level"+i).visible = true;
}
else{
getChildByname("level"+i).visible = false;
}
}
}
and in your MouseEvent.CLICK, startLevel that you add to all the butt
...Copy link to clipboard
Copied
If you want the user to be able to play the unlocked levels the next time he visits your game, LocalSharedObject is a good way to save his progress.
var cookie:String;
var LSO:SharedObject = SharedObject.getLocal("cams_zalzos_game");
//public function to set the variable in the SharedObject
function setVal(key:String, val:String)
{
PM_LSO.data.pm_user[key] = val;
//trace(key + " set to " + val);
PM_LSO.flush();
}
function getVal(key:String):String
{
return PM_LSO.data.pm_user[key];
}
function showCookie():String{
cookie =getVal("maxLevel");
//trace("COOKIE:"+cookie);
return cookie;
}
//HOW TO USE IT
//If your user finishes Level 1 you call
//setVal("maxLevel","2");
//if he finishes Level 2 you call
//setVal("maxLevel","3"); etc.
//When your game starts you detect the users progress
//and making the next levels (or buttons that lead to your next Level)visible
//similar to this (adapt it to your needs)
function init():void{
var maxLevels:int= int(showCookie());
//say it returns "2"
for (var i:int = 1; i<=maxLevels;i++){
getChildByname("level"+i).visible = true;
}
}
Copy link to clipboard
Copied
Thank you for the reply. What if my levels are all external swfs that I have connected in the Game Menu. Will the code still be the same?
Copy link to clipboard
Copied
It depends how you load your levels.
if you attach an event listener to Buttonbs that load your level , you could
add sth. like
function init():void{
var maxLevels:int= int(showCookie());
var totalLevels:int = 4
//say it returns "2"
for (var i:int = 1; i<=totalLevels;i++){
if(i<=maxLevels){
getChildByname("level"+i).visible = true;
}
else{
getChildByname("level"+i).visible = false;
}
}
}
and in your MouseEvent.CLICK, startLevel that you add to all the buttons
you write sth. like
function startLevel(e:MouseEvent):void{
var levelLoader:Loader = new Loader();
levelLoader.load(new URLRequest("level"+e.currentTarget.name.substr(5,1)+".swf");
}
Copy link to clipboard
Copied
It is working now! Thank you for the great help!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now