JavaScriptやFlashには、HTTP cookieに情報を保存してユーザーのローカルマシンに情報を保存できるという便利な機能があります。

この情報は他のサイトに移動しても維持され、ブックマークや進行状況の追跡、パスワードやハイスコアといったユーザー情報の保存に利用されています。また、ユーザーインターフェイスの状態を保存するといった使い方もできます。

実際Flashのプロジェクト中に、

・1回目に見ときはイントロムービーが流れるように
・2回目以降はイントロムービーは流れずにスキップ
・ブラウザを終了させてもう一度開いたら、またイントロムービーが見れるように

というような希望も多いと思いますね。

Actionscript-Cookie-Util-class-free-download

JavaScriptならたいていスニペットコードを使い、以下のソースコードcookieを読み/書きするスニペットコードを元に作ったコードです。

  1. <script type="text/javascript">
  2. function writeCookie(name, value, hours)
  3. {
  4.     var expire = "";
  5.     if( hours != null )
  6.     {
  7.         expire = new Date((new Date()).getTime() + hours * 3600000);
  8.         expire = "; expires=" + expire.toGMTString();
  9.     }
  10.     document.cookie = name + "=" + escape(value) + expire;
  11. }
  12.  
  13. function readCookie(name)
  14. {
  15.     var cookieValue = "";
  16.     var search = name + "=";
  17.  
  18.     if(document.cookie.length > 0)
  19.     {
  20.         offset = document.cookie.indexOf(search);
  21.         if( offset != -1 )
  22.         {
  23.           offset += search.length;
  24.           end = document.cookie.indexOf(";", offset);
  25.           if (end == -1) end = document.cookie.length;
  26.           cookieValue = unescape(document.cookie.substring(offset, end))
  27.         }
  28.     }
  29.     return cookieValue;
  30. }
  31. </script>

cookieに「userScore」という名前で「80」というスコアの値を保存したい場合は、以下のJavaScriptコードを追加してください。

  1. writeCookie("userScore", 80, 40);

ActionScriptも同じ機能を実現したい場合はどうするの?

以下はAlexanderさんが書かれたソースコードです。ここにクリックするとダウンロードできます。

  1. package de.aggro.utils
  2. {
  3.     import flash.external.ExternalInterface;
  4.  
  5.     public class CookieUtil
  6.  
  7.     {
  8.  
  9.         public function CookieUtil()
  10.  
  11.         {
  12.  
  13.         }
  14.  
  15.         private static const FUNCTION_SETCOOKIE:String =
  16.  
  17.             "document.insertScript = function ()" +
  18.  
  19.             "{ " +
  20.  
  21.                 "if (document.snw_setCookie==null)" +
  22.  
  23.                 "{" +
  24.  
  25.                     "snw_setCookie = function (name, value, days)" +
  26.  
  27.                     "{" +
  28.  
  29.                         "if (days) {"+
  30.  
  31.                             "var date = new Date();"+
  32.  
  33.                             "date.setTime(date.getTime()+(days*24*60*60*1000));"+
  34.  
  35.                             "var expires = ‘; expires=’+date.toGMTString();"+
  36.  
  37.                         "}" +
  38.  
  39.                         "else var expires = ”;"+
  40.  
  41.                         "document.cookie = name+’='+value+expires+’; path=/’;" +
  42.  
  43.                     "}" +
  44.  
  45.                 "}" +
  46.  
  47.             "}";
  48.  
  49.         private static const FUNCTION_GETCOOKIE:String =
  50.  
  51.             "document.insertScript = function ()" +
  52.  
  53.             "{ " +
  54.  
  55.                 "if (document.snw_getCookie==null)" +
  56.  
  57.                 "{" +
  58.  
  59.                     "snw_getCookie = function (name)" +
  60.  
  61.                     "{" +
  62.  
  63.                         "var nameEQ = name + ‘=’;"+
  64.  
  65.                         "var ca = document.cookie.split(’;');"+
  66.  
  67.                         "for(var i=0;i < ca.length;i++) {"+
  68.  
  69.                             "var c = ca[i];"+
  70.  
  71.                             "while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);"+
  72.  
  73.                             "if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);"+
  74.  
  75.                         "}"+
  76.  
  77.                         "return null;" +
  78.  
  79.                     "}" +
  80.  
  81.                 "}" +
  82.  
  83.             "}";
  84.  
  85.         private static var INITIALIZED:Boolean = false;
  86.  
  87.         private static function init():void{
  88.  
  89.             ExternalInterface.call(FUNCTION_GETCOOKIE);
  90.  
  91.             ExternalInterface.call(FUNCTION_SETCOOKIE);
  92.  
  93.             INITIALIZED = true;
  94.  
  95.         }
  96.  
  97.         public static function setCookie(name:String, value:Object, days:int):void{
  98.  
  99.             if(!INITIALIZED)
  100.  
  101.                 init();
  102.  
  103.             ExternalInterface.call("snw_setCookie", name, value, days);
  104.  
  105.         }
  106.  
  107.         public static function getCookie(name:String):Object{
  108.  
  109.             if(!INITIALIZED)
  110.  
  111.                 init();
  112.  
  113.             return ExternalInterface.call("snw_getCookie", name);
  114.  
  115.         }
  116.  
  117.         public static function deleteCookie(name:String):void{
  118.  
  119.             if(!INITIALIZED)
  120.  
  121.                 init();
  122.  
  123.             ExternalInterface.call("snw_setCookie", name, "", -1);
  124.  
  125.         }
  126.  
  127.     }
  128.  
  129. }

使用例です。

  1. import de.aggro.utils.CookieUtil;
  2.  
  3. //Set a cookie named mycookie with a value of mycookie value with a time to live of 30 days
  4.  
  5. CookieUtil.setCookie("mycookie", "mycookie value", 30);
  6.  
  7. //Get that cookie and trace its value
  8.  
  9. trace(CookieUtil.getCookie("mycookie"));
  10.  
  11. //Delete the cookie from the users computer
  12.  
  13. CookieUtil.deleteCookie("mycookie");
メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Thursday, 10th December 2009 by admin

Tags: , ,
Posted in ActionScript | Comments (0) | 7,200 views

Leave a Reply