这一篇主要是介绍一下,在 flash cookie的制作和使用例子详解一 的基础上怎么使用外部的actionscript3的类也就是as文件,这里主要是看代码,具体的东西不做介绍,不明白的可以看看 flash cookie的制作和使用例子详解一:
http://ylq365.iteye.com/blog/1873382
先定义一下外部的类,这里的这个类是从网上找的一个操作cookie的类,就以这个类做为例子
这个类型的代码如下,类名叫Cookie.as
package {
import flash.net.SharedObject;
public class Cookie {
private var _time:uint;
private var _name:String;
private var _so:SharedObject;
public function Cookie(name:String = "testFlashCookie", timeOut:uint=3600) {
_name = name;
_time = timeOut;
_so = SharedObject.getLocal(name, "/");
}
//清楚超时内容;
public function clearTimeOut():void {
var obj:* = _so.data.cookie;
if(obj == undefined){
return;
}
for(var key in obj){
if(obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)){
delete obj[key];
}
}
_so.data.cookie = obj;
_so.flush();
}
public function isTimeOut(time:uint):Boolean {
var today:Date = new Date();
return time + _time * 1000 < today.getTime();
}
//获取超时值;
public function getTimeOut():uint {
return _time;
}
//获取名称;
public function getName():String {
return _name;
}
//清除Cookie所有值;
public function clear():void {
_so.clear();
}
//添加Cookie值
public function put(key:String, value:*):void {
var today:Date = new Date();
key = "key_"+key;
value.time = today.getTime();
if(_so.data.cookie == undefined){
var obj:Object = {};
obj[key] = value;
_so.data.cookie = obj;
}else{
_so.data.cookie[key] = value;
}
_so.flush();
}
//删除Cookie值;
public function remove(key:String):void {
if (contains(key)) {
delete _so.data.cookie["key_" + key];
_so.flush();
}
}
//获取Cookie值;
public function get(key:String):Object{
return contains(key)?_so.data.cookie["key_"+key]:null;
}
//Cookie值是否存在;
public function contains(key:String):Boolean{
key = "key_" + key;
return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;
}
}
}
然后在flash中调用,flash中的调用代码如下:
var myCookie:Cookie= new Cookie();
//导入ExternalInterface类
import flash.external.ExternalInterface;
flash.system.Security.allowDomain("http://localhost");
flash.system.Security.allowDomain("http://127.0.0.1");
//允许任何域都可以访问
flash.system.Security.allowDomain("*");
function setFC(obj:Object){
myCookie.put("userInfo",obj);
}
function getFC():String{
return myCookie.get("userInfo").userName;
return myCookie.getTimeOut();
}
//允许js)调用flash中的getFC(),setFC(),setFCUserObj
ExternalInterface.addCallback("getFC",getFC);
ExternalInterface.addCallback("setFC",setFC);
再就是页面的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
<head>
<title>testFC</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
html, body { height:100%; background-color: #ffffff;}
body { margin:0; padding:0; overflow:hidden; }
#flashContent { width:100%; height:100%; }
</style>
<script type="text/javascript">
function setFC(){
var userName = document.getElementById("userName").value;
var obj = new Object();
obj.userName=userName;
obj.sex="m";
thisMovie().setFC(obj);
}
function getFC(){
alert(thisMovie().getFC());
}
//搭建js与flash互通的环境
function thisMovie() {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window["testFC"];
}else{
return document["testFC"];
}
}
</script>
</head>
<body>
<input type="text" name="userName" id="userName" value="" />
<input type="button" onclick="setFC()" value="setFC" />
<input type="button" onclick="getFC()" value="getFC" />
<div id="flashContent">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="1" height="1" id="testFC" title="testFC" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="testFC.swf">
<param name="quality" value="high">
<param name="wmode" value="transparent" />
<embed src="testFC.swf" name="testFC" quality="high" allowScriptAccess="always" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="1" height="1"></embed>
</object>
</div>
</body>
</html>
运行上面的代码就可以看到我们希望的结果。用这样的方法,我们可以单独的写一些类出来,然后在flash里调用,代码更加结构化,更高的重用性等。