Saving and Loading progress in action script 3 - actionscript-3

I've looked everywhere for this answer..but I am not quite sure on how to do it exactly...
I am making a Flash Game using Flash AS3...
I need options where user can save and load their progress..
can somebody give a step by step on how to do this please? I am very new to actionscript...
usually other tuts shows u how to save certain variables

Cookies!
Lucky us it's very simple. Unlucky us it's not obvious how it's done:
// have that referenced all the time:
var cookies: SharedObject = SharedObject.getLocal("myGameData");
// start saving
cookies.data.progress = 50;
cookies.data.lives = 5;
cookies.data.anyRandomVariable = "my name or something";
cookies.flush();
// now it's saved
// start loading
var progress: Number = cookies.data.progress;
var lives: int = cookies.data.lives = 5;
var anyRandomBlah: String = cookies.data.anyRandomVariable;
// now it's loaded

(following the comments above...)
Yes, pretty much along those lines. I was asking questions to try to get you to see that you do actually have variables/data which you'd save. =b
If you want to save in the middle of the level, it is up to you... I don't know how you've designed your game, but basically you can write the saving code to trigger whenever you want based on any conditions you want.
For saving in the middle of levels, since you are handling the saving and loading you could try having a value like level "7.5" or whatever notation you want to indicate a level that is partway done. Or just use whole numbers like 750 and treat it as a percentage (level 7 at 50% done). And so forth. It's up to you.
A very similar question has been asked how to save a current frame value using a SharedObject. In your case, replace 'current frame' with whatever values you want to save:
Actionscript 3 saving currentframe location to local hard drive?

Related

Append string to a Flash AS3 shared object: For science

So I have a little flash app I made for an experiment where users interact with the app in a lab, and the lab logs the interactions.
The app currently traces a timestamp and a string when the user interacts, it's a useful little data log in the console:
trace(Object(root).my_date + ": User selected the cupcake.");
But I need to move away from using traces that show up in the debug console, because it won't work outside of the developer environment of Flash CS6.
I want to make a log, instead, in a SO ("Shared Object", the little locally saved Flash cookies.) Ya' know, one of these deals:
submit.addEventListener("mouseDown", sendData)
function sendData(evt:Event){
{
so = SharedObject.getLocal("experimentalflashcookieWOWCOOL")
so.data.Title = Title.text
so.data.Comments = Comments.text
so.data.Image = Image.text
so.flush()
}
I don't want to create any kind of architecture or server interaction, just append my timestamps and strings to an SO. Screw complexity! I intend to use all 100kb of the SO allocation with pride!
But I have absolutely no clue how to append data to the shared object. (Cough)
Any ideas how I could create a log file out of a shared object? I'll be logging about 200 lines per so it'd be awkward to generate new variable names for each line then save the variable after 4 hours of use. Appending to a single variable would be awesome.
You could just replace your so.data.Title line with this:
so.data.Title = (so.data.Title is String) ? so.data.Title + Title.text : Title.text; //check whether so.data.Title is a String, if it is append to it, if not, overwrite it/set it
Please consider not using capitalized first letter for instance names (as in Title). In Actionscript (and most C based languages) instance names / variables are usually written with lowercase first letter.

How to remember a room has been cleared? AS3,FP

I have am new to AS3 and coding in general so this is why I am asking this question. I am trying to make a like semi-proceduraly generated dungeon crawler sort of thing like binding of isaac using flashbuilder and flashpunk.
but this has left me with a problem; I have figured out two ways to do the rooms. Either I can spawn all the rooms at once and have the players move between them using a door entity to block progress or I can load a new world that contains the room inside it and nothing else using FP.world = new world. But this gives me two problems so basically this is the main thing I want to know.
I want the world to remember what was in it and what was killed even if I enter a new world. E.g. right now if I enter a new world it will load that world up fresh and then if I kill all the enemies and leave and then re enter it will just spawn all the enemies again. I need to know how to get that room to remember it has been cleared of enemies.
Is there anyway to do this?
I'm not familiar with FP, so I cannot tell if FP allows to save room states. You better check the documentation, but if it doesn't help, you can do the following:
Find a way to get the room unique ID somehow. It could be anything - a coordinate, a number, a name, an object, etc. Just be sure it is unique. Once you have it, proceed.
The next mandatory thing you need is a way to get and set the room state. I.e. after getting state you can know that there are three goblins in a room. And setting state would be like "remove all and put these three goblins in this room". These requirements are natural as you are going to read and write the room's content anyway. Once you have it, the rest is easy.
Choose how you will keep the state. Let's say, an Object like {goblins:3, chest:1}. If you kill one goblin, it will become { goblins:2, chest:1 }. If you want to remember just the rooms that are FULLY cleared, then you simply need a boolean variable (true/false) as a state object.
You can store the state Object in a Dictionary as a key-value pair where key is the room's unique ID.
var visited_rooms:Dictionary = new Dictionary();
var roomID = ... // unique room id
var savedState:Object = { goblins:3, chest:1 } // read room state somehow
visited_rooms[roomID] = savedState;
Now, upon leaving the room, you get the state info and put it in the Dictionary.
Once you enter this room again, just check if its unique ID is present in the Dictionary and restore the state if so:
var roomID = ... // unique room id
if (roomID in visited_rooms) {
var savedState:Object = visited_rooms[roomID];
room.setState(savedState);
}
That's basically all.

Display data that already loaded Angular JS

I have an chtml page using Angular Js that contains list with objects from database
but it takes a long time to load the data.
how can I load just 10 objects and display them, and than continue to load the data
and show the rest of data in Angular Js????
It sounds like you have a lot of data that you want to slowly load to the front end so you don't have to wait. The only method I can think of for adding data periodically would be a setInterval.
the key would be to create a new variable
$scope.displayObjects = []
That you could contiously append to like so
for(var x = currentIndex; x < currentIndex + step && x < $scope.objects.length; x++){
$scope.displayObjects.push($scope.objects[x]);
}
Then just set up an interval to continuously call that. You will also need to make use of
$scope.$apply()
to tell angular to re-apply itself (http://docs.angularjs.org/api/ng.$rootScope.Scope#$apply).
http://jsfiddle.net/A9uND/20/ You can adjust how much it loads with each step via the step variable.
Next time include a jsfiddle (or something similar) so it's easier to assist you.
While the method outlined above will work, I would suggest tracking how much you can see and then only loading the relevant ones. As you scroll add more along the way.

Actionscript 3 saving currentframe location to local hard drive?

I asked similar question sometime ago, but I am making new one to be much more specific with my question with some example!
I´ve found this code snippet/tutorial from googling, but I cant seem to figure out how to modify it for my needs:
// open a local shared object called "myStuff", if there is no such object - create a new one
var savedstuff:SharedObject = SharedObject.getLocal("myStuff");
// manage buttons
btnSave.addEventListener(MouseEvent.CLICK, SaveData);
btnLoad.addEventListener(MouseEvent.CLICK, LoadData);
function SaveData(MouseEvent){
savedstuff.data.username = nameField.text // changes var username in sharedobject
savedstuff.flush(); // saves data on hard drive
}
function LoadData(MouseEvent){
if(savedstuff.size>0){ // checks if there is something saved
nameField.text = savedstuff.data.username} // change field text to username variable
}
// if something was saved before, show it on start
if(savedstuff.size>0){
nameField.text = savedstuff.data.username}
So what I am trying to figure out how to do, is to save users current frame to local hard drive, as my flash progress is based on frame location. yeah, so how do I modify it so that it stores data of current frame? and how about current frame inside movieclip, if that is makes things different?
help MUUUCH appreciated! thanks!
In your example it looks like it is already saving something to the shared object:
savedstuff.data.username = nameField.text;
Just replace it with the movie clip frame value instead (and probably under a different property name other then "username").
Then on load, there is another line where it loads the data:
nameField.text = savedstuff.data.username;
It would be the same way, except replace "username" with whatever property name you choose. Then you may have to parse into an int again and use it to restore progress however way you have it set up.

AS3: Calculating the current upload speed (or throughput)

I am uploading files using the upload() method of the FileReference class. I want to display the current connection speed and I was wondering what was a good way to do that.
My current technique is to use a Timer every 1 mili second such as follows:
var speed:Function = function(event:TimerEvent):void {
speed = Math.round((currentBytes - lastBytes) / 1024);
lastBytes = currentBytes;
}
var speedTimer:Timer = new Timer(1000);
speedTimer.addEventListener(TimerEvent.TIMER, uploadSpeed);
and currentBytes gets set into the ProgressEvent.PROGRESS. This technique seems imprecise. I was wondering what other ways I could use to calculate the upload speed while uploading and display it in real time.
Any ideas or opinions are welcomed!
Thank you very much,
Rudy
If that code block is a copy and paste it certainly won't work as you have expected it to. You declare speed as a function within which you appear to redefine it as a number. I appreciate the Flash IDE let's you get away with sketchy grammar, but code like that is going to lead you into all kinds of trouble. Try to be explicit when writing your code.
Try something like this, replacing yourLoader with whatever identifier you assigned to the uploader:
private var speed:Number = 0;
private var lastBytes:uint = 0;
private function uploadSpeed(event:TimerEvent):void
{
speed = Math.round((yourLoader.currentBytes - lastBytes) / 1024);
lastBytes = yourLoader.currentBytes;
}
private var speedTimer:Timer = new Timer(1000);
speedTimer.addEventListener(TimerEvent.TIMER, uploadSpeed);
That should calculate how many bytes moved in 1 second intervals.
Edit:
You might like to make the interval a little smaller than 1000ms and calculate an average speed for your last n samples. That would make the number your users see appear more stable than it probably does right now. Make speed an Array and .push() the latest sample. Use .shift() to drop the oldest samples so that you don't lose too much accuracy. Trial and error will give you a better idea of how many samples to hold and how often to take them.
You can moniter the upload speed on the server and then send that data back to the client. This technique is often used for ajax file upload forms.