Thanks Julien, you confirmed what i thought to be the best programming
practice.
Mike
On 07/06/06, Julien Vignali <[EMAIL PROTECTED]> wrote:
Hi Mike,
You can easily achieve this with the EventDispatcher & Delegate classes
like this:
import mx.events.EventDispatcher;
class cMyMenu{
// let's declare static vars for menu states/options
public static var OPTION1_EVENT:String = "onOption1";
public static var OPTION2_EVENT:String = "onOption2";
// mix-ins functions of EventDispatcher
private function dispatchEvent:Function;
private function removeEventListener:Function;
public function addEventListener:Function;
// menu buttons
private var button1:MovieClip;
private var button2:MovieClip;
// constructor
public function cMyMenu(){
// do the mix-in
EventDispatcher.initialize(this);
init();
}
public function onLoad():Void {
// let's wire buttons events with functions and proper scope
button1.onRelease = Delegate.create(this, this.onButton1Click);
button2.onRelease = Delegate.create(this, this.onButton2Click);
}
private function onButton1Click():Void {
// do some menu graphic things like disabling button1, etc...
button1.enabled = false;
button2.enabled = true;
dispatchEvent({type:OPTION1_EVENT, target:this});
}
private function onButton2Click():Void {
// do some menu graphic things..
button2.enabled = false;
button1.enabled = true;
dispatchEvent({type:OPTION2_EVENT, target:this});
}
}
import mx.utils.Delegate;
class cMyGame {
private var menu:cMyMenu;
public function cMyMenu(){
// let's listen to menu events
menu.addEventListener(cMyMenu.OPTION1_EVENT, this);
menu.addEventListener(cMyMenu.OPTION2_EVENT, this);
}
// implements the default event handler called by EventDispatcher
// but we could have create 2 functions named :
// onOption1() for button1 menu event...
// onOption2() for button2 menu event...
private function handleEvent(event:Object):Void {
var type:String = event.type;
switch(type) {
case cMyMenu.OPTION1_EVENT:
// do some stuff here (disable or load a movie...)
break;
case cMyMenu.OPTION2_EVENT:
// do some other stuff (play sound...)
break;
}
}
}
For more options & understandings :
http://www.gskinner.com/blog/archives/000023.html
http://www.osflash.org/flashcoders/as2
http://www.person13.com/articles/proxy/Proxy.htm
http://www.adobe.com/devnet/flash/articles/eventproxy.html
http://www.actionscript.org/tutorials/beginner/the_delegate_class/index.shtml
mike cann a écrit :
> Hi all,
>
> Im kinda new to the whole event dispatching and listening way of doing
> things that the v2 components have introduced me to and would like some
> advice on how to go about coding this situation:
>
> Suppose i have a game which is represented by a class cMyGame and within
> there i have have my main menu system called cMyMenu. Some code:
>
>
> class cMyGame
> {
> private var myMenu;
>
> function cMyGame()
> {
> myMenu = new cMyMenu();
> }
> }
>
>
> class cMyMenu
> {
> function cMyMenu()
> {
> {
> }
>
>
> Okay now suppose that inside my menu class i have a load of button
> componants to represent the different menu options. When a user clicks
one
> of the menu options i would like the game to then destroy the main menu
(or
> atleast hide it) and then go to the option that the user clicked on.
>
> What i would like to know if how the menu system should tell the cMyGame
> what to do if the user clicked a button. As i see it there are a coupple
of
> ways, i could pass a reference to the cMyGame into the constructor then
> just
> call it like so:
>
> class cMyMenu
> {
>
> private var myParent:cMyGame;
>
> function cMyMenu(parent:cMyGame)
> {
> myParent = parent;
> {
>
> function onMenuOption1Released()
> {
> myParent.doSomething();
> }
> }
>
> Or the alternative is to use events and listeners so that the game can
> listen to events fired by the menu class.
>
> I know this seems like a very basic problem but it is at the core of my
> fundemental understanding of how you should scope your project. So if
> anyone
> could advise me on the best programming practice i should be using it
would
> be greatly appreciated, thanks :)
>
> Mike
> _______________________________________________
> [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
_______________________________________________
[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