运行一个AS3的功能只有一次 [英] Run an AS3 function only once

查看:164
本文介绍了运行一个AS3的功能只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个AIR应用程序,我想知道是否有可能运行一个函数只有一次?

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.

感谢您的回答,

推荐答案

由于您使用的Adobe AIR,你有几种选择。

Since you're using adobe AIR, you have a few options.

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

  1. Write a file that contains your save data.

使用AIR,你可以写一个数据文件到用户的机器。这比共享对象的,因种种原因不能工作或坚持长期更持久。
 实际上,你可以序列化整类复杂对象的便于检索。

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.

下面是一个类,管理人员节省(为preferences或游戏存档等等)文件的例子:

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
    }
    

  • 使用的SQLite。

  • use SQLite.

    AIR可以创建和读取SQLite数据库,如果你的应用有一个需要存储的其他数据将是有意义的生活在一个表结构,它可能是有意义的你添加一个表,用于存储用户的preferences而像第一次运行的标志 - 如果你不使用的数据库为别的,虽然,那肯定是超杀

    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.

    了解更多有关的SQLite在AIR这里: 的http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d49.html

    Find out more about SQLite in AIR here: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d49.html

    这篇关于运行一个AS3的功能只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆