00:00
00:00
View Profile ASSHOLELA

Age 30, Male

Joined on 12/2/07

Level:
5
Exp Points:
180 / 280
Exp Rank:
> 100,000
Vote Power:
4.09 votes
Rank:
Civilian
Global Rank:
> 100,000
Blams:
0
Saves:
11
B/P Bonus:
0%
Whistle:
Normal
Trophies:
1
Medals:
1

Object Duplication (Code)

Posted by ASSHOLELA - April 12th, 2009


Here's the code for Object duplication, as well as the Supporting Functions.Note that theFunction you should access to duplicate your object is _global.duplicateObject and not Object.prototype.duplicate
(its written in AS2 for those who havent have flash 9 or later)

String.prototype.decompileIntoReferenc e = function(thisobj:Object) {
var string = this.valueOf();
if (string.indexOf("_global") == -1) {
if (string.indexOf("_root") != -1) {
string = string.removeString("_root.");
var refsarray = string.split(".");
var reference = _root;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
if (string.indexOf("_root") == -1) {
if (string.indexOf("_global") == -1) {
if (thisobj != undefined) {
string = string.removeString("this.");
var refsarray = string.split(".");
var reference = thisobj;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
}
if (string.indexOf("_global") != -1) {
string = string.removeString("_global.");
var refsarray = string.split(".");
var reference = _global;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
};
String.prototype.removeString = function(string:String) {
var index = this.indexOf(string);
if (index != -1) {
var lastindex = index+string.length;
var newstring = "";
for (var i = 0; i<this.length; i++) {
if (i<index || i>=lastindex) {
var char = this.charAt(i);
newstring += char;
}
}
} else {
newstring = this.valueOf();
}
return newstring;
};
String.prototype.decompileIntoReferenc eParent = function(thisobj:Object) {
var string = this.valueOf();
if (string.indexOf("_root") != -1) {
if (string.indexOf("_global") == -1) {
string = string.removeString("_root.");
var refsarray = string.split(".");
var reference = _root;
var index = 0;
refsarray.pop();
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
if (string.indexOf("_root") == -1) {
if (string.indexOf("_global") == -1) {
if (thisobj != undefined) {
string = string.removeString("this.");
var refsarray = string.split(".");
refsarray.pop();
var reference = thisobj;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
}
if (string.indexOf("_global") != -1) {
string = string.removeString("_global.");
var refsarray = string.split(".");
refsarray.pop();
var reference = _global;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
};
String.prototype.popDot = function() {
var string = this.valueOf();
var stringarray = string.split(".");
var lastref = stringarray.pop();
lastref = "."+lastref;
string = string.removeString(lastref);
return string;
};
String.prototype.remove = function(char) {
var chararray = this.split("");
var newstring = "";
for (var u = 0; u<chararray.length; u++) {
if (chararray[u] != char) {
newstring += chararray[u];
}
}
return newstring;
};
String.prototype.decompileIntoReferenc e = function(thisobj:Object) {
var string = this.valueOf();
if (string.indexOf("_global") == -1) {
if (string.indexOf("_root") != -1) {
string = string.removeString("_root.");
var refsarray = string.split(".");
var reference = _root;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
if (string.indexOf("_root") == -1) {
if (string.indexOf("_global") == -1) {
if (thisobj != undefined) {
string = string.removeString("this.");
var refsarray = string.split(".");
var reference = thisobj;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
}
}
if (string.indexOf("_global") != -1) {
string = string.removeString("_global.");
var refsarray = string.split(".");
var reference = _global;
var index = 0;
while (refsarray[index] != undefined) {
var childname = refsarray[index];
reference = reference[childname];
index++;
}
trace(typeof (reference)+reference);
return reference;
}
};
String.prototype.decompileIntoName = function() {
var string = this.valueOf();
var stringarray = string.split(".");
var name = stringarray[stringarray.length-1];
return name;
};
Object.prototype.createNewThisClass = function() {
if (this.constructor != undefined && this.classname == undefined) {
var constructors = this.constructor;
var newobject = new constructors();
}
if (this.classname != undefined) {
var classname:String = this.classname;
var param:Array = this.param;
var p = param;
var constructorfunction:Function = _global[classname];
var newobject = new constructorfunction(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13]);
}
return newobject;
};
_global.duplicateObject = function(object:Object, objectpath:String, newpath:String) {
if (_global.locals == undefined) {
_global.locals = {};
}
var local = _global.locals;
local.pathsarray = [];
local.objectduplicated = {};
local.objectduplicated.originalobject = object;
local.objectduplicated.path = objectpath;
var newobj = object.duplicate(objectpath);
for (var i = local.pathsarray.length-1; i>-1; i--) {
var obj = local.pathsarray[i];
var objduplicate = obj.duplicateobject;
if (objduplicate.path != undefined) {
var pat = objduplicate.path;
pat = pat.removeString(objectpath);
objduplicate.path = newpath+pat;
}
}
_global.locals = {};
return newobj;
};
Object.prototype.duplicate = function(thispath:String) {
if (thispath == undefined) {
var thispath2 = "this";
} else {
var thispath2 = thispath;
}
var local = _global.locals;
var newobject = this.createNewThisClass();
var newpathobj = {};
newpathobj.path = thispath2;
newpathobj.duplicateobject = newobject;
newpathobj.originalobject = this;
local.pathsarray.push(newpathobj);
for (var prop in this) {
var propvalue = this[prop];
var pat = propvalue.path;
var parents = pat.decompileIntoReferenceParent();
var createdalready = false;
if (this[prop] instanceof MovieClip) {
continue;
}
//see if the object the Function is trying to duplicate exists already asa result of duplication by reference
for (var o = local.pathsarray.length-1; o>-1; o--) {
var obj = local.pathsarray[o];
if (obj.originalobject == this[prop]) {
createdalready = true;
dupparentobj = obj.duplicateobject;
break;
}
}
if ((propvalue.path == undefined || parents == this) && createdalready == false) {
b;
var newpaths = thispath2+"."+prop;
//trace(prop+":"+typeof (propvalue));
if (propvalue.classname != undefined && typeof (propvalue) != "function") {
newobject[prop] = propvalue.duplicate(newpaths);
} else if (propvalue instanceof Object && typeof (propvalue) != "function") {
newobject[prop] = propvalue.duplicate(newpaths);
} else if (typeof (propvalue) == "function") {
newobject[prop] = this[prop];
} else if (typeof (propvalue) != "function") {
newobject[prop] = this[prop];
}
} else {
var path = propvalue.path;
var path2 = propvalue.path;
//determine whether the reference is from outside the Original Object or inside
var pathwork = path;
var insidefound:Boolean = false;
var localobjectduplicatedpath = local.objectduplicated.path;
while (insidefound != true) {
if (pathwork == localobjectduplicatedpath) {
insidefound = true;
break;
}
if (pathwork.indexOf(".") == -1) {
break;
}
pathwork = pathwork.popDot();
}
if (insidefound != true) {
//the object reference is outside the original object
var ref = path.decompileIntoReference();
var name = path.decompileIntoName();
var parent = path.decompileIntoReferenceParent();
var rootparent:Object;
if (ref != undefined) {
newobject[prop] = ref;
}
}
if (insidefound == true) {
//the object reference is insideteh original object
var parentpath = path.popDot();
//var parentfound = false;
var newnestedobjects:Array = [];
var indexof:Boolean = false;
var dupparentobj:Object;
//loop through the list of newly created objects in the Original Object's hierachy to determine if a duplicated reference object exists
for (var o = local.pathsarray.length-1; o>-1; o--) {
var obj = local.pathsarray[o];
if (obj.originalobject == this[prop]) {
indexof = true;
dupparentobj = obj.duplicateobject;
break;
}
}
// if the object being referenced is already created, pass a reference to the property
if (indexof == true) {
var name = path.decompileIntoName();
newobject[prop] = dupparentobj;
//ref = dupparentobj;
}
// if the object does not exist,create a new object and pass the reference as the duplicate object's property
if (indexof != true) {
//if the object being referenced's parent's duplicate doesnt exist,create it too and so on...
var newnestedobjects:Array = [];
var localpath = path;
var parentfound:Boolean = false;
while (parentfound != true) {
if (localpath == local.objectduplicated.path) {
var rootobject:Object = {};
rootobject.originalobject = objectref;
rootobject.path = local.objectduplicated.path;
rootobject._name = local.objectduplicated.path.decompileI ntoName();
break;
}
if (localpath.indexOf(".") == -1) {
break;
}
var localobjectduplicatedlocalpath = local.objectduplicated.path;
var objectref:Object = localpath.decompileIntoReference(local objectduplicatedlocalpath);
for (var o = local.pathsarray.length-1; o>-1; o--) {
var obj = local.pathsarray[o];
if (obj.originalobject == objectref) {
var rootobject:Object = {};
rootobject.originalobject = objectref;
rootobject.path = localpath;
rootobject._name = localpath.decompileIntoName();
parentfound = true;
break;
}
}
var objecttocreate:Object = {};
objecttocreate.originalobject = objectref;
objecttocreate.path = localpath;
objecttocreate._name = localpath.decompileIntoName();
newnestedobjects.unshift(objecttocreat e);
localpath = localpath.popDot();
}
//loop through newnestedobjects array to create the objects that are nested
//var rootpath = rootparentpath.removeString(thispath);
var parentobject = rootobject;
for (var c = 0; c<newnestedobjects.length; c++) {
var objecttocreate:Object = newnestedobjects[c];
var newname = objecttocreate._name;
parentobject[newname] = objecttocreate.originalobject.duplicat e(objecttocreate.path);
parentobject = parentobject[newname];
/*var nestpath:Object = String(newnestedobjects[c]);
nestpath = nestpath.removeString(thispath);
var refobj = nestpath.decompileIntoReference(this);
var newobj:Object = refobj.createNewThisClass();
newobject.assignprop(nestpath, newobj);*/
}
newobject[prop] = parentobject;
}
}
}
}
return newobject;
};


Comments

My comp had some problems copying the entire chunk of code, so it you see Errors pop up in the flash Output window when you try to use it, delete the spaces between the words .
e.g:parentobject[newname] = objecttocreate.originalobject.duplica t e(objecttocreate.path);
"duplicat e" should be duplicate (delete the space )

Holy snap, crackle, and pop. Code isn't useful if you don't know how to use it, and uh, isn't this a bit too much to just give away?