在动作脚本 3.0 中使用共享对象加载显示对象数组
Posted
技术标签:
【中文标题】在动作脚本 3.0 中使用共享对象加载显示对象数组【英文标题】:Loading array of display objects using shared objects in action script 3.0 【发布时间】:2017-09-26 23:09:23 【问题描述】:我正在尝试加载一个包含一些显示对象的数组,该程序允许我用圆圈填充数组并将它们保存到共享对象,然后,我可以使用加载按钮跟踪我的数组的内容。问题是我重新启动程序后无法加载数组。它跟踪我这条消息:“加载的对象:,,,”
这是代码:
var SO:SharedObject=SharedObject.getLocal("myFile", "/");
var arr:Array=new Array();
var counter:Number=-1;
addBtn.addEventListener(MouseEvent.CLICK, addObjects);
saveBtn.addEventListener(MouseEvent.CLICK, saveObjects);
loadBtn.addEventListener(MouseEvent.CLICK, loadObjects);
function addObjects(event:Event)
counter++;
var circle:circleClip=new circleClip();
arr.push(circle);
trace("current object: "+arr[counter]);
function saveObjects(event:Event)
SO.data.arrSaved=arr;
SO.flush();
trace("objects saved: "+SO.data.arrSaved);
function loadObjects(event:Event)
var arrLoaded:Array=new Array();
arrLoaded=SO.data.arrSaved;
trace("objects loaded: "+arrLoaded);
【问题讨论】:
1.修复你的代码。 2.不能将DisplayObject存储为SharedObject的项,只能存储普通数据类型:help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… 您好 Organis,我正在寻找解决方案,但似乎很复杂,希望有另一个解决方案 退后一步。您想通过存储显示对象来达到什么目的? 我更具体地询问了这个网站:gamedev.stackexchange.com/q/140488 如果你需要我可以给你flash文件 【参考方案1】:你要了解MVC模式的方法:https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
您需要将数据与将这些数据可视化的代码分开,并仅存储数据。比如:
// Lets say this Array contains a list of your circle objects.
var Curcus:Array;
// Storage.
var SO:SharedObject = SharedObject.getLocal("myFile", "/");
function saveCircus():void
var aList:Array = new Array;
aList.length = Circus.length;
for (var i:int = 0; i < Curcus.length; i++)
// Get an object.
var aCircle:Sprite = Curcus[i];
// Record its properties you want to store.
var anEntry:Object =
"x":aCircle.x,
"y":aCircle.y,
"scaleX":aCircle.scaleX,
"scaleY":aCircle.scaleY
;
// Store the entry.
aList[i] = anEntry;
// Store and save data.
SO.data.arrSaved = aList;
SO.flush();
function loadCircus():void
// Retrieve saved data.
var aList:Array = SO.data.arrSaved;
// Make it empty data if there are none.
if (!aList) aList = new Array;
Circus = new Array;
Curcus.length = aList.length;
for (var i:int = 0; i < aList.length; i++)
// Get one entry.
var anEntry:Object = aList[i];
// Create a new item. BTW, naming classes with the
// lowercase first letter is the 8th cardinal sin.
var aCircle = new CircleClip;
// Restore saved properties.
aCircle.x = anEntry['x'];
aCircle.y = anEntry['y'];
aCircle.scaleX = anEntry['scaleX'];
aCircle.scaleY = anEntry['scaleY'];
// Add to display list.
addChild(aCircle);
// Keep it for the future reference/saving.
Curcus[i] = aCircle;
【讨论】:
以上是关于在动作脚本 3.0 中使用共享对象加载显示对象数组的主要内容,如果未能解决你的问题,请参考以下文章
如何创建 If 语句以检查数组中的某些对象。 Flash CS5 动作脚本 3