`
hereson3
  • 浏览: 159903 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

SharedObject 使用

阅读更多

一,什么是共享对象?它的英文名称为SharedObject(以下简称SO)。在flash MX里,SO允许你在使用者本机上存储所有flash支持的数据(包括:Array, Boolean, Date, Number, Object, String, XML...etc),如同在浏览器中使用cookie一样。SO的存储位置,一是本地客户端的硬盘;二是存放在服务器上。在这里,我只能简单介绍一下本地共享对象(Local shared object)的使用。本地共享对象存储默认存储路径为:C:\DocumentsandSettings\登录名称\ApplicationData\Macromedia\FlashPlayer\服务器网域名称\影片路径\影片文件名.swf\...其扩展名为*.sol。默认情况下,Flash 可以在本地保存多达 100K 大小的永久性远程共享对象。当您尝试保存一个较大的对象时,Flash Player 将显示“本地存储”对话框,用户可以使用该对话框允许或拒绝请求访问的域的本地存储。如果用户选择“从不”,那么SO将无法完成保存任务。SO的名称可以包含正斜杠 (/);例如 work/addresses 就是合法名称。SO名称中不允许使用空格,也不允许使用以下字符: ~ % & \ ; : " ' , < > ? # ;SO存储的信息具有只读属性,你每次打开swf播放器的时候,会获得该信息。 
测试代码: 
//创建一个名为kookie的本地共享对象; 
this.so = SharedObject.getLocal("kookie"); 
//给共享对象添加一个username的属性来记住使用者; 
this.so.data.username="bluelife"; 
//添加该属性不会将它保存到硬盘。只有影片被卸载或使用语句so.flush()强制保存信息。 
this.so.flush(); 
//现在可以用trace语句获取一下username的值; 
trace(this.so.data.username); 
//添加下面这个语句,再trace一下,username值为空。注意该命令只清除硬盘信息,SO对象仍然存在,可以引用. 
this.so.clear(); 
SO对象的其它方法的使用,请查阅flash帮助文档,这里不再赘述。 
二,共享对象的应用.如果理解SO对象可以永久保存用户的数据的话,那么它可以用来记录flash游戏的成绩,使用者的个人信息(姓名,年龄,爱好...ETC) 
两个独立的swf文件,如果存放在同一文件夹下,可以共享一个名为“cookies”的SO。方式是: 
anotherSO=SharedObject.getLocal("shared","/"); 
同理,在子目录下的SO对象,可以这样共享。 
anotherSO=SharedObject.getLocal("shared","/test1/"); 
anotherSO=SharedObject.getLocal("shared","/test1/test2/"); 
应用一: 
----------------------------- 
//copy right :edmack.com 可读写的Cookie 
MovieClip.prototype.setCookie = function (c,n,v){ 
var so = SharedObject.getLocal(c); 
so.data[n] = v; 
so.flush(); 
}; 

MovieClip.prototype.getCookie = function (c,n){ 
var so = SharedObject.getLocal(c); 
return so.data[n]; 
}; 
//c = cookie name 
n = 存储的变量名称 
v = 要存储的变量值 
-------------------------------- 
应用二: 
-------------------------------- 
//by Steve Grosvenor 记录本地用户的访问次数 
sObjVisits = SharedObject.getLocal("userVisitsLocal"); 
if (sObjVisits.data.visits == undefined) { 
sObjVisits.data.visits = 1; 
} else { 
sObjVisits.data.visits = sObjVisits.data.visits+1; 

howManyVisits = sObjVisits.data.visits; 
sObjVisits.flush(); 
visits.text = "been here "+howManyVisits+" times"; 
--------------------------------- 
应用三: 
-------------------------------- 
//本地使用者的帐户登陆,上传我做的一个练习。 
so=SharedObject.getLocal("userinfo"); 
function checkin(){ 
if(this.mytxt.text==so.data.user&this.psword.text==so.data.psword){ 
this.gotoAndStop(2); 
this.msg.text="you are come back again!"; 

else if(this.mytxt.text!=so.data.user){ 

so.data.user=this.mytxt.text; 
so.data.psword=this.psword.text; 
so.flush(); 
this.gotoAndStop("welcome"); 
this.msg.text="you are new visitor."; 

else trace("error"); 

checkbtn.onRelease=function(){ 
checkin(); 



this.mytxt.text=so.data.user; 
this.psword.text=so.data.psword; 
stop(); 

-------------------------------- 
应用四: 
-------------------------------- 
//by mvmartin 保护swf文件的手段 
//“尽管如同星光所说,任何的保护都是徒劳的,但...那是另一回事了”. 
这种保护手段是以用户的使用时间为界限的。当我们设定时间线,用户在第二次使用的时候,会得到期限已过的警告。我知道这是一个陈旧的简单的保护手段,用户可以修改系统时间来绕过“障碍”。为了避免这样的情况,SO对象就派上用场了. 保护代码持续跟踪swf文件上次使用的时间。假定用户更改电脑的系统时间回到早先,那么我们仍然可以通过SO知道这个欺骗手段。也就是保证用户以后访问的时间必须大于上次访问的时间,那么我们给定的时间限制就真正起作用了。下面给出代码,如果你喜欢,可以做测试。给定一个你期望的限制时间段,然后设置系统时间超前,那么swf会跳转到你所指定的STOP帧;如果把系统时间返回到“先前”,你仍然会得到相同的结果。// 
1) 主时间轴上建立一个实例名为 DateCheck的影片剪辑MC (里面是空的,只包含代码). 
2) 在影片剪辑DateCheck的第一帧: 
// 这部分代码检测用户是否将系统时间人为的“提前”。 

// 建立一个名为datesharedobject.sol的共享对象. 
D4XSO.data.datelastaccessed = new Date(); 
D4XSO = sharedobject.getLocal("datesharedobject"); 
// 设定 "now" 等于当前系统时间 
var now = new Date(); 
if (now< D4XSO.data.datelastaccessed) { 
// If the computer date (now) is less than the date last accessed, 
// then the date has probably been set backwards on the users computer, 
// so set "now" forward to that last accessed date so they can't 
// access the course. 
var now = D4XSO.data.datelastaccessed; 
trace("Today's date (now) is being set to the datesharedobject date, which means the computer was set back; ="); 
trace (D4XSO.data.datelastaccessed); 
trace ("Which should equal what now has been set to, which is:"); 
trace (now); 
// Else if the computer date (now) is greater than the last date accessed, 
// then all is right with the world, so go ahead and write that date 
// back to the shared object as the date last accessed. 
} else { 
D4XSO.data.datelastaccessed = now; 
D4XSO.flush(); 
trace("The datesharedobject date is being updated to today's date ="); 
trace(D4XSO.data.datelastaccessed); 



3) 稍微给一点延迟时间,比方在第20帧的代码如下. 

var start = new Date(2003, 10, 1); 
// set start equal to 11/1/2003 (Jan = 0) 
var end = new Date(2004, 6, 15); 
// set end equal to 7/15/04; do not put zeros in front of numbers 
if (now // if date is before start, then they're trying to cheat us, so give error message 
trace ("If you're seeing this then the now is before the START date, meaning they've set the clock back too far."); 
trace ("The START date is:"); 
trace (start); 
trace ("Today's date (now) is set at:"); 
trace (now); 
_root.gotoAndStop(31); 
} else if (now>end) { 
// if date is after end, then go to end 
trace ("If you're seeing this then the now is before the END date"); 
trace ("The END date is:"); 
trace (end); 
trace ("Today's date (now) is set at:"); 
trace (now); 
_root.gotoAndStop(31); 
} else { 
trace ("Everything is hunky-dorey, let's go!"); 
play(); 

stop();

 

 

来源:http://qzone.qq.com/blog/14339015-1203305457
http://blog.sina.com.cn/s/blog_494f8065010004aw.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics