AS3 - 不能使用SharedObject?

时间:2014-09-27 10:41:54

标签: actionscript-3 flash

出于某种原因,当试图查看它是否正常工作时,我得到TypeError:

  

错误#1034:类型强制失败:无法转换“[对象   SharedObject] 1“到flash.net.SharedObject。

这是我的代码:

stop();

var savedstuff:SharedObject = SharedObject.getLocal("myStuff");

addEventListener(Event.ENTER_FRAME, enterFrameEvent);

function enterFrameEvent (event:Event){
  savedstuff += 1;
  trace (savedstuff);
}

bootoon.addEventListener(MouseEvent.CLICK, clicks);

function clicks (event:MouseEvent){
  gotoAndStop(2);
}

1 个答案:

答案 0 :(得分:0)

您需要从共享对象访问名为 data 的属性,以便在本地保存和加载变量。

var so:SharedObject = SharedObject.getLocal("savedata");
var foo:uint = so.data.foo ? so.data.foo : 0; // If there is a foo property then load it, otherwise set it to zero.
foo++;

so.data.foo = foo; // Save foo to the shared object
trace(foo); // Should increase the value of foo with every launch of the application

//so.clear(); // If you ever want to reset shared object call this.