Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This game currently does a good job of just keeping track of the level the person was on and their score when they finish the game basically, by the time all of their players die. In fact, they can even enter in their name. But none of this data is being stored, and what I want to do is I want to store the highest score, known as sort of the all-time high score. So that's what I want to store, and each time you play it's going to compare your score against the all-time high score. So that's what I am going to do.
I am going to go into my File, this first frame here, and open up the Actions panel. And right down here, near the bottom, is this Show Results function, and there's a couple if else statements. But what I want to do is just after sort of turning off a couple this button and the high score name, I want to go ahead and basically create a shared object. It will store the all-time high score.
That's what I am going to create. So I am going to say var so, for SharedObject. SharedObject. Again, as I type that it's highlighted and I could hit Enter to select it. SharedObject = SharedObject.getLocal. So I am going to get the local shared object. This is going to be the local shared object on their phone. And then I need to give it a name, so I can say "alltimeHighScore" and that's the shared object that's going to be stored.
So I have created this shared object, and within this first if statement what I need to do is I need to go ahead and see if there is a current score in there. So for the shared object, s-o, for that shared object, the data property, score, for that score, which I'm just going to define, says for this shared objects data score property is it equal to anything? Or is it undefined. Because if it's undefined, that's when I want to go ahead and show them this message.
It says, "You made it to level whatever, and go ahead and enter in your name." So again, if this is undefined, so go ahead and display this message. Now the other reason I want to display this information is if they have the highest score. So again, two of those vertical bars, those pipes as they are known, and I am going to basically say if it's undefined or if the score is greater than the data in that shared object, known as score.
So if you basically got the highest score or if it's undefined, then go ahead and display this message saying congratulations, go ahead and enter in your name. So that's what this does. We want this to happen, and it will happen the first time through. So that's we are going to do. I am going to scroll down a little more. And else if the player didn't get the highest score, then display another message. It says your score of whatever doesn't beat 0. Well, your score of whatever you had does not beat--just outside of that string.
I am going to add a Plus sign--so.data.score. In fact, what I also want to do is I want to go ahead and add the person's name as well. So let me just scroll down here a little more. Your score of whatever doesn't beat this other score "by" so.data.name. And then we will even. I do have to put a Plus sign right in here, but down here at the end, we will go ahead and put a period at the end of that sentence.
So if your score of say 3 doesn't beat the high score of 5 by whoever. So basically that's what that says. So that looks pretty good, but I need to go ahead and save the score and even save the person's name if they happen to get the high score. So remember, we turn on the high score name and the Enter button. Let's take a look at that right down here. Again we are basically turning on and making that the enter button "work" if you will. So here it is.
Here is my Enter button. It does say, "Great job," yada yada yada. It drops in their name and all that good stuff. But what I want to do in here, because this is only displayed if they haven't entered in their name yet or if they have the highest score, so in here what I need to do is I need to define all of the elements within that shared object. So define all of the properties of the shared object, known as s-o.
So right in here I can go ahead in so.data.score = whatever score they had. I can even save the level that they were on, so.data.level = level and so.data.name. What about that name? I can't just type in name. What the name is is it actually has to equal to whatever they entered in within this highscore name.
So I need to actually take this and copy it and paste it right in here. So for that endScreen that highScoreName text field, well, whatever is in that text field needs to be saved in the name for that shared object. The last thing I need to do is I can go ahead and say, for that shared object, I am going to actually go ahead and flush it, which is basically push that data into that shared object.
That's all that does. Now if you do quit out of the game, it will automatically perform a flush, but you can always add that right in there, just to be on the safe side. All right, with all that working, I am going to go ahead and run this project, do a test movie. So the first thing I need to do is establish a score to begin with.
All right, you make it to level 1 with a high score of 8. Enter your name below. Paul. I will click Enter, Great job, Paul! You made it to level 1 with score of 8! This is inside of Invaders.swf, so if you could imagine this on my phone, the great thing is this also works on the desktop. So what I'm going to do is I am just going to close this file out and I am going to open it back up again. I am not going to republish it. I am just going to go ahead and go to my desktop. Here is the SWF that was just created, and again as if I was going back to this game and playing it again, that's essentially what I'm doing.
But again, it stores that data. Whether you're on the desktop or the phone, it works either way. So I am going to try to beat, say for instance, the score of 8. All right, you made it to level 2 with a high score of 14. Enter your name below. Paul number 2. Enter it.
So it works just as expected, just like before. Now when I play this game one more time, what's going to happen is I am not going to hit 14. I am going to get something lower so it's not going to display either one, this text field or that button. Okay, so I will close this down, go back out to the SWF. I am going to launch this SWF, and this time I am just going to die immediately. All right, there we are. As you can see, your score of 1 doesn't beat 14 by Paul 2.
So that was my second time through the game. So as you can tell, it is actually storing that data. It's storing that shared object data, also known as the Flash cookie, which is another slang term, and it works quite well. Now, there's other uses for this as well. You can not only store say the highest score, but you can also store the various levels. The person can come back and play that same game at the level they left off at, or what you could even do is you can extend it further by adding a SQLite database.
But for the purposes of this game, this is really all I needed to do.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101787 Viewers
61 Video lessons · 88547 Viewers
71 Video lessons · 72357 Viewers
56 Video lessons · 104066 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.