仅运行一次AS3功能

时间:2014-11-10 10:58:31

标签: actionscript-3 function air

我正在制作AIR应用,我想知道是否可以只运行一次功能?

如果用户重启设备并启动应用程序,则该功能不会再次触发。

感谢您的回答,

1 个答案:

答案 0 :(得分:0)

由于您使用的是adobe AIR,因此您有几个选择。

  1. 编写包含保存数据的文件。

    使用AIR,您可以将数据文件写入用户的计算机。这比SharedObject更持久,由于各种原因,它不能长期工作或持久存在  实际上,您可以序列化整个类,以便轻松检索复杂对象。

    以下是管理保存文件的类的示例(用于首选项或游戏保存等):

    package  
    {
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;
    import flash.net.registerClassAlias;
    
    public class SaveData 
    {
    
        //a list of any vars you want to save.
        public var firstRun:Boolean = true;
        public var someVar:int = 1;
        public var anotherVar:Dictionary;
    
    
        //the file for the user save data
        public static function getFile():File {
            return File.applicationStorageDirectory.resolvePath("saveFile.data");
        }
    
        public static function loadFile():SaveData {
            registerClassAlias("flash.utils.Dictionary", Dictionary); //you need to register any non-native classes you want to save 
            registerClassAlias("saveData", SaveData);
    
            var data:SaveData 
    
            //check for a default save file in the application directory (optional, but sometimes it's nice to bundle a default with your application before the user saves their own data
            var f:File = new File("app:/saveFile.data");
    
            if(f.exists){
                data = loadFromFile(f);
            }else {
                trace("Default File Doesn't Exist");
            }
    
            //check the user storage directory for a save file, and see if it's newer than the one in the application directory
            var userFile:File = getFile();
            if (userFile.exists) {
                if (f.exists && f.modificationDate.time < userFile.modificationDate.time) {
                    trace("Local File is newer, load it");
                    data = loadFromFile(userFile);
                }else {
                    trace("Default File is newer");
                }
            }else {
                trace("Save File DOESN'T EXIST YET");
            }
    
            if (!data) {
                //data didn't load, let's create a new save file
                data = new SaveData();
                saveToFile(data);
            }
    
            saveData = data; //assing to main static accessible var
            return data;
        }
    
        private static function loadFromFile(f:File):SaveData {
            var d:SaveData;
            try{
                var stream:FileStream = new FileStream();
                stream.open(f, FileMode.READ);
                d = stream.readObject() as SaveData;
                stream.close();
                trace("DATA LOADED: " + f.nativePath);
    
                return d;
            }catch (err:Error) {
                trace("Error Loading Save Data: " + f.nativePath);
            }
    
            return null;
        }
    
    
        public static function saveToFile(saveData:SaveData):void {
            var fileStream:FileStream = new FileStream();
            fileStream.open(getFile(), FileMode.WRITE);
            fileStream.writeObject(saveData);
            fileStream.close();
        }
    
    }
    }
    

    然后使用它:

    var saveData:SaveData = SaveData.loadFile();
    
    if(saveData.firstRun){
        //do you first run code
    
        //now set first run to false
        saveData.firstRun = false;
        SaveData.saveFile(saveData);
    }
    
  2. 使用SharedObject。还有另一个答案涵盖了这一点,但由于没有给出一个例子,我将给出一个:

    var mySO:SharedObject = SharedObject.getLocal("yourAppNameHere");
    if(mySO.data.hasHadFirstRun){
        //the cookie exists
    }else{
        //the cookie hasn't indicated there has been a first run yet, so do whatever you need to do    
        //some code
        mySO.data.hasHadFirstRun = true;  //now that you've done whatever on the first run, let's save the cookie
        mySO.data.flush(); //save (though it will save automatically I believe when you close the application
    }
    
  3. 使用SQLite。

    AIR可以创建和读取SQLite数据库,如果您的应用程序需要存储在表结构中有意义的其他数据,则可能需要在表上添加用于存储用户首选项和类似的内容第一次运行标志 - 如果你不会将数据库用于其他任何事情,那肯定会过度杀人。

    在此处了解有关AIR中SQLite的更多信息: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d49.html