Flash Goddess Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Duplicate object

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Flash Goddess Forum Index -> ActionScript 1.0
View previous topic :: View next topic  
Author Message
i11uminatus



Joined: 21 Aug 2003
Posts: 110
Location: Toronto, ON

PostPosted: Thu Sep 18, 2003 4:19 pm    Post subject: Duplicate object Reply with quote

Ok, there's probably some very simple way to do this...but I can't think of it!

Imagine I have an object. Let's call it "hoser" and give it a property.

hoser = new Object();
hoser.favDrink = "beer";


Now, I want to create some instances of other objects: Bob and Doug.

Bob = new Object();
Bob.type = hoser;
//
Doug = new Object();
Doug.type = hoser;


Now Bob and Doug both contain references to the hoser object as their "type".

So if we tried:
trace(Bob.type.favDrink);

We would get "beer" in the output.

Ok, now what if Doug changes his favorite drink to coffee?

If we say:
Doug.type.favDrink = "coffee";

We are essentially changing the property of the "hoser" object.
trace(Bob.type.favDrink);

would now get us "coffee" in the output!
But Bob still likes beer!

So...
Assuming I'm dealing with much more complicated objects and am passing in many properties, how do I then change the property in one object without changing it for all objects?

In other words, how do I make a duplicate of an object (give Bob and Doug each their own unique version of all the properties contained in "hoser") instead of a reference to it?

Thanks
i.
Back to top
View user's profile Send private message MSN Messenger
K-dog
Moderator


Joined: 23 Jul 2002
Posts: 772
Location: Toronto, ON

PostPosted: Fri Sep 19, 2003 9:21 am    Post subject: Reply with quote

Excellent question... and I love the Bob and Doug theme!

Let's rework your code a little and see what we get. First let's create a hoser:

Code:
function Hoser(favDrink, hatColor, name) {
   this.favDrink = favDrink;
   this.hatColor = hatColor;
   this.name = name;
}


And now let's create Bob and Doug, giving them their own properties:

Code:
var hoserBob = new Hoser("beer", 0xFF0000, "Bob");
var hoserDoug = new Hoser("beer", 0x00FF00, "Doug");


Let's see if they have different names....

Code:
trace(hoserBob.name); // hey, his name is Bob!
trace(hoserDoug.name); //wow, it's Doug!


Now let's try to change Doug's favorite drink to coffee:

Code:
hoserDoug.favDrink = "coffee";
trace(hoserDoug.favDrink); // it's changed!
trace(hoserBob.favDrink); // he still loves beer!


Right on! Hope that helps, eh?

--K
Back to top
View user's profile Send private message
i11uminatus



Joined: 21 Aug 2003
Posts: 110
Location: Toronto, ON

PostPosted: Mon Sep 22, 2003 9:19 am    Post subject: Reply with quote

Thanks kristin,

I appreciate your response, and your class solution does give me some perpective, but at the moment I'm not sure it's applicable to this particular scenario.

I will think on it.

AUMMMMMMMMM

i.
Back to top
View user's profile Send private message MSN Messenger
pedram



Joined: 26 Nov 2002
Posts: 111
Location: london

PostPosted: Mon Sep 22, 2003 8:11 pm    Post subject: Reply with quote

Don't know if this might be of any use to you:

if you goto http://proto.layer51.com/

and search under the objects catagory, theres some code by klitze7-c004
which might be usefull,

its called /// duplicate any given Object

the code allows you to duplicate an object and then reassign the variables within the new created object

this is the direct link to the prototype:

http://proto.layer51.com/d.aspx?f=446

but I havn't tested it myself
ped
_________________
http://www.xplatform.org/
http://www.debreuil.com/docs/ch01_Intro.htm
http://www.stranger.per.sg/
http://www.quantumwave.com
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
i11uminatus



Joined: 21 Aug 2003
Posts: 110
Location: Toronto, ON

PostPosted: Tue Sep 23, 2003 9:17 am    Post subject: Reply with quote

pedram,

Thank you for this useful link. I did eventually implement a for-in loop as a solution to my problem, creating a new object and reproducing each property of the target object, as in:
Code:
enemyType = {name:"enemy", speed:4, clipLink:"enemy"};
//
function buildCharacter(name, type){
   world[charName] = new Object();
   character.type = new Object();
   for (var i in type) {
      world[charName].type[i] = type[i];
   }      
}
This is a simplified version, of course.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Flash Goddess Forum Index -> ActionScript 1.0 All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Google
 




Powered by phpBB © 2001, 2005 phpBB Group