locked
[Solved] Problems with WinJS LocalFolder

    Question

  • Hi All!

    I was developing a game for Windows Store and took some final steps to get it done. The last thing is storing and reading users data, I used LocalFolder for this task and this is my code with WinJS and newest CreateJS.
    (function () {
    
    function Gamex(preload,stage,typex,target,levelx,scorex) {
            this.Container_constructor();
            this.stringData;
            this.setup()
    
    }
    var p = createjs.extend(Gamex, createjs.Container); p.setup = function () { Windows.Storage.ApplicationData.current.localFolder.getFileAsync(Constants.gameStorageGrid) .then(function (mainStore) { return Windows.Storage.FileIO.readTextAsync(mainStore); }).done(function (mTxt) { this.stringData = mTxt; console.log(this.stringData);
    ////result is 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 });
    this.startGame(); } p.moveObjects = function(){ console.log(this.stringData) //result is undefined } window.Gamex = createjs.promote(Gamex, "Container"); }());

    I can read "stringData" in done function but the result is undefined when calling it outside, for this case in moveObjects function.
    My main background is AdobeAir/Starling and this is my first game with HTML/JS. Is there anything went wrong with my code or I did not understand Promises clearly. Any help would be very appreciated!

     
      






    Friday, March 27, 2015 2:43 AM

Answers

  • My bad, it seems to be all about the scopes and the structure of my code. After taking a look on here *http://stackoverflow.com/questions/15572375/how-to-read-image-from-application-folder-in-winjs*, I can get the string data from a promise. My new code:
    p.getLocalData = function() {
            return new WinJS.Promise(
                function (completed, error, progress) {
                   
                    var sourceFolder;
    
                        var appData = Windows.Storage.ApplicationData.current;
                        sourceFolder = appData.localFolder;
                        
                        sourceFolder.getFileAsync(Constants.gameStorageGrid).then(function (file) {
                            completed(Windows.Storage.FileIO.readTextAsync(file));
                        },
                        function (error) {
                            
                        });
                    
                }
            );
        }
    
    
    And call it
    this.stringData = this.getLocalData();
    
    this.stringData.done(function (mTxt) { 
    
    console.log(mTxt);
    
    })

    Have a nice day! :)



    Friday, March 27, 2015 7:48 AM

All replies

  • My bad, it seems to be all about the scopes and the structure of my code. After taking a look on here *http://stackoverflow.com/questions/15572375/how-to-read-image-from-application-folder-in-winjs*, I can get the string data from a promise. My new code:
    p.getLocalData = function() {
            return new WinJS.Promise(
                function (completed, error, progress) {
                   
                    var sourceFolder;
    
                        var appData = Windows.Storage.ApplicationData.current;
                        sourceFolder = appData.localFolder;
                        
                        sourceFolder.getFileAsync(Constants.gameStorageGrid).then(function (file) {
                            completed(Windows.Storage.FileIO.readTextAsync(file));
                        },
                        function (error) {
                            
                        });
                    
                }
            );
        }
    
    
    And call it
    this.stringData = this.getLocalData();
    
    this.stringData.done(function (mTxt) { 
    
    console.log(mTxt);
    
    })

    Have a nice day! :)



    Friday, March 27, 2015 7:48 AM
  • Thanks for sharing the solution. :) Nice day

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 30, 2015 12:06 PM
    Moderator