2013-04-22 88 views
1

我有兩個應用程序,一個是試用版,另一個是完整版遊戲,都是用Adobe Air製作的。雖然通過sharedobjects解決方案保存數據不成問題,但我希望爲這兩個appsm使用「one」savegame,以便用戶在升級到完整版時可以保持其進度。我嘗試了一下。但是代碼如...:Air android/mobile:相同的sharedobject爲不同的應用程序?

SharedObject.getLocal(「myApp」,「/」);

...不工作。所以問題是,有沒有辦法讓兩個Air應用程序使用同一個共享對象?或者,如果不使用,至少「讀」另一個空氣應用程序的共享對象?

由於提前, ANB_Seth

+0

我不確定Android的沙箱應用程序是否與iOS相似,但是這樣做是不可能的。 – crooksy88 2013-04-22 12:57:32

回答

0

答案是肯定的,我實際上是通過網絡連接和6位散列用戶在新安裝的應用程序進入,以獲取做了一個遊戲傳輸系統爲iOS和Android 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)); 
} 
相關問題