Inheritance

OK, this was the code:

// dave yang's inheritance code
MovieClip.prototype.setClass = function(c, args) {
	this.sup = c;
	this.__proto__ = c.prototype;
	this.sup(args);
	delete this.sup;
};

// the class constructor
MCbutton = function(){
	this.class = "MCbutton";
}

// link the prototype chain back to MovieClip's
MCbutton.prototype.__proto__ = MovieClip.prototype;

// set clip "sqr" to class MCbutton
sqr.setClass(MCbutton);

Let’s sum it up: This part creates the class MCbutton

// the class constructor
MCbutton = function(){
	this.class = "MCbutton";
}

Then you say that your MCbutton inherits the properties of MovieClip but I don’t know, this line of code looks strange to me… It’s probably because my Chinese master taught me that I should use the MCbutton.prototype=new MovieClip(); (but there isn’t such a thing as a MovieClip constructor is there?)

// link the prototype chain back to MovieClip's
MCbutton.prototype.__proto__ = MovieClip.prototype;

Finally you call the infamous setClass function

// set clip "sqr" to class MCbutton
sqr.setClass(MCbutton);

Which will

MovieClip.prototype.setClass = function(c, args) {
	this.sup = c;
	this.__proto__ = c.prototype;
	this.sup(args);
	delete this.sup;
};

But I don’t understand the use of that function… Why don’t you just declare sqr=new MCbutton??

pom :asian:

Then you say that your MCbutton inherits the properties of MovieClip but I don’t know, this line of code looks strange to me… It’s probably because my Chinese master taught me that I should use the MCbutton.prototype=new MovieClip(); (but there isn’t such a thing as a MovieClip constructor is there?)

i think either method will work. they seem to do pretty much equivilant things.

Why don’t you just declare sqr=new MCbutton?

doing that would overwrite the movieclip “sqr” with an MCbutton rather than make the clip a member of the MCbutton class.

you’d have all the prototypes and the variables defined in MCbutton but no clip to apply them to!

Hmm… I’m going to get some sleep and everything should be OK tomorrow [SIZE=1]yeah, right…[/SIZE]

Hi guys, I know this is an old thread, but Ive posted a similar question on fkit with no real answer. I was looking at this thread and it seems similiar what I want to do, Im just not knowledgeable enough on prototypes to understand it very well.

The problem I am having is that I want to use prototypes to populate the textfields (with vars from a LoadVars Obj) and give mouse functionality to newly attached clips. (for an idea see my below code).

Anyway its a bit of a mess as you can see, so anyhints would be great


stop();
//->Prototypes
_global.DrawCfg = function(){}
DrawCfg.prototype = new MovieClip();
DrawCfg.prototype.onLoad = doLabels();//for some reason anonymous function doesnt work??
function doLabels()
{
               // 'label' is the name of textfield within the mc library item
	this.label.text= LoadedVars["label" + i];
}

DrawCfg.prototype.onPress = function()
{
	//go play this clip
	_root.cfgClip.gotoAndPlay("start");
}

//->load text
LoadedVars = new LoadVars();
LoadedVars.load("cfgText.txt");

LoadedVars.onLoad = function(success)
{		
	if(success && LoadedVars.read == "ok")//if the text file has loaded, AND flash has read the whole textfile
	{			
		trace("LOADED");
		//->initiliase code example area
		_root.anchor2.attachMovie("largeDialogue", "largeDialogue1", 2, {_x:0, _y:0});
		_root.anchor2["largeDialogue1"].exampleLabel.text = LoadedVars.codeLabel;
		_root.anchor2["largeDialogue1"].exampleComments.text = LoadedVars.codeComments
		_root.anchor2["largeDialogue1"].exampleCode.text = LoadedVars.codeExample; 
		
		//->initialise cfg steps area
		numb = parseInt(LoadedVars.numbSteps);//number of dialogues to attach
		vSpace = 0;//vertical spacing between dialogues
		hSpace = 0;//horizontal spacing between dialogues
		
		for(var i=1; i <= numb; i++)
		{
			trace("LOOP" + i);
			
			_root.anchor1.attachMovie("smallDialogue", "dialogue" + i, i, {_y:vSpace, _x;hSpace});
			if(LoadedVars["label" + i] == "1. Draw CFG")
			{				
				_root.anchor1["dialogue" + i].__proto__= DrawCfg.prototype;
				_root.anchor1["dialogue" +i].stepLabel.text = LoadedVars["label"+i];
				_root.anchor1["dialogue" +i].body.stepText.text = LoadedVars["info"+i];
			}
			else if(LoadedVars["label" + i] == "2. Calculate Basis Paths")
			{
				//.....
			}
			else if(LoadedVars["label" + i] == "3. Identify Basis Paths")
			{
				//....
			}
			vSpace+=35;
		}
		_root.gotoAndPlay("startDemo");//on finish dialogue intialisation start rest of movie				 
	}
	else
	{
		trace("LOAD FAILED");
	}
	
	
	
}

Mmmhh… Old thread indeed :slight_smile:

Anyway, welcome, mindFriction. Can you explain what your code does, and what it does not?

Hi ilyaslamasse,

I just checked and I saw that you are online. :D!

Just working on clarifying my thread question, should be done soon!

Thanks

without going too much into the code I can point out a few errors

  1. DrawCfg.prototype.onLoad = doLabels();
    -that assigns the onLoad to the return value of that function as it is at that point called. If you want the onLoad to BE that function, dont use the parens
    -that function doLabels isnt even defined at that point of assignment either, so that line above assigns onLoad to be nothing. You’ll need to declare doLabels before assigning it anywhere.
  2. because you are using a new seperate object to handle the attached clips, you will need to use Object.register class to associate the attached clips with that object.
    DrawCfg.prototype = new MovieClip();
    alone doesnt do that. All that means is that the DrawCfg object inherits from the movieclip object so it can use its properties/methods etc. For Object.registerclass, youd use something like

Object.registerClass(“largeDialogue”, DrawCfg);

so that everytime a largeDialogue library item is attached, the DrawCfg constructor is run and the prototypes are associated with the new clip.

hope this helps :slight_smile:

My newly added Mc’s which are essentially dialogues, (ie topbar and textarea with scroller) play a different movies via onPress event depending on the content they contain. (this content is ynamically addedvia a textfile/ LoadVars Obj. Hence I thought I could populate the textfields, check their content
and add appropriate functionality via prototyping on the fly. Hmm make more sense?


stop();
//->Prototypes;
										  
_global.DrawCfg = function(){}
DrawCfg.prototype = new MovieClip();
DrawCfg.prototype.onLoad = doLabels();//for some reason anonymous function doesnt work??
function doLabels()
{
               // 'label' is the name of textfield within the mc library item
	this.label.text= LoadedVars["label" + i];
}
DrawCfg.prototype.onPress = function()
{
	//go play this clip
	_root.cfgClip.gotoAndPlay("start");
}

//->load text
//Here I get all the content from a external txt file, in the text file -> 
//&numbSteps=3&label=1. Draw CFG&info=The CFG is blah blah&label2=2. Calc Basis Paths&info=bah blah
LoadedVars = new LoadVars();
LoadedVars.load("cfgText.txt");

LoadedVars.onLoad = function(success)
{		
	if(success && LoadedVars.read == "ok")//if the text file has loaded, AND flash has read the whole textfile
	{			
		trace("LOADED");
		//->initiliase code example area
		_root.anchor2.attachMovie("largeDialogue", "largeDialogue1", 2, {_x:0, _y:0});
		_root.anchor2["largeDialogue1"].exampleLabel.text = LoadedVars.codeLabel;
		_root.anchor2["largeDialogue1"].exampleComments.text = LoadedVars.codeComments
		_root.anchor2["largeDialogue1"].exampleCode.text = LoadedVars.codeExample; 
		
		//->initialise cfg steps area
		numb = parseInt(LoadedVars.numbSteps);//number of dialogues to attach
		vSpace = 0;//vertical spacing between dialogues
		hSpace = 0;//horizontal spacing between dialogues
		
		for(var i=1; i <= numb; i++)
		{
			trace("LOOP" + i);
			
			_root.anchor1.attachMovie("smallDialogue", "dialogue" + i, i, {_y:vSpace, _x;hSpace});
			if(LoadedVars["label" + i] == "1. Draw CFG")
			{				
				_root.anchor1["dialogue" + i].__proto__= DrawCfg.prototype;
				_root.anchor1["dialogue" +i].stepLabel.text = LoadedVars["label"+i];
				_root.anchor1["dialogue" +i].body.stepText.text = LoadedVars["info"+i];
			}
			else if(LoadedVars["label" + i] == "2. Calculate Basis Paths")
			{
				//.....
			}
			else if(LoadedVars["label" + i] == "3. Identify Basis Paths")
			{
				//....
			}
			vSpace+=35;
		}
		_root.gotoAndPlay("startDemo");//on finish dialogue intialisation start rest of movie				 
	}
	else
	{
		trace("LOAD FAILED");
	}
	
	
	
}

Problems im having:
1.Popualting the textfileds within the newly attached mc’s via the prototyping
2.Im not sure whether Im doing everything in an effecient way

Ill attach the code in sep posting to clarify

*Originally posted by senocular *
**without going too much into the code I can point out a few errors
**

Hi T, well Im just going to strip down my code and rough it out with comments and rename my variables to me meaningful names :slight_smile: And i think i’ll give everybody a better understanding of what im trying to do. Ive sort of lost sight of what I was trying to do as Ive been fiddling with this code too much!

Hi guys, (senocular,ilyaslamasse) sorry about all the postings, its just Im finding this really hard and Im not going to rest until I gigure it out :/.
Anyway here’s a simplified version which im sure will make things a little easier to understand


stop();
//->Prototypes
_global.InitDialogue = function(){}
InitDialogue.prototype = new MovieClip();
InitDialogue.prototype.onLoad = function()
{
	this._x=0   //->//Not sure how to do this but all dialogues attached to the empty "anchoring" clip
	this._y+=35 //->//need to be aligned left one fater the other vertically
	//->//not sure what else would go here
}

InitDialogue.prototype.onPress = function()
{
	//->//Each dialogue has a playback button to load and play a small movie relative to its content
}
InitDialogue.prototype.onPress = function()
{
	//->//Each dialogue has drag ability to it when user holds and drags top bar of dialogue
}

this.onLoad = function()
{
	//Creat LoadVars Obj load in txt vars for dialogue
	LoadedVars = new LoadVars();
	LoadedVars.load("dialogueVars.txt");
	
	LoadedVars.onLoad = function(success)
	{
		if(success)
		{
			numb = parseInt(LoadedVars.numbDialogues)// a var storing number of dialogues required
													 //to be created  
			for(var i,i < numb; i++)                               
			{   //all dialogues placed on after the other vertically
				emptyClip.attachMovie("dialogueMc", "dialogue" + i, i, //->//call prototype to initialise dialogues			}
									                                   //label and content textfields and
																	   //to pass 'playback' functionality to play button
																	   //on dialogues top bar. Each attached Mc
																	   //will have a different movie (mc) to load and play
																	   //when 'playback' button pressed,so a parameter
																	   //needs to be passed somehow?
		}
		else
		{
			//->//alert!, possibley FS quit movie as it wont work without txt file
		}
}