Creating Bloons in Flash

Tutorial parts

Part 5: Sounds and Saving

Bookmark and Share

More managers!

Making classes that manage things simplifies your game code so much as we saw with the scenery components. For saving and sound we do the same thing again.

Our Save Manager

This class uses a SharedObject which is like a cookie to save what level the user is up to, and to return that level as an integer. We use it in the LevelSelect screen from part 4.

With this code we can save at any time by calling SaveManager.Save(levelnumber) and see what level the user is up to via SaveManager.Get().

package Doubloons
{
    import flash.net.SharedObject;

    public class SaveManager
    {
        public static function Save(level:int):void
        {
            // Check if we have already saved the level
            var savedlevel:int = Get();
            
            // If our saved level is the same or higher we just leave
            if(savedlevel >= level)
                return;
                
            // Otherwise save it
            var cookie:SharedObject = SharedObject.getLocal("level");
            cookie.data["level"] = level.toString();
            cookie.flush();
        }
        
        public static function Get():int
        {
            var cookie:SharedObject = SharedObject.getLocal("level");
            
            if(cookie.data["level"] == undefined)
                return 1;
            else
                return int(cookie.data["level"]);
        }
    }
}

Our Sound Manager

This is a very simple piece of code, we just feed it a sound object's class and it will play it if the sound is on. Here you will see we finally use the public static var SoundOn from the document class.

package Doubloons
{
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;
    
    public class SoundManager
    {
        public static function Play(sound:Class):void
        {
            if(Main.SoundOn)
            {
                var soundchannel:SoundChannel = new sound().play(0, 0);
            }
        }
    }
}

Tutorial parts