RE: [Flashcoders] Best way to access my main class?
*Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Option 3! Always opt to use event based architecture. It promotes loose coupling of your components. Although it might be a bit more code, you will be able to use BouncingBall within many games, or other applications. On Thu, Feb 5, 2009 at 10:02 AM, Merrill, Jason jason.merr...@bankofamerica.com wrote: *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Cheers, Nate http://blog.natebeck.net ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
My preference is usually option 3, using an event. It means you can use your ball in other situations, where that call back to the root may or may not be necessary. Regarding cleaning up references in event listeners, you can use a weak reference when you call addEventListener(). Also, I often find that existing flash or flex events match the nature of what I want to express, so I re-use theirs sometimes which saves writing a new event class. Option 4 would be to make MyGame implement some interface (Game? IGame?) which specifies that function. Then the BouncingBall constructor is passed a reference to the interface instead of the whole MyGame class. Same as option 2, really, but this way it's plausible that you would implement the interface with some other game-like class and use BouncingBall in that. Option 5 would be to pass a Function reference into the constructor of BouncingBall. This is very generic, like the event solution, but has a few downsides: 1) function specifications are not maintained by the compiler when you pass Function references, so if you write a bug into the call of the function, you'll get a run-time error instead of a compile-time error. 2) Exactly one object can listen for this function call, unlike an event, which can be listened for by any number of clients. I often use Option 5 for success and failure callbacks when something won't complete right away. Dave On Thu, Feb 5, 2009 at 9:44 AM, Todd Kerpelman t...@kerp.net wrote: Hey, FlashCoders. I'm wondering if you can help me out with a general style question that I keep running into. Let's say I've got a setup like this... class MyGame extends MovieClip - It creates a camera sprite that I can add children into - It then creates a Bouncing Ball object with the camera sprite as the parent to use. class BouncingBall { public function BouncingBall(parentToUse:DisplayObjectContainer) { // Create member variable _mySprite:Sprite and add it to my parentToUse //... } } For reasons I won't get into unless you're really interested, BouncingBall does NOT extend Sprite, it simply contains a sprite. So MyGame has a camera as a child. That camera has my bouncingBall._mySprite as a child. The question is this: I want the BouncingBall sprite to occasionally call a function in MyGame. What's the best way to do this? *Option 1:* Within BouncingBall, just call... MyGame(_mySprite.root).foo(_myVar); This works, but it strikes me as a little unnatural, since I have to dig into my member variable and get its root. Also, I'm not sure this works if my game were to be imported by some larger, wrapper class. *Option 2: *My constructor for BouncingBall should contain two variables public function BouncingBall (var parentToUse:DisplayObjectContainer, var gameApplication:MyGame) I store gameApplication as a member variable, and use it later... _myGame.foo(_myVar); This is my current solution, but the idea of passing a parent and the game application to the constructor strikes me as slightly redundant, and, like Option 1, it tightly couples my BouncingBall object to my main application. *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. I'm sure all of you have encountered this situation before. So what do you generally do? Is there a fourth, totally obvious option that I'm overlooking? Thanks! --Todd ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Agreed on Option 3. Custom event inclusive of whatever kind of data you want to send to the associated listening method. I think I whip up custom events more than almost anything else. - Eric On Thu, Feb 5, 2009 at 1:30 PM, Nate Beck n...@tldstudio.com wrote: Option 3! Always opt to use event based architecture. It promotes loose coupling of your components. Although it might be a bit more code, you will be able to use BouncingBall within many games, or other applications. On Thu, Feb 5, 2009 at 10:02 AM, Merrill, Jason jason.merr...@bankofamerica.com wrote: *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Cheers, Nate http://blog.natebeck.net ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- http://ericd.net Interactive design and development ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Best way to access my main class?
I think I whip up custom events more than almost anything else. Yup - me too - in fact, in FlashDevelop, I have a nice event template in the form of a code snippet for a new custom events I use all the time: package { import flash.events.Event; public class extends Event { public static var :String = ; public function (type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } } Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Additionally, you should take a look at what the code is doing. Why does Bouncing Ball have to call something in the parent? Is it some sort of notification? (I've finished animating/I've hit a wall!). In which case an event is definitely the right solution. Is it to gain information of some sort? In which case, an event won't do it. Consider passing that information in at creation of the ball. If that doesn't make sense - you need to query something in the parent at regular intervals - consider whether the code inside BouncingBall really belongs in BouncingBall. Are you sure whatever it's doing shouldn't be in the parent class instead? Think about black box models - for example, if it's calling the parent to query about collision detection or some such, it's not the ball's _job_ to know whether it's collided with anything; it's the parent's. If you really need to talk to the parent/regularly fetch changing information from the parent and it _is_ the ball's business to do it, I'd be looking at using interfaces if I wanted everything to be properly decoupled. Hope that helps, Ian On Thu, Feb 5, 2009 at 7:07 PM, Merrill, Jason jason.merr...@bankofamerica.com wrote: I think I whip up custom events more than almost anything else. Yup - me too - in fact, in FlashDevelop, I have a nice event template in the form of a code snippet for a new custom events I use all the time: package { import flash.events.Event; public class extends Event { public static var :String = ; public function (type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } } ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Look up event bubbling. Essentially if you dispatch your event with bubble=true (the second parameter to an Event constructor), then the event will 'bubble' all the way up the display hierarchy. So your document class will only have to listen to itself for a given event - if any of its children dispatch that event, it'll be notified. HTH, Ian On Thu, Feb 5, 2009 at 7:14 PM, Lehr, Ross (N-SGIS) ross.l...@lmco.com wrote: This brings up a question I had about events. Is there a way to send an event all the way to the document class, no matter where it's been dispatched from? For instance, I have a document class that creates a menu class, which creates several icon button classes. I want the document class to be able to receive the event dispatched from the icon button. Currently, the only way I know how to do it is have the icon button send it to the menu and then the menu send it to the document. So, my question is, can the document class receive an event directly sent from the icon button? Thanks, Ross -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Merrill, Jason Sent: Thursday, February 05, 2009 1:03 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Best way to access my main class?
This brings up a question I had about events. Is there a way to send an event all the way to the document class, no matter where it's been dispatched from? For instance, I have a document class that creates a menu class, which creates several icon button classes. I want the document class to be able to receive the event dispatched from the icon button. Currently, the only way I know how to do it is have the icon button send it to the menu and then the menu send it to the document. So, my question is, can the document class receive an event directly sent from the icon button? Thanks, Ross -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Merrill, Jason Sent: Thursday, February 05, 2009 1:03 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Best way to access my main class?
Yes - just turn event bubbling on - then any DisplayObject like sprite or movie clip will pass the event up the display list. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Lehr, Ross (N-SGIS) Sent: Thursday, February 05, 2009 2:15 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? This brings up a question I had about events. Is there a way to send an event all the way to the document class, no matter where it's been dispatched from? For instance, I have a document class that creates a menu class, which creates several icon button classes. I want the document class to be able to receive the event dispatched from the icon button. Currently, the only way I know how to do it is have the icon button send it to the menu and then the menu send it to the document. So, my question is, can the document class receive an event directly sent from the icon button? Thanks, Ross -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Merrill, Jason Sent: Thursday, February 05, 2009 1:03 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Thanks, everybody! Sounds like creating custom events is the way to go (and yes, it's for Send an alert of some kind to MyGame kind of functionality). I don't have a lot of experience with 'em, so I was probably going through more trouble to avoid them than would actually be required to just suck it up and do things the right way. :) --T On Thu, Feb 5, 2009 at 11:31 AM, Merrill, Jason jason.merr...@bankofamerica.com wrote: Yes - just turn event bubbling on - then any DisplayObject like sprite or movie clip will pass the event up the display list. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto: flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Lehr, Ross (N-SGIS) Sent: Thursday, February 05, 2009 2:15 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? This brings up a question I had about events. Is there a way to send an event all the way to the document class, no matter where it's been dispatched from? For instance, I have a document class that creates a menu class, which creates several icon button classes. I want the document class to be able to receive the event dispatched from the icon button. Currently, the only way I know how to do it is have the icon button send it to the menu and then the menu send it to the document. So, my question is, can the document class receive an event directly sent from the icon button? Thanks, Ross -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto: flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Merrill, Jason Sent: Thursday, February 05, 2009 1:03 PM To: Flash Coders List Subject: RE: [Flashcoders] Best way to access my main class? *Option 3:* I create a custom event, dispatch that event, and create a listener in MyGame rather than call a function directly. I'm guessing this is the best way to go theoretically, and will allow me to reuse my BouncingBall object in other applications, but it's a lot of extra code, and I constantly worry about not property cleaning up event listeners. Hands down, your option 3 is what you should do. So what if it's some extra code? It's the right way to accomplish this. Your object should not target and call methods in other classes outside of it, that's extremely tight coupling, which is bad. So have your bouncing ball sprite dispatch a custom event, have the other class listen for that same custom event and do whatever logic you want, like call another method. It's not messy if you keep your code clean. It just takes practice of doing this a lot before you realize an architecture emerges in your coding you are familiar with. I think its even messier to do option 1 or 2, if that makes you feel any better. Jason Merrill Bank of America Instructional Technology Media · Learning Performance Solutions LLD Interested in Flash Platform technologies? Join the Bank of America Flash Platform Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Best way to access my main class?
Look into Event bubbling http://www.google.com/search?hl=enq=AS3+event+bubblingmeta= - Original Message - From: Lehr, Ross (N-SGIS) ross.l...@lmco.com To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Thursday, February 05, 2009 8:14 PM Subject: RE: [Flashcoders] Best way to access my main class? This brings up a question I had about events. Is there a way to send an event all the way to the document class, no matter where it's been dispatched from? For instance, I have a document class that creates a menu class, which creates several icon button classes. I want the document class to be able to receive the event dispatched from the icon button. Currently, the only way I know how to do it is have the icon button send it to the menu and then the menu send it to the document. So, my question is, can the document class receive an event directly sent from the icon button? Thanks, Ross ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders