• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Write a file in execution (Save game)

New Here ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

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 ?

Views

541

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 11, 2018 Jan 11, 2018

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.addEventListen

...

Votes

Translate

Translate
LEGEND ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines