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

ActionScript 3 - Save and load for each level

Explorer ,
Dec 14, 2015 Dec 14, 2015

Can someone help me,
I create each level in different scene,
Scene 1 for level 1,
Scene 2 for level 2.... so on.

i want when player push the back or home button on android hardware, it will save the level and will load when player want to play it next time.

I already search on internet and understand that i must use sharedObject in order to make save and load. But i did not find any suitable example code for me to study.

And now i dont know how to solve this.

TOPICS
ActionScript
2.9K
Translate
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

Enthusiast , Dec 15, 2015 Dec 15, 2015

//Create the SharedObject class and give it an instance in the first frame of the project:

var mySO:SharedObject = SharedObject.getLocal("myProjectID");

var theScore:Number; // this is the player score.

if(!mySO.data.myScore) // check the shared object data if there's no data in name myScore set the value to 0, because at the first run there will not be any data in the shared object, we will write the data later.

{

    theScore = 0;

}

else

{

    theScore = mySO.data.myScore;

}

if(!mySO.data.myFrameNumber)

...
Translate
Enthusiast ,
Dec 15, 2015 Dec 15, 2015

//Create the SharedObject class and give it an instance in the first frame of the project:

var mySO:SharedObject = SharedObject.getLocal("myProjectID");

var theScore:Number; // this is the player score.

if(!mySO.data.myScore) // check the shared object data if there's no data in name myScore set the value to 0, because at the first run there will not be any data in the shared object, we will write the data later.

{

    theScore = 0;

}

else

{

    theScore = mySO.data.myScore;

}

if(!mySO.data.myFrameNumber) // same thing here we'll check if there's data in name myFrameNumber, if there's no, don't do anything.

{

    false;

}

else // if there's

{

    gotoAndStop(mySO.data.myFrameNumber, mySO.data.mySceneName);

}

// All the code above must be in the first frame.

//Naw whenever you want to update the data, for example when you go to the next level use:

mySO.data.myFrameNumber = currentFrame; // the current frame has been stored in name "myFrameNumber"

mySO.data.mySceneName = currentScene.name; // the current scene has been stored in name "mySceneName"

mySO.data.myScore = theScore; // the score has been stored in name "myScore"

mySO.flush(); // the new data has been written in the shared object class.

//You can write any value in the same way

//And if you want to clear the data for some reason use:

//mySO.clear();

Translate
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
Explorer ,
Dec 15, 2015 Dec 15, 2015

Thank you for your good explanation.

But still i dont know where and how to declare my frame number and scene name.

my game just like mario game, but have story element between levels.

Let's say...

i start at scene 1, scene 1 is game environment.

then, i want to go to scene 2, scene 2 is story environment.

then go to scene 3, scene 3 is game environment.

my question, is it possible to do that?

is it necessary to declare my frame number and scene name?

Translate
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
Enthusiast ,
Dec 15, 2015 Dec 15, 2015

The user will not see any data saved in the SharedObject, the application will read the data and apply it.. you only need to tell the application where to read the data and where to apply it and where to write it (update it).

for example when you go to another scene tell the application to write the scene name:

mySO.data.mySceneName = currentScene.name; // first you have to store the new data

mySO.flush(); // then write it in the SharedObject.

Then when the user open the game again the application will read the shared object data in the first scene and apply it:

if(!mySO.data.mySceneName) // there's no tag in name "mySceneName" saved in the mySO? if there's no

{

    false; //stay in the current scene (the first level)

}

else // if there's

{

    gotoAndStop(1, mySO.data.mySceneName); // the scene name you've saved last time

}

//The shared object will save the data on the user device only.

Translate
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
Enthusiast ,
Dec 15, 2015 Dec 15, 2015

When the player click the home button, update and write the data

on click home button:

function onClose(e:MouseEvent):void

{

mySO.data.myFrameNumber = currentFrame;

mySO.data.mySceneName = currentScene.name;

mySO.data.myScore = theScore;

mySO.flush();

// then close the app.

}

Also you can update the data when the player goes to the next level by adding the code on the first frame of the new level:

mySO.data.myFrameNumber = currentFrame;

mySO.data.mySceneName = currentScene.name;

mySO.data.myScore = theScore;

mySO.flush();

You can save it anytime and anywhere you want..

Translate
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
Explorer ,
Dec 15, 2015 Dec 15, 2015

Thank you so much..... You are the best....

Translate
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
Enthusiast ,
Dec 15, 2015 Dec 15, 2015

You're welcome my friend and thank you too.

One more thing, at the game starts:

var mySO:SharedObject = SharedObject.getLocal("myProjectID"); // getLocal() this method checks if there is an existing instance of this object stored it'll not create a new one.

The second step is to check if there's any data saved in that object before, So if there's a sceneName that means there's another data tags, because you're saving all the data at the same time, so you don't need to check it one by one, check for any one and apply them all:

if(!mySO.data.myScore)

{

    theScore = 0;

}

else

{

/// Apply all the actions here.

    theScore = mySO.data.myScore;

     gotoAndStop(mySO.data.myFrameNumber, mySO.data.mySceneName);

}

Translate
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
Explorer ,
Dec 15, 2015 Dec 15, 2015

So, if i want to make the

1st player -> input name -> play game -> save game

then,

2nd player->input name ->play game->save game

then,

1st player -> resume game ->play game -> save game

Should i need more than one instances of sharedObject?

or should i store it in one instance?

Translate
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
Enthusiast ,
Dec 15, 2015 Dec 15, 2015

NO you only need one instance and if you want to save the player ID you can add it to the SharedObject use some tag like: mySO.data.playerName = playerNameString;

to load it use:

playerNameString = mySO.data.playerName;

//in the first frame of the project. you have to load all the data in the first frame.

note that you have to set all the values before you go tho the saved scene.

------------------------------

When the user press the android home button, add another function for that event (home) and write all the data in the SharedObject.

Translate
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
Explorer ,
Dec 15, 2015 Dec 15, 2015

When the player click the home button, update and write the data

on click home button:

function onClose(e:MouseEvent):void

{

mySO.data.myFrameNumber = currentFrame;

mySO.data.mySceneName = currentScene.name;

mySO.data.myScore = theScore;

mySO.flush();

// then close the app.

}

Also you can update the data when the player goes to the next level by adding the code on the first frame of the new level:

mySO.data.myFrameNumber = currentFrame;

mySO.data.mySceneName = currentScene.name;

mySO.data.myScore = theScore;

mySO.flush();

You can save it anytime and anywhere you want..

So if the player click the android hardware button like home button and back button, should I make another function?

Translate
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
Enthusiast ,
Dec 15, 2015 Dec 15, 2015
LATEST

What you can do is creating a save function and use it anywhere you want:

function saveData():void

{

// update and write the data here:

mySO.data.mySceneName = currentScene.name;

mySO.data.myScore = theScore;

mySO.flush();

}

call the function when you want to save the game status use:

saveData(); // you can put it inside a button function like:

home_btn.addEventListener(MouseEvent.CLICL, onClick);

function onClick(e:MouseEvent):void

{

     saveData();

}

note that the saveData() function must be visible in all the frames (put it in a separate layer on the first frame)

As you have to create a new one in a different name for each scene.

Translate
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