PDA

View Full Version : simple question about multiple classes


muchogrande
05-21-2008, 05:15 PM
Hi, Im new to AS3 and trying to figure out how to communicate between classes.

If I have a Document class, that on execution creates some instances of another class, how do I read/write vars and call methods of the document class from within this other (non-document) class?

I can trace (parent) from the non-document class, and it gives me the name of the document class, but I cant seem to access any properties of the doc class. Flash tells me I am trying to access a possible undefined property through a reference with static type. (Which I dont think is the case, as the prop I am trying to read is public)

thanks.

wvxvw
05-21-2008, 05:19 PM
There're several ways you can do it, but, probably the most simple would be like this:
pass reference to the document class to the instance of another class -> it'll became a variable of the child class, so you'll be able to call all public methods on it.
package {

import flash.display.Sprite;

public class DocClass extends Sprite {

public var child:ChildClass;

public function DocClass(){
super();
init();
}
function init():void {
child = new ChildClass(this);
//or
child = new ChildClass();
child.setParent(this);
}
}
}
package {
public class ChildClass {

private var _docclass:DocClass;

public function ChildClass(dc:DocClass=null) {
if(dc) _docclass = dc;
}
public function setParent(dc:DocClass):void {
_docclass = dc;
}
}
}

buginajar
05-21-2008, 06:13 PM
Personally, I would not reference other classes from a class unless it was directly related. I would try and use events whenever possible.

muchogrande
05-21-2008, 07:20 PM
wvxvw, thanks. That makes perfect sense. I also now realize that I can access the document class by using 'this.parent' from the other class. But the way I have to do it adds an extra step.

This doesnt work:

trace(this.parent.rotationEnabled) // inside other class

this does work:

var base2 = this.parent
trace(base2.rotationEnabled)

Can someone explain to me why the 1st line doesn't work?


buginajar, how would you go about this then? Here is basically what I am doing:

I have a document class that creates a display object and a variable used to control that object (a papervision sphere). The document class then generates instances of the other class (it is a simple button class). When the user clicks on one of the buttons, I need to change some vars in the document class and call a method of the document class.

I am using events on the buttons to track the click, but from there I am now changing vars/calling methods via the parent reference. I'm a newb to AS3, so if this is crazy please let me know, I'd love to learn how to do this better.

wvxvw
05-21-2008, 09:03 PM
Personally, I would not reference other classes from a class unless it was directly related. I would try and use events whenever possible.

The example I gave was supposed to be as simple as possible. Actually, there're lots of ways you can reference one class from another, depends on the purpose...

muchogrande:
You have to cast to the this.parent type of your document class, othervise compiler will treat it as if it was a DisplayObjectContainer, apparently, there's no method rotationEnabled on DisplayObjectContainer class. I.e.:
trace(MyDocumentClass(this.parent).rotationEnabled );
//or
trace((this.parent as MyDocumentClass).rotationEnabled)
var base2 = this.parent
This works because the variable you declared is untyped, which means compiler will treat it as if it was of the Object type (this allows calling any method you can imagine on it =). But, untyped variables are considered to be a bad programming style. so, you had to do it like this:
var base2:MyDocumentClass = this.parent;

muchogrande
05-21-2008, 09:15 PM
wvxvw, thanks a lot! That makes perfect sense to me now.