I’m having a problem creating some custom prototypes for the Flash Object. It’s supposed to link one variable to another, so when you modify x, y and z are automatically adjusted based on the value of x. It could be a very useful thing, and I’m trying to do it with the Object.watch property. The problem is that when everything has run the anchor value is removed from the object.
so:
a = {x:0, y:1, z:0};
a.setLinked(“x”, [“y”, “z”], 0.5);
a.x = 10;
will set y and z to be 10*0.5, but the x value becomes blank. I’ve traced it right down to the end of the watch function and the x value is still at it’s original value, but as soon as everything is finished the x value is gone.
Anyone have any suggestions?
here’s the code and the FLA.
--=::Cheers ::=--
Object.prototype.setLinked = function (anchor, links, modifier)
{
// abort the linking if required data is unspecified or the values to be linked do not exist
if (anchor == undefined)
return false;
if (links == undefined)
return false;
if (eval("this." add anchor) == undefined)
return false;
if (modifier == undefined)
modifier = 1;
// create a link object based on the anchor
set("link" add anchor, new Object());
// create the linked array in the newly created link object
eval("this.link" add anchor).linked = [];
// if the links value is an Object
if (links instanceof Object){
// check all values of the links object to make sure they exist
for (var i = 0; i< links.length; i++){
if (eval("this." add links*) == undefined)
return false;
}
// copy the provided list into the object
eval("this.link" add anchor).linked.duplicate(links);
} else {
// the links value is a variable, check if it exists
if (eval("this." add links) == undefined)
return false;
// store the value in the first index of the array
eval("this.link" add anchor).linked[0] = links;
}
// store the link modifier value in the link object
eval("this.link" add anchor).linkModifier = modifier;
// check for the successful watching of the anchor object
success = this.watch(anchor, function(id, oldval, newval){this.changeLinked(id, oldval, newval)});
if (success)
trace(links add " linked SUCCESSFULLY to " add anchor);
else
trace("Linking FAILED");
return success;
}
Object.prototype.changeLinked = function (id, oldval, newval)
{
// loop through the array of links and assign the newval value to all linked variables
for (i in eval("this.link" add id).linked)
if (isFinite(i))
set("this." add eval("this.link" add id).linked*, newval*eval("this.link" add id).linkModifier);
}
// A prototype for copying objects without linking them
Object.prototype.duplicate = function (obj)
{
for(var i in obj){
if(obj* instanceof Array){
this* = [];
this*.duplicate(obj*);
} else if (typeof obj* == "object"){
this* = {};
this*.duplicate(obj*);
} else if(typeof obj* == "string" || typeof obj* == "number" || typeof obj* == "boolean" || obj* != null){
this*= obj*;
}
}
}
// A prototype for copying arrays without linking them
Array.prototype.duplicate = function (arr)
{
for (var i in arr){
if(arr* instanceof Array){
this* = [];
this*.duplicate(arr*);
} else if(typeof arr* == "string" || typeof arr* == "number" || typeof arr* == "boolean" || arr* != null){
this*= arr*;
}
}
}