I've always had problems with this...
Say I've got two classes, Ball.as and Controller.as.
The Ball class contains all the properties and methods that pertains to the ball itself (and is attached to the ball via linkage) and controller contains the functions that pertain to the ball's position within the fla. An instance of the controller class is called in frame 1 of the fla var controller:Controller = new Controller(this);
When the ball hits a certain x, y coordinate, I want something else to happen in the fla (say a different movie clip changes colour).
The way I've been doing it up to now is by calling a function in the controller class from the ball class (this._parent.controller.someFunction) which I know is not the best way to do it, because it limits the reuseability because the context might be different in the next fla that uses these classes.
Can I use a listener to say "When a ball hits a certain x, y coordinate, do something" and if so, where would I house the listener? Would it be in the controller class and if so, how would you access the listener object variable from within the Ball class?
I've done tons of searches, but they all want you to put code on the timeline... I have never found a good one that displays this in a proper object-oriented fashion...
Any advice greatly appreciated.
It's usually because everyone in the Flash world for the last couple of years has been talking OOP but few places actually utilize it properly. Guys like Arse and bit though are the brilliant ones and can give you the proper answer.
I would put it all in the Controller since it already contains references to Ball. Putting the listener in Ball would mean you're now trying to work backwards.
But I'm prepared to be wrong by the programming geniuses.
Originally posted by: Storm Putting the listener in Ball would mean you're now trying to work backwards.
Too true which is why I would put the listeners in Controller as well. My only problem is accessing the object variable from the Ball class:
If this is in Controller.as:
var someObject:Object = new Object(); // Creates broadcast object.
var myListener1:Object = new Object(); // Creates listener object. var myListener2:Object = new Object(); // Creates listener object.
myListener1.someEvent = function() { // Creates listener method. trace("myListener1 received someEvent"); } myListener2.someEvent = function() { // Createz listener method. trace("myListener2 received someEvent"); }
How do I access the above variables to call someEvent from the Ball class without creating a new instance of Controller inside Ball?
Are you perhaps requiring a DelegateWithArgs when adding the listener, so that the ball ref gets passed to whatever is listening? I would post some code but as2 has escaped me!
Originally posted by: ernieweaselfat How do I access the above variables to call someEvent from the Ball class without creating a new instance of Controller inside Ball?
that's not the way to approach it - the Ball class needs to dispatch an event when it hits that x/y coord (see dispatchEvent), and the Controller needs to be listening to the instance of the Ball class that it has, waiting for that event to happen. Then Controller does whatever it needs to. That way the Ball doesn't need to know anything about the Controller, and the Controller just needs to know that the Ball class is capable of dispatching that particular event.
Does that make sense?
Here's a tut - I haven't really followed that tut, but a quick glance makes me think it'll cover it all for you.