Air android / mobile:针对不同应用的相同共享对象?

时间:2013-04-22 05:23:45

标签: android flash mobile air shared-objects

我有两个应用程序,一个是试用版,另一个是完整版的游戏,都是用adobe air制作的。虽然通过sharedobjects解决方案保存数据没有问题,但我想为这两个appsm使用“one”savegame,因此用户可以在升级到完整版时保持其进度。我试了一下。但代码如,例如......:

SharedObject.getLocal的( “对myApp”, “/”);

......不起作用。所以问题是,有没有办法让两个Air应用程序使用相同的共享对象?或者,如果不使用,至少“读取”另一个Air应用程序的共享对象?

提前致谢, ANB_Seth

1 个答案:

答案 0 :(得分:0)

答案是肯定的,我实际上是通过网络连接为iOS和Android制作了一个游戏传输系统,用户必须在新安装的应用程序中输入6位哈希来从服务器获取SO。您可以使用本地存储在SD卡或其他本地存储设备上的简单文件来执行此操作。

/**
 * send this user's current save data to the server
 */
public function send():void{

    var ba:ByteArray = new ByteArray();

    // Main.sv.user - is the registerClassAlias object we write/read locally via SharedObject
    ba.writeObject(Main.sv.user);

    var name:String = Crypto.hash("Random Secrect Salt - typically user score, name, etc.");
    // create 6 digit hash
    var key:String = Crypto.hash(name).slice(0, 6).toUpperCase();

    var request:URLRequest = new URLRequest ( 'https://sharedobject.com/transfer/save/name/'+name+'/key/'+key );
    var loader: URLLoader = new URLLoader();
    request.contentType = 'application/octet-stream';
    request.method = URLRequestMethod.POST;
    request.data = ba;

    loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
        trace("error - network");
        onSaveRestoreEvent(1);
    });

    loader.addEventListener(Event.COMPLETE, function (evt:Event) {
        addChild(new BaseDialog("Save Sent To Server", "Your data has been sent to the server. To get this data back from the server " +
            "you will need your secret key. Please write this six digit key down:\n"+name));
    });

    loader.load( request );
}

/**
 * create a GET SO dialog
 */
public function receive():void{
    var text:Sprite = new Sprite();
    var textInput:TextInput = new TextInput();
    textInput.width = Constants.SCREEN_WIDTH-100;
    textInput.y = -50;
    text.addChild(textInput);

    var dialog:BaseDialog = new BaseDialog("Enter Secret Save Key", "Please enter your six digit secret save key in the field below, then press \"Get\".\n\n",
        "Get", function():void{
            text.removeChildren();

            var url:String = "https://sharedobject.com/transfer/get/name/"+textInput.text; //servlet url
            var request:URLRequest = new URLRequest(url);

            //get rid of the cache issue:
            var urlVariables:URLVariables = new URLVariables();
            urlVariables.nocache = new Date().getTime();
            request.data = urlVariables;
            request.method = URLRequestMethod.GET;

            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.BINARY;

            loader.addEventListener(Event.COMPLETE, function (evt:Event) {
                var loader:URLLoader = URLLoader(evt.target);

                var bytes:ByteArray = loader.data as ByteArray;
                bytes.position = 0;
                if(bytes.length <= 10 || !(bytes.readObject() is User)){
                    onSaveRestoreEvent(2);
                }else{
                    try{
                        bytes.position = 0;
                        Main.sv.user = (bytes.readObject() as User);
                        Main.sv.save();
                        onSaveRestoreEvent(0);
                    }
                    catch( e : EOFError ){
                        onSaveRestoreEvent(3);
                    }

                }
            });

            loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
                trace("error - network");
                onSaveRestoreEvent(1);
            });

            loader.load(request);

        },
        "Close", function():void{text.removeChildren();}, null, null, text);

    dispatchEvent(new CreateBaseDialogEvent(dialog));
}


/**
 * called after the restore save system is done
 * @param prompt int [0 = complete][1 = error network][2 = error key][3 = error EOF]
 */
private function onSaveRestoreEvent(prompt:int):void{
    var dialog:BaseDialog;
    if(prompt == 0){
        dialog = new BaseDialog("Restore Complete!", "All save data has been restored.");
    }else if(prompt == 1){
        dialog = new BaseDialog("Network Error!", "Please seek an internet connection and try again.");
    }else if(prompt == 2){
        dialog = new BaseDialog("Invalid Secret Key!", "The key you've entered seems to be invalid, or the save data has expired on the server. " +
            "Data only lasts on the server for 24 hours.");
    }else{
        dialog = new BaseDialog("Error!", "There was an issue getting the file from the server. Please try the transfer again.");
    }
    dispatchEvent(new CreateBaseDialogEvent(dialog));
}