Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communication between subcclasses 2

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
Hi. I would like to know how to communicate between two classes using the following method:

test.swf
Code:
_global.CORE = new game.core();
CORE.initialize();

game/Core.as
Code:
class Core extends Object
{
   var oFirstClass, oSecondClass;

   function Core(){
   
   }

   function initialize(){
      oFirstClass = new game.FirstClass();
      oSecondClass = new game.SecondClass();
   }

   function get firstclass(){
      return oFirstClass;
   }
}

game.FirstClass.as
Code:
class game.FirstClass extends game.Core
{
   function FirstClass(){
   
   }

   function TraceText(){
      trace("Test");
   }
}

game.SecondClass.as
Code:
class game.SecondClass extends game.Core
{
   function SecondClass(){
      oFirstClass.TraceText()
   }
}

So, that's the basic structure. If I call oFirstClass.TraceText() in the Core.as file, it will output the text. However, if I call oFirstClass.TraceText() or this.oFirstClass.TraceText() from the SecondClass, it won't shows the text.

What I don't understand is that subclasses are supposed to get properties and methods from the superclass, but a trace(oFirstClass) or this.oFirstClass nor this.firstclass() will show undefined :/ All I want is to use classes without putting everrything static and calling stuff like dir.class.function() and dir.class.property
 
Basically, what I want to do is the following structure:

Game.swf
Core.as -> Class1.as
-> Class2.as
-> Class3.as
-> Class4.as

and so on. What I would like is that Class4 may talk to Class1 by going by Core (eg: in class4: this.Core.Class1.function() ). I don't want to use static functions/variables.

Once I get it to work, i'll be able to make more branches. I searched for about 5 hours on google for "flash class tutorial" or "flash game tutorial" or similar things, but all I found was basic classes :x

 
I dont think you're understanding inheritance correctly. It looks like you're mixing up inheritance and composition.
A class defines a type of object. With inheritance you can create a class (subclass) which inherits the features of another class (the superclass).

The Superclass doesnt control or instantiate the subclasses , rather the Subclass is a TYPE of Superclass, has all its properties and methods which can be used, added to, or overridden etc.

Havent got time to write a tute on inheritance as its such a big subject! But a good place to start is Colin Moocks book Essential Actionscript 2.0.

These links might help you out too:
 
I'm not sure if the way you're doing is the best way, but here's a hack for you:

Class Core:
Code:
class game.Core extends Object
{
	var oFirstClass : game.FirstClass, oSecondClass : game.SecondClass;
	function Core ()
	{
	}
	function initialize () : Void
	{
		oFirstClass = new game.FirstClass ();
		oSecondClass = new game.SecondClass (this);
	}
}
Class FirstClass:
Code:
class game.FirstClass extends game.Core
{
	function FirstClass ()
	{
	}
	function TraceText ()
	{
		trace ("Test");
	}
}
Class SecondClass:
Code:
class game.SecondClass extends game.Core
{
	var ancestor : game.Core;
	function SecondClass (o)
	{
		ancestor = o;
		ancestor.oFirstClass.TraceText ();
	}
}
Timeline:
Code:
var coreInstance:game.Core = new game.Core();
coreInstance.initialize();
Output:
Test

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top