开发者

AS3 central event dispatcher shared between instances (not static)

I'm building a flash app (just AS3 with FlashDevelop) and I'm having some trouble keeping things loosely coupled around the event system. I've done a lot of reading about central event systems and static eventdispatchers but they don't quite fit the bill for me.

What I'm building is similar to a video player. I have a Player class which is the parent of all the other little parts of the app. The Player class extends Sprite and I've currently designed it so that you could instantiate multiple Player's and put them on the stage. I also have a Controller class which extends EventDispatcher and I dispatch all my events through this class. It's a central event class.

The problem is that I need to pass around a reference to this class so that all the other classes can dispatch and listen through it. Passing around the reference works but it's the exact opposite of loose coupling. I know I could make a static EventDispatcher that all the classes could see but then if I had two or three Player's on the stage they would all hear each others events.

How can I create a type of sandbox that will allow all the child classes of a Player ins开发者_开发知识库tance to be aware of the central dispatcher without passing a reference or making it static?


I'd suggest to use my static dispatcher, this one has an ID mechanism that allows to tell which Player instance dispatches an event.


It turns out what I was really trying to understand was loose-coupling using something like dependency injection.

I never ended up doing this in any AS3 projects as I don't do much of it anyway. Having done more C# recently I more easily grasped this concept using libraries such as StructureMap and Ninject.

For AS3 you could use a framework like Robotlegs. This will probably change the way you code AS3 and may not be for all developers/situations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜