hopefully this does not spark a big debate. always try to favour composition
over inheritance, not that extending movieclip is a bad sollution, however
then you start dealing with linkageID's in the ide and all that schlep.
anyway that said this is how to fix the code.
import mx.utils.Delegate;
class Ball extends MovieClip {
public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;
public function Ball () {
// initialize EventDispatcher
mx.events.EventDispatcher.initialize(this);
// create a ball MC
var aBall:MovieClip = _level0.createEmptyMovieClip('testBall',
getNextHighestDepth());
// draw ball
aBall._x = 100;
aBall._y = 100;
aBall.lineStyle(20, 0xff0000, 100);
aBall.lineTo(0,.2);
declareEventMethods();
}
public function declareEventMethods():Void {
aBall.onMouseDown = Delegate.create(this,onMouseDown);
aBall.onMouseUp = Delegate.create(this,onMouseUp );
aBall.onPress = Delegate.create(this,onPress );
aBall.onRelease= Delegate.create(this,onRelease);
}
private function onMouseDown() {
dispatchEvent( {type: "mouseDown", target: this} );
}
private function onMouseUp () {
dispatchEvent( {type: "mouseUp", target: this} );
}
private function onPress () {
dispatchEvent( {type: "press", target: this} );
}
private function onRelease() {
dispatchEvent( {type: "release", target: this} );
}
}
On 4/12/06, Mike Anderson <[EMAIL PROTECTED]> wrote:
>
> If you are extending a MovieClip - doesn't the MovieClip already have
> all those things "you are trying to recreate" at your disposal?
>
> That is the whole point of extending a MovieClip - so you DON'T have to
> go through all the stuff you are going through right now.
>
> I am no guru at this stuff, but I write Class Files on a daily basis,
> and I NEVER have to deal with stuff like this. I create custom
> listeners, and dispatch events on a regular basis - and it works just
> beautifully.
>
> It just seems like you are reinventing the wheel, and defeating the
> whole purpose of why things were created this way in the first place.
>
> This is just my initial reaction to what I've seen, and I am sure some
> more experienced people will chime in on this thread.
>
> Mike
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Daniel
> Cardenas
> Sent: Wednesday, April 12, 2006 7:10 PM
> To: [email protected]
> Subject: [Flashcoders] Need help understanding EventDispatcher
>
>
> Hi,
>
> I'm trying to learn event handling using the EventDispatcher class and
> am having a hard time getting a test to work. I'm also fairly new to
> OOP, so I'm having a hard time finding the problem in my code.
>
> I've created a simple "Ball" class that creates an empty MC and then
> draws a circle in it. I've defined some event methods in the class and
> contained them within a "declareEventMethods" method. I don't know if
> that's the right way to do it or not - I modeled this test on various
> tutorials I've found online, but it's not working.
>
> What I'm trying to do is create a new instance of the "Ball" class
> called "myBall", then notify an event handler of events dispatched by
> myBall. The handler should then execute a trace command.
>
> The "Ball" class assigns the name "testBall" to the MC it creates, and I
> can trigger the event handler if I define a method directly on the MC
> that's created, ie:
> testBall.onPress = function () {myEventHandler(testBall);}
>
> but that's not what I'm trying to accomplish.
>
> I've pasted the code below - any help would be greatly appreciated.
>
> Thanks,
>
> Daniel Cardenas
>
>
> ------------------------------------------------------
> Ball Class
> ------------------------------------------------------
>
> class Ball extends MovieClip {
>
> public var addEventListener:Function;
> public var removeEventListener:Function;
> private var dispatchEvent:Function;
>
> public function Ball () {
> // initialize EventDispatcher
> mx.events.EventDispatcher.initialize(this);
>
> // create a ball MC
> var aBall:MovieClip = _level0.createEmptyMovieClip('testBall',
> getNextHighestDepth());
>
> // draw ball
> aBall._x = 100;
> aBall._y = 100;
> aBall.lineStyle(20, 0xff0000, 100);
> aBall.lineTo(0,.2);
> }
>
> public function declareEventMethods():Void {
> onMouseDown = function() {
> dispatchEvent( {type: "mouseDown", target: this} );
> }
> onMouseUp = function() {
> dispatchEvent( {type: "mouseUp", target: this} );
> }
> onPress = function() {
> dispatchEvent( {type: "press", target: this} );
> }
> onRelease = function() {
> dispatchEvent( {type: "release", target: this} );
> }
> }
>
> }
>
> ------------------------------------------------------
> Timeline Code
> ------------------------------------------------------
>
> /* create object
> ----------------------------------*/
> var myBall:Ball = new Ball();
> myBall.declareEventMethods();
>
> /* add listener
> ----------------------------------*/
> myBall.addEventListener("mouseDown", myEventHandler);
> myBall.addEventListener("mouseUp", myEventHandler);
> myBall.addEventListener("press", myEventHandler);
> myBall.addEventListener("release", myEventHandler);
>
>
> /* define handler
> ----------------------------------*/
> function myEventHandler(evt:Object):Void {
> trace('A ' + evt.type + ' event was dispatched by ' + evt.target); }
>
> // this works - but it's not what I'm trying to do...
> // testBall.onPress = function () {myEventHandler(testBall);}
>
>
>
>
> -------------------------------------------------------
> [EMAIL PROTECTED]
> 917-750-6398
> AIM: dcardena
> -------------------------------------------------------
>
>
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training http://www.figleaf.com
> http://training.figleaf.com
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
--
j:pn
http://www.lennel.org
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com