问题
I'm making an AIR app and I would like to know if it's possible to run a function only once ?
If the user reboot his device and launch the app, the function won't be trigger again.
Thank you for your answers,
回答1:
Since you're using adobe AIR, you have a few options.
Write a file that contains your save data.
Using AIR you can write a data file to the user's machine. This is more permanent than SharedObject's, which for various reasons can not work or persist long term.
You can actually serialize entire classes for easy retrieval of complex objects.Here is an example of a class that managers a save file (for preferences or a game save etc):
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(); } } }
Then to use it:
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); }
use a SharedObject. There is another answer covering this, but since no example was given I'll give one:
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 }
use SQLite.
AIR can create and read SQLite databases, if your application has a need to store other data that would make sense to live in a table structure, it might make sense for you to add on a table for storing user preferences and things like a first run flag - if you wouldn't use the database for anything else though, it would definitely be over-kill.
Find out more about SQLite in AIR here: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d49.html
来源:https://stackoverflow.com/questions/26842214/run-an-as3-function-only-once