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
-------------------------------------------------------
_______________________________________________
Flashcoders@chattyfig.figleaf.com
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