Flash Vista - 主页
本周的热门 Flash 网站
The Two Tales
Site info | 档案文件
主项
主页
新进网站链接
优良评选网站
热门网站
编辑推荐
查找

Flash Tutorials
Flash Resources
FlashVista 新闻
Flash Templates
Flash Intro Templates
Website Templates
Flash Games
Flash Books
FlashVista 票选
本站图


无故定网站连结
English
Pioneer Graphics
Site info | 找寻另一个
FlashVista
登录
注册注册
订阅电子报

新建网站链接
编修网站链接
个人喜好网站
建议新分类

打广告
支持
致谢
联落 F

Flash Templates:

More Templates ...

电子报
用户

密码



注册注册
忘记密码?
Partner websites
Free Hit Counter
Free Seo Tools
Free Tutorials
Free Video Tutorials
Forum signatures
Best Free Scripts
Wii Fit
Wii Fit News
Cool Tech Gadgets
Other Resources
推荐
推荐本站给朋友


电子报
状态: 尚未登录

Simplified Chinese English German French Spanish Italian Portuguese Russian Polish Finnish Dutch Swedish Thai Romanian Traditional Chinese Simplified Chinese
查找尚未登录
关键字: 查找:: 高级查找


Using Local Shared Objects in Flash MX: The Flash Cookies


NEW Flash Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, flash 8 and more.


Download the source file for this tutorial Printer version



by Philipp Kostin
www.philarmon.com


Before Flash MX, it was pretty tricky to "remeber" the data in a Flash movie - it could be done with a standard browser cookie, which was hard to implement for someone with intermediate Flash skills, using a 3rd party script such as PHP or ASP or (in offline Flash applications) with the undocumented fscommand "save". Flash MX's Shared objects allow to store and retrieve the information within a Flash movie easily. Lets see how it works.


What is a local shared object ?

Flash MX Shared objects is a new feature allow you to store information at the user's machine the same way as cookies would and retreive it at a later time. Shared objects could be used to remember the user's name, the number of the level he last played in a game, his highscore or anything else you can imagine.

Shared objects are stored in .sol files located in the Flash player directory of the user's profile : "C:/Documents and Settings/Administrator/Application Data/Macromedia/Flash Player", and have their own format

Here is a working example of a movie using the Shared Object. Type in your name and age and click on "Save". Then, refresh this page to let Flash read and display the stored data:
WIDTH="200" HEIGHT="150" id="shared_object" ALIGN="">
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">



Creating a shared object

First, we have to create a local shared object within a Flash movie. To do so, just put this line of code in the first frame of your movie:


local_data = SharedObject.getLocal("user_data");


Now we have created an Object named "local_data" which is associated with a shared object on the local hard-drive named user_data. Note that in the feature, this data can be read from other movies from the same domain that created the Shared Object.


Writing data

Lets store something in our fresh created Shared Object. Lets say, we want to store the user's name and his age in it. To do so, use this:


local_data.data.user_name = "John Smith";
local_data.data.user_age = 23;
local_data.flush ()


Note that this code must be in the same level where you have created the Shared Object.

The flush () command is optional, it is used to write the information to disk immediately. If you don't use this command, Flash MX writes the shared object to a file when the SWF movie is closed or when the shared object is garbage-collected because it no longer has any references to it.


Reading data

To retrieve data from a saved Shared Object, just use the following syntax:


stored_user_name = local_data.data.user_name;
stored_user_age = local_data.data.user_age;


Don't forget that you still have to create the local_data shared object first. Now, the user's name is stored in a variable called "stored_user_name" and the user's age in the variable "stored_user_age"and you can use it anywhere in your movie.

You can store more information in one Shared Object file in the same way.

Similar to the way how we have saved simple text variables you can store whole arrays and other objects.


Important notices

There are some important things to remember when using the local Shared Objects in your movies:

  1. The primary drawback of shared objects is that it can be overwritten/disabled/erased by the user (right click on the swf, and click on settings), so avoid to store information which is absolutely necessary to let the application work properly. Think of it as an additional feature.




  2. The amount of the information you can store in a local Shared Object from one domain is set to 100 kb by default. If you will try to store more information, the Flash player will ask the user for a permission to increase this limit. Be sure the stage of your movie is at least this is the minimum size Macromedia Flash MX requires to display the dialog box.




Additional information

This tutorial covers the basics of the local Shared Object feature. You can read more about it and take a look at additional features at the Macromedia website:

What is a Shared Object ?
Using local shared objects in Macromedia Flash MX


(注册日期: 01-19-2004, 点阅人次: 1, 评比积分: 3.48, 票数: 25, 评论篇数: 12)
新建至个人喜好 建议本站给朋友

评论篇数: (10)

can you please explaithis in more detail, i can't get it to work at all...

can youplease post where i need to put the script, like on a button, or on the frame containing the text boxes to load the data into...

i really appreciate any help, since my text boxes dont change no matter what i try.

(注册日期: 02-23-2007 用户: 访客)

how do you save tings like x and y values of an object... eg: if the user drags an object around the stage and then drops it, how do you save the x and y positions so they can refresh the page and the object automatically moves to the position they dragged it to before?
(注册日期: 02-22-2007 用户: 访客)

i would like to know how use LSO to get user name and then read that user name on later login to let that user continue from the last frame he stopped at b4 closing the application and to save his scores in a quiz and do the LSO do overwrite each other if another user used the same CD or it append it to let more than one user use the same application and call the scores and last point he wast at for each user independently
(注册日期: 02-19-2007 用户: doaa)

How can i aplly this so that when my flash movie plays on one page, when i go to another page the cookie can control the timeline of the swf. I have a header with links and i don't want the intro to play on every page just the index, but the swf contains all my navigation.
thanks

(注册日期: 05-18-2006 用户: 访客)

td8f4

thanks, but how would i make it so it ca save a game?

thanks

td8f4@hotmail.com

(注册日期: 04-27-2006 用户: 访客)

That was all well explained and detailed, but there is one thing about all this... one thing about every Shared Object tutorial I've looked at today!
Nobody explains how to save variables! I don't need to save text, I want to save numbers! Not written numbers, numbers you can add and subtract from.
For example, on the first frame of the timeline, writing this:

_root.examplevariable = 0;

Which defines 'examplevariable' as being equal to zero. Then I have buttons in my example Flash file which might have a script like:

on (release) {
_root.examplevariable +=1;
}

Which adds 1 to the variable.

The variable I want to save is an active one, which is always changing.

So please contact me if you know how to do such a thing at danny.poloskei@gmail.com

(注册日期: 01-15-2006 用户: 访客)

Zbotic, you can use the shared object in any flash movie from the same domain
(注册日期: 07-31-2005 用户: Philarmon )

Thank you so much for the lesson. I do have a question: What if you want a different flash file on your site to use the shared object?
(注册日期: 07-28-2005 用户: zbotic )

Very Nice
But When instal new windows what I doing

(注册日期: 05-30-2005 用户: 访客)

vamos fazer um teste para ver se funciona mesmo
(注册日期: 05-03-2005 用户: 访客)


新建评论
Please note:
We review EVERY comment before it appears on the site, so please dont waste your time by posting spam links :)
No URLs allowed, no HTML please.

If you register or login first, your review will contain your nickname


评分



很棒!
非常好