PDA

View Full Version : stupid question about classes


shoeshine159
09-18-2007, 08:58 AM
Gday, i'm new to Classes in actionscript, I've been trying to get my head around classes recently.

Here is my problem. how do i call a method (function) of an instantiated class from inside another class? nothing i do seems to work.

Heres what i'm doing. I have 3 classes.
InterfaceLayer, NodeLayer, and LoginPanel.
i instantiate them as children of DocumentClass as iLayer and nLayer.
and child of InterfaceLayer as login.

within NodeLayer is a function drawNode() which i want to call from InterfaceLayer's child login.

so within login am i allowed to call nLayer.drawNode() ? because it doesn't seem to work. "1120: Access of undefined property nLayer."

or parent.parent.nLayer "1119: Access of possibly undefined property nLayer through a reference with static type flash.display:DisplayObjectContainer."

can anyone help me from just this or should i post some source. it's gotten quite complex already.

thanks in advance.

shoeshine159
09-18-2007, 09:38 AM
solution found :D

I call:

DocumentClass(this.root).nLayer.drawNode()

YES!!

(from http://developer.yahoo.com/flash/articles/display-list.html )

Just incase anyone has a similar problem.

plutocrat
09-18-2007, 01:47 PM
The correct way is to make the functions public, then call them thus:
//in the NodeLayerClass:
public function eatCake():void {}

//and then in your Document Class:
internal var nLayer:NodeLayer
//...
nLayer.eatCake()

On the other hand, you could make them static (and call them using NodeLayerClass.eatCake()) or pass a reference of nLayer (or even the document class) in the constructor. What you are doing, however, is *pure evil* and will bite you in the ass within minutes.

shoeshine159
09-19-2007, 03:32 AM
If you don't mind my asking, why is it *pure evil* ?

it seems equivanent to what i would do when i wanted to access a root function in as2 with _root.functionName(); just with a few extra characters.

and also what do i call to get at the function from within a child of a child of the documentclass (in terms of the display list - they aren't subclasses)?
just the same thing as what you wrote for the documentClass?

thanks for your help :D

plutocrat
09-19-2007, 05:45 AM
You are confusion the object model (Class extends SuperClass implements Interface) with the Display List model ([instance0...instanceN]).

Classes are groups of variables and functions that describe the states and behaviours of an object. That object may not always have anything to do with the display list.

Read the flash help section: Programming ActionScript 3.0/Introduction of Object-Oriented Programming. You may find your answers there.