Should be easy. I have an object. I want to modify it, but before i do I want to save a copy of it that I can go back to. I tried setting copy = original but when i modify the attributes of the original the copy also shows the changes. I am assuming this is because in actionscript any time you assign, it really just stores a reference to the original object. So whats the best way for me to store a copy of the original object for later use?
var newObj:Object = Object(ObjectUtil.copy(oldObj));
"Copies the specified Object and returns a reference to the copy. The copy is made using a native serialization technique. This means that custom serialization will be respected during the copy.
This method is designed for copying data objects, such as elements of a collection. It is not intended for copying a UIComponent object, such as a TextInput control. If you want to create copies of specific UIComponent objects, you can create a subclass of the component and implement a clone() method, or other method to perform the copy."
http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#copy()
What you are looking for is a deep copy of the object rather then passing by reference. I found the answer here which uses the new ByteArray class in AS3:
http://www.kirupa.com/forum/showthread.php?p=1897368
function clone(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
Which you then use like this:
newObjectCopy = clone(originalObject);
Cheers!
// duplicate any given Object (not MCs)
Object.prototype.copy = function()
{
ASSetPropFlags(Object.prototype,["copy"],1);
var _t = new this.__proto__.constructor(this) //
for(var i in this){
_t[i] = this[i].copy()
}
return _t
};
Usage
x = ["1","2","3",[4,5],[{a:1,b:2}]]
y = x.copy()
y[0] = 0
y[3][0]="d"
trace(x)
trace(y)
Related
Can anybody show me some easy to understand guides on how to modify and update an external XML file from within ActionScript 3. I've been looking into it for a long time now, but haven't found a tutorial that I was able to understand. My goal is to create a really basic database.. Let's say, a Database of Contacts.. I want to load those contacts through an xml file. but I also want to be able to add new contacts and modify the existing ones. How do I do that?
I want to that in an Desktop AIR Application.
An easy task:
First, load the external XML. For the sake of an example let's imagine the XML to be of this structure:
<contactData>
<contact firstName="John" lastName="Smith" phone="285-493-5421-793" email="example#mail.com"/>
<contact firstName="Jane" lastName="Roberts" phone="285-493-5421-214" email="example#gmail.com"/>
</contactData>
Second, parse that XML. For that create a value object type of a class, let's call it ContactData. It might look something like this:
package
{
public class ContactData
{
public var firstName:String;
public var lastName:String;
public var email:String;
public var phone:String;
public var id:int; // always nice to store an ID
}
}
Loop through your XML - for every contact node create a ContactData class object and fill it the data from the XML.
Store an array with your ContactData objects somewhere, you'll need them later.
Third, edit the ContactData object, or even remove it from an array if you will. Adding is not a problem too.
Fourth and last, create a new xml with AS3 and loop through the ContactData object array to add contact nodes, then save the XML. Use File and FileStream classes to save the file on the hard drive or URLLoader to pass it to the server.
This is how a primitive XML creation code could look like:
var xml:XML = <contactData></contactData>;
for (var i:int = 0; i < contactDataArray.length; i++)
{
var cd:ContactData = contactDataArray[i];
xml.appendChild(<contact></contact>);
xml.contact[i].#firstName = cd.firstName;
xml.contact[i].#lastName = cd.lastName;
xml.contact[i].#phone = cd.phone;
xml.contact[i].#email = cd.email;
}
I hope it's helpful and easy to understand. Good luck!
I'm trying to do a little project for my class and though I know how to do it the long way I'd prefer to do it in a more intuitive way so that I can avoid having to copy and paste a load of essentially the same code. The idea is to have a function which will create an instance of a class object with it's own unique name, set it's position/size/etc, and then add that child to the stage. Looking at this (what I have now) might help out a little bit.
//Set up variables for all deco pieces
var decoGreen:GreenBall;
var decoRed:RedBall;
var decoStar:Star;
var decoFlower:Flower1;
var decoYellow:YellowBall;
var decoBlue:BlueBall;
//Functions to allow easier object placement
function makeDeco(posX:Number, posY:Number, decoName:String, rootClass:Object):void
{
decoName = new (rootClass)();
decoName.x = posX;
decoName.y = posY;
addChild((decoName));
}
makeDeco(90,320,"greenBall",GreenBall)
Now obviously this code doesn't work and it's pretty rough right now but I think it's sufficient to understand what I'm trying to accomplish here. Thanks for any and all who attempt to decipher my mess! :D
You are pretty close from what I can tell and if I understand your question, it would simply be using the getDefinitionByName class
function makeDeco(posX:Number, posY:Number, decoName:String):void
{
var DecoClass:Class = getDefinitionByName(decoName) as Class;
var deco:DisplayObject = new DecoClass();
deco.x = posX;
deco.y = posY;
addChild((deco));
}
makeDeco(90,320,"greenBall")
You don't need to define the variables initially like you did, granted they've all set to "Export as actionscript" in the library. For example calling a string of "greenBall" would mean you have a movie clip in the library with a class name of greenBall
I have a problem with creating and applying displacement maps filters on a container Sprite. The setup is like this
public class ExplosionManager
{
[Embed...] // this is an image I tested, basically a spherical displacement
private var explodeDisplaceAsset:Class;
private var explodeDisplace:Bitmap;
private var displacementFilters:Array = new Array();
private var displacementMaps:Vector.<BitmapData> = new Vector.<BitmapData>();
private var unusedFilters:Vector.<DisplacementMapFilter> = new Vector.<DisplacementMapFilter>();
private var unusedMaps:Vector.<BirmapData> = new Vector.<BitmapData>();
public function ExplosionManager(container:Sprite)
{
explodeDisplace = new explodeDisplaceAsset();
container.filters = displacementFilters;
}
public function ExplodeAt(pos:Point, force:int) : void
{
var newDisplaceMap:BitmapData;
var newDisplaceFilter:DisplacementMapFilter;
if(unusedFilters.length == 0)
{
newDisplaceMap = new BitmapData(64,64,true, 0x808080);
newDisplaceMap.draw(explodeDisplace);
newDisplaceFilter = new DispalcementFilter(newDisplaceMap, pos, BitmapDataChannel.RED, BitmapDataChannel.GREEN, force, force);
}
else
{
newDisplaceMap = unusedMaps.pop();
newDisplaceFilter = unusedFilters.pop();
newDisplaceFilter.componentX = force;
newDisplaceFilter.componentY = force;
newDisplaceFilter.mapPoint = pos;
newDisplaceFilter.mapBitmap = newDisplaceMap;
}
displacementMaps.push(newDisplaceMap);
displacementFilters.push(newDisplaceFilter);
}
}
I'm trying to make an adaptive object pool that reuses and creates filters as needed, my initial thought was that if I have about 2-3 explosions tops it'd be more efficient to have 2-3 filters that only get applied to a part of the scene rather than have one filter with a single big bitmap that gets updated every frame.
The problem is that I can't see any displacement no matter what combination of parameters I try, is there something I'm doing wrong ?
PS: I'm sorry if the code has some typos, I'm not at my development machine right now, so I had to rewrite it, but from the algorithms' point of view that's exactly what I wrote.
It seems like you're assigning the array displacementFilters to the filters property within the constructor (at setup of the ExplosionManager). It then looks like you're adding and modifying filters within that array at a later date.
Unfortunately, changes made to the filters stored within the array you used as a source last time will not be applied to the object. This is because when you assign an array to filters, a copy of that array is actually created and used. From the documentation:
To modify an existing filter object, you must use the technique of
modifying a copy of the filters array:
Assign the value of the filters array to a temporary array, such as
one named myFilters.
Modify the property by using the temporary array,
myFilters. For example, to set the quality property of the first
filter in the array, you could use the following code:
myFilters[0].quality = 1;
Assign the value of the temporary array to
the filters array.
You will need to assign the updated displacementFilters array to filters whenever there are changes made.
I am writing an iOS game in Flash and I need a way to clone polymorphic objects.
I have BaseClass, SubClass1, SubClass2 (and so on...) and I need a clone() method in BaseClass, that will create a copy of the current object, without a conditional such as
var obj:BaseClass;
if(this is SubClass1) {
obj = new SubClass1();
}else if(this is SubClass2) {
obj = new SubClass2();
}else...
I need a way to create an object and create the exact bytes (yes, a shallow copy is enough for my purpose) of the object. I've looked at:
AS3 - Clone an object
As3 Copy object
http://actionscripthowto.com/how-to-clone-objects-in-as3/
But none seem to work. Probably not available in AIR 3.3 for iOS SDK. (they compile, but the code doesn't work in my case)
Is there any other way, or did anybody achieve to clone an object in AIR for iOS?
Thanks,
Can.
Bit-by-bit cloning cannot be done with ActionScript, unless your class only contains primitive values (i.e. a simple data structure). That's what the ByteArray approach you've linked to in this question's answer is used for - but when you're dealing with complex types, especially display objects, you'll soon come to the limits (as, I gather, you have already realized).
So this more or less leaves you with two options:
Create a new object and copy all of its fields and properties.
This is the way to go if you're going to need behavior and field values, and you didn't use any drawing methods (i.e., you can not copy vector graphics this way). Creating a new class instance without knowing its exact type can be done in a generalized way using reflections, getQualifiedClassName() and getDefinitionByName() will help you there, and if you need more than just the name, describeType(). This does have limits, too, though:private fields will not be available (they don't appear in the information provided by describeType()), and in order to not run into performance problems, you will have to use some sort of cacheing. Luckily, as3commons-reflect has already solved this, so implementing the rest of what you need for a fully functional shallow copy mechanism is not too complex.
Create a new instance like this:
var newObject:* = new Type.forInstance( myObject ).clazz();
Then iterate over all accessors, variables and dynamic properties and assign the old instance's values.
I have implemented a method like this myself, for an open source framework I am working on. You can download or fork it at github. There isn't any documentation yet, but its use is as simple as writing:
var myCopy:* = shallowCopy( myObject );
I also have a copy() method there, which creates a true deep copy. This, however, has not been tested with anything but data structures (albeit large ones), so use at your own risk ;)
Create a bitmap copy.
If you do have vector graphics in place, this is often easier than recreating an image: Simply draw the content of the object's graphics to a new Bitmap.
function bitmapCopy( source:Sprite ):Bitmap {
source.cacheAsBitmap = true;
var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0xFFFFFF );
bitmapData.draw( source, new Matrix(), null, null, null, true );
return new Bitmap( bitmapData, PixelSnapping.AUTO, true );
}
You need to create an abstract clone method in the base class and implement it for each subclass. In the specific implementations, you would copy all of the properties of the object to the new one.
public class BaseClass {
public function clone():BaseClass
{
// throw an error so you quickly see the places where you forgot to override it
throw new Error("clone() should be overridden in subclasses!");
return null;
}
}
public class Subclass1 extends BaseClass {
public override function clone():BaseClass
{
var copy:Subclass1 = new Subclass1();
copy.prop1 = prop1;
copy.prop2 = prop2;
// .. etc
return copy;
}
}
If you wanted to create a generic default implementation of clone, you could use describeType to access the properties and copy them over:
public function clone():BaseClass
{
var defn:XML = describeType(this);
var clsName:String = defn.#name;
var cls:Class = getDefinitionByName(clsName) as Class;
var inst:* = new cls();
for each(var prop:String in (defn.variable + defn.accessor.(#access == 'readwrite')).#name )
{
inst[prop] = this[prop];
}
return inst;
}
The main issue with this is that the describeType XML can get quite large - especially if you are dealing with objects that extend DisplayObject. That could use a lot of memory and be slow on iOS.
I have read this article about abstracting assets from ActionScript:
Abstracting Assets from Actionscript in AS3.0 – Asset Libraries and DuplicateMovieClip
But it requires to set the Linkage Class name. How can I get the same result without setting the linkage class name?
What I want to do is to cache a loaded asset, and use the cached version every time I request the same URL. A solution is to clone the loaded DisplayObject, but I think it's unnecessary since I only want a new copy.
I think the way to do that is to use byte arrays
here's a quick sample
// once you load your data...
private function loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
var byteArray:ByteArray = loaderInfo.bytes; //<- this will create your byte array
}
you can then use byteArray.readObject(); to generate the new class;
look at senocular's post at http://www.kirupa.com/forum/showthread.php?p=1897368
where he's got a function like this:
function clone(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
//that you use with
newObjectCopy = clone(originalObject);
hope this gets you started
As of Flash 11.3, there's a function named getQualifiedDefinitionNames that tells me exactly what linkage names should I use with getDefinition, so there's no need to know the values beforehand.