2 Replies Latest reply on Jan 11, 2018 3:05 PM by JoãoCésar

    Write a file in execution (Save game)

    jbh16182640

      Hello , I want to save variables during the execution of the program (.exe) done with Animate in version 18.0.1, in order to save the game locally (offline) the progress of the player, but I do not know how to write file is it even possible ?

      Can you teach me or enlighten me on how to create a game save with Animate ?

        • 1. Re: Write a file in execution (Save game)
          Colin Holgate MVP & Adobe Community Professional

          The simplest solution is to use a SharedObject. Could you please read this article to get the general idea?:

           

          SharedObject - Adobe ActionScript® 3 (AS3 ) API Reference

           

          There are some lengthy examples at the end of the page, in reality it can be a lot less lines.

          • 2. Re: Write a file in execution (Save game)
            JoãoCésar Adobe Community Professional

            Hi.

             

            Besides of what Colin suggested, if you want to use AIR, here is an example:

             

            import flash.filesystem.File;
            import flash.events.Event;
            import flash.events.FocusEvent;
            import flash.events.IOErrorEvent;
            import flash.utils.ByteArray;
            import flash.events.MouseEvent;
            import flash.text.TextField;
            
            // save
            function save(filePath:String, saveString:String, completeFunction:Function, errorFunction:Function):void
            {
                var file:File = File.applicationStorageDirectory.resolvePath(filePath);
               
                file.addEventListener(Event.COMPLETE, completeFunction);
                file.addEventListener(IOErrorEvent.IO_ERROR, errorFunction);
                file.save(saveString);   
            }
            
            function fileSaved(e:Event):void
            {
                trace("Data saved.");
            }
            
            function fileSavingError(e:IOErrorEvent):void
            {
                trace(e.text);
            }
            
            // load
            function load(filePath:String, completeFunction:Function, errorFunction:Function):void
            {
                var file:File = File.applicationStorageDirectory.resolvePath(filePath);
               
                file.addEventListener(Event.COMPLETE, completeFunction);
                file.addEventListener(IOErrorEvent.IO_ERROR, errorFunction);
                file.load();
            }
            
            function fileLoaded(e:Event):void
            {
                var file:File = e.target as File;
                var bytes:ByteArray = file.data as ByteArray;
                var str:String = bytes.readUTFBytes(bytes.length);
               
                loadText.text = str;
               
                trace("Data loaded: " + str);
            }
            
            function fileLoadingError(e:IOErrorEvent):void
            {
                trace(e.text);
            }
            
            function saveHandler(e:FocusEvent):void
            {
                save("test.txt", (e.currentTarget as TextField).text, fileSaved, fileSavingError);
            }
            
            function loadHandler(e:MouseEvent):void
            {
                load("test.txt", fileLoaded, fileLoadingError);   
            }
            
            saveText.addEventListener(FocusEvent.FOCUS_OUT, saveHandler);
            loadText.addEventListener(MouseEvent.CLICK, loadHandler);
            
            load("test.txt", fileLoaded, fileLoadingError);
            

             

            Change the text field on the left to save its text and click on the text field on the right to load the saved text.

             

            FLA download: save_and_load.zip - Google Drive

            Official reference: Adobe AIR 1.5 * Using the load() and save() methods