ForumsProgramming ForumQuestion about classes and parent, etc

5 2779
clipmaster3
offline
clipmaster3
104 posts
Nomad

let's say I have two classes (as3 btw)

package{
public class Hi{
public var a:Hello;
}
}

and

package{
public class Hello{
}
}

ignore the lack of constructors etc
so Hi has an instance of Hello and can directly reference it.
is there a way to reference Hi from Hello? You can't use parent because you have to addChild first.

I could write the constructor of Hello so that you pass it an object... so insert this into the Hello class

public var parentThing:Object;
public function Hello(pT:Object){
parentThing = pT;
}

but I wanted to know if there was a built in property or something

  • 5 Replies
LonLonRanch
offline
LonLonRanch
172 posts
Nomad

This kind of thing usually requires some sort of custom event in order to communicate between the two classes. If you haven't read about custom events and the dispatchEvent method than I recommend reading about it like ostcount=80">here or here. It's a little confusing at first but that is probably the method I would recommend for this kind of situation.

dank
offline
dank
983 posts
Peasant

There are many ways, but because you do not name any context, it's hard to say which is the best.

You can give Hi static vars/functions
You can pass a reference
You can use an interface

Typically it's not good practice to traverse up a tree, only down one. Any objects/functions/interfaces that the child needs to interact with should be given to it by the parent (or grandparents further up). You should never have the entire tree open to a single node way down the list that only needs minimal information to complete its job.

clipmaster3
offline
clipmaster3
104 posts
Nomad

Yeah I know the whole situation is a little... completely not OOP. An dank, I normally try to follow the general ideas of encapsulation, it's just I was hoping to be able to access an updating value of the parent class. I've ended up just going with an event listener like LonLon said.

This next part is basically an unrelated question but I don't feel like starting a new thread (yet). Speaking of static functions, I had a function that calculated the distance between points that I was going to use in several different classes, so I made a MyMath class that held that and several other such functions. Flash won't recognize it when I call MyMath.getDistance(blah) unless I make getDistance a static function... I don't know why. (btw ignore the blah, the actual arguments aren't important)

dank
offline
dank
983 posts
Peasant

Only functions declined as static are accessible through a class call.

Also, the Point class already has a predefined function called distance. It takes two points and returns the distance.

clipmaster3
offline
clipmaster3
104 posts
Nomad

Ok so I had to put it as static to use it that way because that's apparently what "static" does to functions... I didn't really know what it did to functions before, just that with variables it makes it so all instances of the class use the same instance of that variable. So thanks.

And I did not know that about the point class. Thanks again =P

Showing 1-5 of 5