View Full Version : accessing a child
Pier25
August 9th, 2007, 10:06 AM
Hi, I'm super new into AS3 and OOP...
I'm trying to use the Papervision 3d or PV3D for a new project and first I'm trying to make it work looking at examples and so.
There's an example that adds a child to the scene
scene.addChild(plane);
and then they access that plane from a method inside the same class simply by:
plane.rotationX += 4.35;
This works, so logically it's right... but it sounds more logical to me to write scene.plane.property... even if it doen't work because of compiler errors.
Can someone explain this to me?
I'm a designer walking into the dark path of programming... well dark for me :)
tecknoize
August 9th, 2007, 10:26 AM
Well I didn't had the chance to work with Papervision but I'm pretty sure that the scene Object is not dynamic (you can't call property that are undefined in the class), which is why scene.plane.prop throw an error.
To understand the logic behind this, I'll throw a very cheesy/top-of-my-head analogy: Let's say we both have a cell phone. I can call you from anywhere, and you can too. So even if you enter your house, I can still call you, because I have your cell phone number. Variables works in somewhat the same way. If you have a reference to an Object, in this case, plane, and you add it to the scene, you can still call plane because your plane variable still exist. You must be aware tho of variable scope, which is, in short : a variable declared inside a block of code {} is only accessible within that block, and any subsequent block, like this.
{
var a:Number = 5;
if(a==5)
{
trace("a is accessible" + a);
}
}
trace("a is not accessible" + a);
Hope it's clear :ex:
Pier25
August 9th, 2007, 10:45 AM
hmmm ok so my problem was understanding the scope of the child...
Ok so let's say I wanted to access the plane object from outside of that class...
Before in as2 it used to be _root.movieclip.property... but now?
Thanks for your help!
tecknoize
August 9th, 2007, 11:11 AM
Well you can access _root.movieclip.property because the MovieClip class is dynamic. To access the plane property outside of that class you need to define it as a public property like this :
package {
public class MyClass {
public var plane:IDontKnowTheType;
public function MyClass()
{
//constructor
}
//methods here...
}
}See how plane will be accessible everywhere inside the class, and also, since it's public, outside the class.
var myClass:MyClass = new MyClass();
myClass.plane;
I should mention that setting a property to public is not very recommended, simply because the object (here, MyClass) have no more control on his own property. It's like having your cellphone number printed on your forehead. Anyone can access it, an anyone can change it without you to know (well I guess you would feel it if someone erase something printed on your forehead, but follow me here :P ). Instead, you should use getter/setter. They work the same way as methods, but give you a property-like access.
//underscore is a convention I and many people use for private variables
private var _plane:IDontKnowTheType;
public function get plane():IDontKnowTheType
{
return _plane;
}
public function set plane(value:IDontKnowTheType):void
{
_plane = value;
}
You can also create read only property by removing the "function set plane"... This way, from the outside, you cannot assign something to plane, but you can read it.
Hope it make sense!
Pier25
August 9th, 2007, 11:33 AM
This is starting to make sense... thank you again! ;)
Ok so I understand the public/private thing.... but I dont really understand why it's better to access the object with public methods (because if the methods aren't public you can't access them I supose) than the objects directly.
tecknoize
August 9th, 2007, 02:36 PM
My bad, forgot to make the get/set method public. They should be public and I've corrected my last post.
Giving external access to properties with get/set is better and here's why. Let's say your plane variable is very important through all the functions in your class. If you simply define it as public (without get/set), here's what I can do from outside :
var myClass:MyClass = new MyClass();
myClass.plane = null;
//or
myClass.plane = somethingTotallyDifferent;
And your Class will not know about those changes. Setting it to null will then break every function in your class that use the plane property.
What can you do to prevent this? Make it Read Only, like this :
private var _plane:Plane;
public function get plane():Plane
{
return _plane;
}
If you don't define a set function, your property is considered read-only.
That means you can't assign something to it from outside.
trace(myClass.plane); //we can read
myClass.plane = null; //we can't assign, compiler will scream
Another quality of get/set function is that you can execute something when accessing the variable. Here, I have a TextField variable named _txtField. I want to provide a easy way of changing this TextField's text without doing myClass.textField.text. While there's nothing wrong with setting a read-only public property called textField, you wouldn't have any ways of knowing that the text of the textField has changed. Basically, getter/setter are giving you more control over your variables.
private var _txtField:TextField;
public function get text():String
{
return _txtField.text;
}
public function set text(value:String):void
{
_txtField.text = value;
//Here I dispatch an event when the text change, something I couldn't do with the public textfield method.
dispatchEvent(new Event(Event.CHANGE));
}
Hope it clear things up!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.