万卷书   首页 > 电脑书籍 > Flash > Flash MX本地保存数据的三种方法
 
  方法三:结合JavaScript

  还是在Flash 5时,ActionScript就可以与JavaScript相互通信了。那又该怎样使用JavaScript本地保存Flash MX的数据呢?首先我们得用fscommond命令将Flash MX获得的数据传递到JavaScript中。

  步骤1:按方法一设置场景,然后在按钮上加入下面代码。
 

 

 

 
on (release) {
fscommand("send_var", name); //fscommand参数为name变量
} 


  步骤2:以Flash5的格式发布Flash电影和html文件,然后打开html文件,在与之间加入如下代码:
 


 

 

 
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function myFlash_DoFSCommand(command, args) {
var myFlashObj = InternetExplorer ? myFlash : document.myFlash;
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c:\\testfile.txt", true);
tf.WriteLine(args);
tf.Close();
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub myFlash_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFlash_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>


  你不必知道这些JavaScript和VbScript语句的具体含义,只要明白我们已经将name变量的值传递给了args就行了。接下来我们还得将args的值写入文本文件,最简单的办法就是使用FileSystemObject(文件系统对象)。添加如上红色部分的代码。关于FileSystemObject的用法,请参考<<JScript 语言参考 >>。

  步骤3:运行html文件,输入一个用户名,点击按钮,用户名就保存在C盘的testfile.txt中了。

  严格来说, 这应该算是FlashMX借用Flash 5语句来实现本地保存数据的方法。

  至于选择哪种方法,视具体情况而定。如果你是开发游戏,适宜用第一、二种方法;如果你不使用FlashMX的新增动作角本,可以试一试最后一种方法。不明之处请参看源文件。

 

 

上一页