{"id":13743988,"url":"https://github.com/CreativeBottle/starlingMVC","last_synced_at":"2025-05-09T02:32:10.104Z","repository":{"id":3578447,"uuid":"4641219","full_name":"CreativeBottle/starlingMVC","owner":"CreativeBottle","description":"IOC Framework for Starling based games","archived":false,"fork":false,"pushed_at":"2017-04-24T17:52:02.000Z","size":1994,"stargazers_count":125,"open_issues_count":10,"forks_count":26,"subscribers_count":24,"default_branch":"master","last_synced_at":"2024-08-03T05:02:55.837Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"starlingmvc.org","language":"ActionScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CreativeBottle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-12T18:29:05.000Z","updated_at":"2024-06-12T07:40:32.000Z","dependencies_parsed_at":"2022-08-29T11:41:47.922Z","dependency_job_id":null,"html_url":"https://github.com/CreativeBottle/starlingMVC","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CreativeBottle%2FstarlingMVC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CreativeBottle%2FstarlingMVC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CreativeBottle%2FstarlingMVC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CreativeBottle%2FstarlingMVC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CreativeBottle","download_url":"https://codeload.github.com/CreativeBottle/starlingMVC/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224802884,"owners_count":17372520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T05:01:01.007Z","updated_at":"2024-11-15T15:31:23.415Z","avatar_url":"https://github.com/CreativeBottle.png","language":"ActionScript","funding_links":[],"categories":["Frameworks"],"sub_categories":["MVC Framework"],"readme":"StarlingMVC Framework\n===========\n\nStarlingMVC is an IOC framework for use in games built using the great [Starling framework](http://gamua.com/starling/). Closely modelled after established IOC frameworks like [Swiz](http://swizframework.org/) and [RobotLegs](http://www.robotlegs.org/), StarlingMVC features:\n* Dependency Injection(DI)/Inversion of Control(IOC)\n* View Mediation\n* Event Handling\n* Stays out of the way of your Starling game code\n* Simple configuration\n* Easily extended\n* More utilities to help with your game code\n\nStarlingMVC Framework is provided under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).\n\nRequirements\n------------\n* [Flex SDK 4.6](http://www.adobe.com/devnet/flex/flex-sdk-download.html)\n* [Starling 1.1](http://gamua.com/starling/)\n* [FlexUnit 4.1 (For running the unit tests)](http://www.flexunit.org/)\n\nContributors\n------------\n* [Creative Bottle, Inc](http://www.creativebottle.com)\n* [Scott Jeppesen](mailto:scott.jeppesen@creativebottle.com)\n* [Tom McAvoy](mailto:tom.mcavoy@creativebottle.com)\n\nSetup\n------------\nGetting your Starling project configured to work with StarlingMVC requires only a few lines of code. From your base Starling display class (starling.display.*), you need to create an instance of StarlingMVC and provide it the root Starling display object, an instance of StarlingMVCConfig, and some Beans.\n\n```as3\npackage com.mygame.views\n{\n  \timport com.creativebottle.starlingmvc.StarlingMVC;\n\timport com.creativebottle.starlingmvc.config.StarlingMVCConfig;\n\timport com.creativebottle.starlingmvc.views.ViewManager;\n \timport com.mygame.models.GameModel;\n\n\timport starling.core.Starling;\n\timport starling.display.Sprite;\n\n\tpublic class GameMain extends Sprite\n\t{\n\t\tprivate var starlingMVC:StarlingMVC;\n\n\t\tpublic function GameMain()\n\t\t{\n\t\t\tvar config:StarlingMVCConfig = new StarlingMVCConfig();\n\t\t\tconfig.eventPackages = [\"com.mygame.events\"];\n\t\t\tconfig.viewPackages = [\"com.mygame.views\"];\n\n\t\t\tvar beans:Array = [new GameModel(), new ViewManager(this)];\n\n\t\t\tstarlingMVC = new StarlingMVC(this, config, beans);\n\t\t}\n\t}\n}\n```\n\nThe StarlingMVCConfig instance above tells StarlingMVC which event packages and view packages it should mediate.\nThe beans Array is merely a collection of objects. The array can accept an object of any type. The framework will handle it accordingly.\n\n### Additional setup steps for Flash Builder\nWhen exporting release builds using Flash Builder, the ActionScript compiler will strip out non-standard metadata unless it is instructed otherwise. StarlingMVC's custom metadata is required in order for powerful features such as automatic dependency injection (DI) to work correctly and so the removal of the metadata can effectively render your application useless.\n\nPreventing the removal (or \"stripping\") of StarlingMVC's custom metadata is simple and just requires some additional compiler arguments to be set for your project.\n\nTo add the additional compiler arguments, right-click your project and click Properties. Next go to \"ActionScript Compiler\" and under \"Additional compiler arguments\" add the following:\n```\n-keep-as3-metadata+=Dispatcher\n-keep-as3-metadata+=EventHandler\n-keep-as3-metadata+=Inject\n-keep-as3-metadata+=Juggler\n-keep-as3-metadata+=PostConstruct\n-keep-as3-metadata+=ViewAdded\n-keep-as3-metadata+=ViewRemoved\n```\nYou will now be able to export release builds and StarlingMVC will function as expected.\n\nBeans\n------------\nA Bean is an instance of an object that is provided to StarlingMVC to manage. Beans can be injected, receive injections, and handle events. There are several ways that beans can be provided to StarlingMVC during setup:\n### Object instance\n```as3\nvar beans:Array = [new GameModel(), new ViewManager(this)];\n```\n\n\n### Bean instances\n```as3\nvar beans:Array = [new Bean(new GameModel()), new Bean(new ViewManager(this))];\n```\nProviding a Bean instance as shown above does not give much benefit. However, there is an option second parameter to thw Bean constructor that allows for an id. If you provide an id then you can use the id during dependency injection. Additionally, beans are stored within the framework by class type unless you provide an id. So if you have two beans of the same type you will need to specify an id or subsequent beans will overwrite the previous beans. For example:\n```as3\nvar beans:Array = [new Bean(new GameModel(),\"gameModelEasy\"),new Bean(new GameModel(),\"gameModelHard\"), new ViewManager(this)];\n```\n\n### BeanProvider instances\nA BeanProvider is a collection of Beans. The beans within the provider, like with a simple array, can be of any type, including BeanProvider.\n```as3\npackage com.mygame.config\n{\n\timport com.creativebottle.starlingmvc.beans.BeanProvider;\n\timport com.mygame.assets.AssetModel;\n\timport com.mygame.models.AudioModel;\n\timport com.mygame.models.GameModel;\n\n\tpublic class Models extends BeanProvider\n\t{\n\t\tpublic function Models()\n\t\t{\n\t\t\tbeans = [new GameModel(), new Bean(new AudioModel(),\"audioModel\"), new AssetModel()];\n\t\t}\n\t}\n}\n```\nOnce you have your BeanProvider set up, you can pass that as a part of your original beans array.\n```as3\nvar beans:Array = [new Models(), new ViewManager(this)];\n```\n\n### ProtoBeans\nA ProtoBean is a bean that is created at the time of injection. Where normal beans require a class instance, a ProtoBean requires a class and an id.\n```as3\nvar beans:Array = [new ProtoBean(Character,\"character\"), new ViewManager(this)];\n```\nUsing a ProtoBean here will allow StarlingMVC to create the instances of this class for you. Each time it is injected, it will be a new instance of the, in this case, \"Character\" class instead of using a singleton like a normal Bean. The advantage to allowing the framework to create the class over just using \"new Character()\" is that when StarlingMVC creates the instance it will run injection and all processing on the created instance.\n\nDependency Injection\n------------\nDependency injection occurs on all beans and all Starling display objects. A dependency is denoted with an `Inject` metadata tag over a public property or getter/setter. Injection can be done by type:\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject]\n\t\tpublic var gameModel:GameModel;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nor by id, if an id was specified when the bean was created:\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject(source=\"gameModel\")]\n\t\tpublic var gameModel:GameModel;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nIn the above example, if the GameModel is a normal bean, the framework will set the value to the singleton instance that was created during setup. If it was a protobean, a new instance will be created and injected into the property.\n\nStarling also supports injecting properties of beans. In order to use this functionality, the source Bean must contain an id (i.e. `new Bean(new GameModel(),\"gameModel\");`). To inject a property of a bean, simply append the property name to the end of the id parameter in your Inject tag:\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject(source=\"gameModel\")]\n\t\tpublic var gameModel:GameModel;\n\n\t\t[Inject(source=\"userModel.currentUser\")]\n\t\tpublic var currentUser:User;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nIn the example above, the value of the `currentUser` property on the `userModel` bean would be injected into the currentUser property of our controller. This functionality is also recursive. If you wanted to inject the first name of the currentUser you could potentially use `[Inject(source=\"userModel.currentUser.firstName\")]`.\n\n### Binding\nThe InjectProcessor also supports a very simple binding mechanism that will cause injected properties to be automatically refreshed when they are changed.\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject(source=\"gameModel\")]\n\t\tpublic var gameModel:GameModel;\n\n\t\t[Inject(source=\"userModel.currentUser\", bind=\"true\")]\n\t\tpublic var currentUser:User;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nThe example above uses the optional `bind=\"true\"` parameter of the `[Inject]` tag to create a binding.  When the currentUser property of the userModel is updated StarlingMVC will automatically update any injections using binding. This will also work with getter/setters methods. Using these will allow code to easily react to changes in the properties.\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject(source=\"gameModel\")]\n\t\tpublic var gameModel:GameModel;\n\n\t\t[Inject(source=\"userModel.currentUser\", bind=\"true\")]\n\t\tpublic function set currentUser(value:User):void\n\t\t{\n\t\t\t_currentUser = value;\n\n\t\t\t// Do something to update your UI with the new value\n\t\t}\n\n\t\tpublic function get currentUser():User\n\t\t{\n\t\t\treturn _currentUser;\n\t\t}\n\t\tprivate var _currentUser:User;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nBinding is connected directly to the Starling juggler instance and will check for changes on each bound property everytime the `advanceTime()` method is called. This does not provide a binding that works as instantaneously as Flex binding. Binding should be used sparingly as there is still overhead to check for changes to the bound properties.\n\nAs an alternative to auto bound properties, StarlingMVC supports binding through invalidation. This method is much more efficient than the auto bound properties because it give you much more control over when properties are updated. This is done through an injected `Bindings` class and the optional \"auto\" parameter.\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject(source=\"gameModel\")]\n\t\tpublic var gameModel:GameModel;\n\n\t\t[Inject(source=\"userModel.currentUser\", bind=\"true\", auto=\"false\")]\n\t\tpublic function set currentUser(value:User):void\n\t\t{\n\t\t\t_currentUser = value;\n\n\t\t\t// Do something to update your UI with the new value\n\t\t}\n\n\t\tpublic function get currentUser():User\n\t\t{\n\t\t\treturn _currentUser;\n\t\t}\n\t\tprivate var _currentUser:User;\n\n\t\tpublic function GameController():void\n\t\t{\n\n\t\t}\n\t}\n}\n\npackage com.mygame.models\n{\n\tpublic class UserModel\n\t{\n\t\t[Bindings]\n\t\tpublic var bindings:Bindings;\n\n\t\tpublic function set currentUser(value:User):void\n\t\t{\n\t\t\tif(value != _currentUser)\n\t\t\t{\n\t\t\t\t_currentUser = value;\n\n\t\t\t\tbindings.invalidate(this, \"currentUser\");\n\t\t\t}\n\t\t}\n\n\t\tpublic function get currentUser():User\n\t\t{\n\t\t\treturn _currentUser;\n\t\t}\n\t\tprivate var _currentUser:User;\n\t}\n}\n```\n\nEvents\n------------\n### Dispatching Events\nEvents in StarlingMVC are dispatched in one of two ways:\n1) StarlingMVC contains a global instance of `starling.events.EventDispatcher`. The quickest way to dispatch an event into the StarlingMVC framework is to use this dispatcher. This dispatcher can be injected into your bean by using the `[Dispatcher]` metadata tag.\n2) DisplayObjects can dispatchEvents using their own `dispatchEvent()` method. This is only available to DisplayObjects and the events must set `bubbles=true'.\n\n### Handling Events\nEvent handlers are denoted by using the `[EventHandler(event=\"\")]` metadata tag on a public method of a bean. The event argument in the tag can contain one of two options: the event type string\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[EventHandler(event=\"scoreChanged\")]\n\t\tpublic function scoreChanged(event:ScoreEvent):void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nor the typed event\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[EventHandler(event=\"ScoreEvent.SCORE_CHANGED\")]\n\t\tpublic function scoreChanged(event:ScoreEvent):void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nBy using the second approach, you will gain the benefit that StarlingMVC will type check any of the events during initialization and throw and error if the event or event type doesn't exist. This protects against typos.\n\nIn both examples above, the handler must accept the type of the dispatched event to handle. However, a second optional parameter exists in the EventHandler tag that will allow you to specify specific properties of the event to use as parameters to the event handler. For example:\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[EventHandler(event=\"ScoreEvent.SCORE_CHANGED\", properties=\"user, newScore\")]\n\t\tpublic function scoreChanged(user:User, newScore:int):void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nIn the above example, instead of passing the entire event into the handler, StarlingMVC will pass only the \"user\" and \"newScore\" properties. Note that the types must match or an error will be thrown.\n\nView Mediation\n------------\nView mediators are a great way of keeping your view classes separate from the code that controls them. A view mediator is set up just like any other bean. To link a view to a view mediator a `[ViewAdded]` metadata tag is used on a public method. When a DisplayObject is added to the stack, StarlingMVC will look for instances of the ViewAdded tag. If the parameter of any ViewAdded methods are of the type of the view that was just added, the new DisplayObject will be passed to that method. To unlink a mediator from a view when the view has been removed the `[ViewRemoved]` metadata tag is used.\n```as3\npackage com.mygame.mediators\n{\n\tpublic class GameMediator\n\t{\n\t\tprivate var view:Game;\n\n\t\t[ViewAdded]\n\t\tpublic function viewAdded(view:Game):void\n\t\t{\n\t\t\tthis.view = view;\n\t\t}\n\n\t\t[ViewRemoved]\n\t\tpublic function viewRemoved(view:Game):void\n\t\t{\n\t\t\tthis.view = null;\n\t\t}\n\t}\n}\n```\nBean Lifecycle\n------------\nNormal beans are set up on initialization. However, since the dependency injection and event handling happens after creation bean values are not available immediately. To receive notification of when a bean has been fully processed we can add the `[PostConstruct]` tag to a public method. This method will be automatically called when all DI has been completed. Similarly, when a bean is being destroyed we can specify a public method to be called with the `[PreDestroy]` tag. This works in all standard beans and DisplayObjects.\n```as3\npackage com.mygame.controllers\n{\n\tpublic class GameController\n\t{\n\t\t[Inject]\n\t\tpublic var gameModel:GameModel;\n\n\t\t[PostConstruct]\n\t\tpublic function postConstruct():void\n\t\t{\n\t\t\t// set up code here\n\t\t}\n\n\t\t[PreDestroy]\n\t\tpublic function preDestroy():void\n\t\t{\n\t\t\t// tear down code here\n\t\t}\n\n\t\t[EventHandler(event=\"ScoreEvent.SCORE_CHANGED\", properties=\"user, newScore\")]\n\t\tpublic function scoreChanged(user:User, newScore:int):void\n\t\t{\n\n\t\t}\n\t}\n}\n\npackage com.mygame.controllers\n{\n\tpublic class GameModel\n\t{\n\t\t[Bindings]\n\t\tpublic var bindings:Bindings;\n\n\t\tpublic function set score(value:int):void\n\t\t{\n\t\t\tif(value != _score)\n\t\t\t{\n\t\t\t\t_score = value;\n\n\t\t\t\tbindings.invalidate(this, \"score\");\n\t\t\t}\n\t\t}\n\n\t\tpublic function get score():int\n\t\t{\n\t\t\treturn _score;\n\t\t}\n\t\tprivate var _score:int;\n\t}\n}\n```\nIn the above example you can see that in the controller the Inject tag includes \"auto='false'\". This disables the auto binding so that a check is not done on every iteration of the Juggler. In the GameMode, we are injecting the Bindings instance and then invalidating manually when the property changes. Once the property is invalidated, it will be automatically updated on the next pass of the juggler.\n\nManual Bean Creation/Removal\n------------\nManual bean creation and removal is done through the event system. Dispatching `BeanEvent.ADD_BEAN` will add and process a new bean. Dispatching `BeanEvent.REMOVE_BEAN` will remove the bean from the system.\n```as3\npackage com.mygame.view\n{\n\tpublic class Game\n\t{\n\t\tpublic var gamePresentationModel:GamePresentationModel;\n\n\t\t[PostConstruct]\n\t\tpublic function postConstruct():void\n\t\t{\n\t\t\tgamePresentationModel = new GamePresentationModel();\n\n\t\t\tdispatchEvent(new BeanEvent(BeanEvent.ADD_BEAN, gamePresentationModel));\n\t\t}\n\n\t\t[PreDestroy]\n\t\tpublic function preDestroy():void\n\t\t{\n\t\t\tdispatchEvent(new BeanEvent(BeanEvent.REMOVE_BEAN, gamePresentationModel));\n\n\t\t\tgamePresentationModel = null;\n\t\t}\n\t}\n}\n```\nIn the example above, we create a presentation model for our view and add it to StarlingMVC as a bean. In doing this, the PM will be processed as a bean and gain all of the benefits of DI and EventHandling.\n\nCommand Pattern\n------------\nStarlingMVC includes support for the command pattern. Commands are essentially beans that are created when a specified event is dispatched, executed, and then disposed of. To add a command to the framework, an instance of the Command class should be added to the beans.\n```as3\npackage com.mygame.views\n{\n  \timport com.creativebottle.starlingmvc.StarlingMVC;\n\timport com.creativebottle.starlingmvc.config.StarlingMVCConfig;\n\timport com.creativebottle.starlingmvc.views.ViewManager;\n \timport com.mygame.models.GameModel;\n\n\timport starling.core.Starling;\n\timport starling.display.Sprite;\n\n\tpublic class GameMain extends Sprite\n\t{\n\t\tprivate var starlingMVC:StarlingMVC;\n\n\t\tpublic function GameMain()\n\t\t{\n\t\t\tvar config:StarlingMVCConfig = new StarlingMVCConfig();\n\t\t\tconfig.eventPackages = [\"com.mygame.events\"];\n\t\t\tconfig.viewPackages = [\"com.mygame.views\"];\n\n\t\t\tvar beans:Array = [new GameModel(), new ViewManager(this), new Command(DoSomethingEvent.DO_SOMETHING, DoSomethingCommand)];\n\n\t\t\tstarlingMVC = new StarlingMVC(this, config, beans);\n\t\t}\n\t}\n}\n```\nThis will map the event to the command. The command class can receive all of the same injections as any other bean. It cannot handle events, however, since the instance will only exist long enough to execute a single command. The method to execute in the command instance is noted with the `[Execute]` metadata.\n```as3\npackage com.mygame.commands\n{\n\timport com.mygame.events.NavigationEvent;\n\timport com.mygame.models.BubbleModel;\n\n\timport starling.events.EventDispatcher;\n\n\tpublic class DoSomethingCommand\n\t{\n\t\t[Dispatcher]\n\t\tpublic var dispatcher:EventDispatcher;\n\n\t\t[Inject]\n\t\tpublic var bubbleModel:BubbleModel;\n\n\t\t[Execute]\n\t\tpublic function execute(event:DoSomethingEvent):void\n\t\t{\n\t\t\ttrace(\"did something!\");\n\t\t}\n\t}\n}\n```\nIn this example, whenever the `DoSomethingEvent.DO_SOMETHING` is dispatched, StarlingMVC will create an instance of DoSomethingCommand, run the execute method, and then dispose of the instance. The Command class also contains an optional parameter called runOnce.\n```as3\npackage com.mygame.views\n{\n  \timport com.creativebottle.starlingmvc.StarlingMVC;\n\timport com.creativebottle.starlingmvc.config.StarlingMVCConfig;\n\timport com.creativebottle.starlingmvc.views.ViewManager;\n \timport com.mygame.models.GameModel;\n\n\timport starling.core.Starling;\n\timport starling.display.Sprite;\n\n\tpublic class GameMain extends Sprite\n\t{\n\t\tprivate var starlingMVC:StarlingMVC;\n\n\t\tpublic function GameMain()\n\t\t{\n\t\t\tvar config:StarlingMVCConfig = new StarlingMVCConfig();\n\t\t\tconfig.eventPackages = [\"com.mygame.events\"];\n\t\t\tconfig.viewPackages = [\"com.mygame.views\"];\n\n\t\t\tvar beans:Array = [new GameModel(), new ViewManager(this), new Command(DoSomethingEvent.DO_SOMETHING, DoSomethingCommand, true)];\n\n\t\t\tstarlingMVC = new StarlingMVC(this, config, beans);\n\t\t}\n\t}\n}\n```\nIn this example, the command bean is added with the following line: `new Command(DoSomethingEvent.DO_SOMETHING, DoSomethingCommand, true)`. The optional last parameter, `true`, specifies that this command should be run once. So in this case, when the DO_SOMETHING event is dispatched, the framework would create an instance of DoSomethingCommand, run execute, dispose of the instance, and then remove the mapping. This functionality is very useful for initialization commands.\n\nEventMap\n------------\nEventMap is a utility class for creating and managing event listeners. Using EventMap exclusively to create listeners within your class makes cleanup very easy.\n```as3\npackage com.mygame.mediators\n{\n\timport com.creativebottle.starlingmvc.events.EventMap;\n\n\tpublic class GameMediator\n\t{\n\t\tprivate var eventMap:EventMap = new EventMap();\n\n\t\t[ViewAdded]\n\t\tpublic function viewAdded(view:Game):void\n\t\t{\n\t\t\teventMap.addMap(view.playButton,TouchEvent.TOUCH, playButtonTouched);\n\t\t\teventMap.addMap(view.instructionsButton,TouchEvent.TOUCH, instructionsTouched);\n\t\t}\n\n\t\t[ViewRemoved]\n\t\tpublic function viewRemoved(view:Game):void\n\t\t{\n\t\t\tevent.removeAllMappedEvents();\n\t\t}\n\n\t\tprivate function playButtonTouched(event:TouchEvent):void\n\t\t{\n\n\t\t}\n\n\t\tprivate function instructionsButtonTouched(event:TouchEvent):void\n\t\t{\n\n\t\t}\n\t}\n}\n```\nJuggler\n------------\nThe Juggler class in Starling is used to handle all animation within a game. For a class to take advantage of the Juggler instance, it must implement the IAnimatable interface but defining `advanceTime(time:Number)`. The global juggler reference can be accessed as a static property of Starling as `Starling.juggler1`. However, this property can also be directly injected into your class instances using the `[Juggler]` tag.\n ```as3\n package com.mygame.mediators\n {\n \timport com.creativebottle.starlingmvc.events.EventMap;\n\n \tpublic class GameMediator implements IAnimatable\n \t{\n \t\t[Juggler]\n        public var juggler:Juggler;\n\n \t\t[ViewAdded]\n \t\tpublic function viewAdded(view:Game):void\n \t\t{\n \t\t\tjuggler.add(this);\n \t\t}\n\n \t\t[ViewRemoved]\n \t\tpublic function viewRemoved(view:Game):void\n \t\t{\n \t\t    juggler.remove(this);\n \t\t}\n\n \t\tpublic function advanceTime(time:Number):void\n \t\t{\n            // do some animation logic\n \t\t}\n \t}\n }\n ```\n\nViewManager\n------------\nViewManager is a utility class to facilitate creating views and adding/removing them from the stage. When creating the instance of ViewManager the constructor requires a reference to the root view of the game (i.e. `new ViewManager(this)`) from the root DisplayObject. Adding the ViewManager instance to the StarlingMVC beans makes it easy to swap views from anywhere in the Starling application.\n### setView\nCalls to setView will remove existing views and add the new view. ViewManager handles instantiating the view and adding it to the stack.\n```as3\npackage com.mygame.controllers\n{\n\tpublic class NavigationController\n\t{\n\t\t[Inject]\n\t\tpublic var viewManager:ViewManager;\n\n\t\t[EventHandler(event=\"NavigationEvent.NAVIGATE_TO_VIEW\", properties=\"viewClass\")]\n\t\tpublic function navigateToView(viewClass:Class):void\n\t\t{\n\t\t\tviewManager.setView(viewClass);\n\t\t}\n\t}\n}\n```\n### addView\nCalls to addView will add a new view on top of the existing view. This is handy for popups, HUDs, etc. Whereas setView accepts a parameter of type Class, addView accepts a view instance.\n```as3\npackage com.mygame.views\n{\n\tpublic class Game\n\t{\n\t\t[Inject]\n\t\tpublic var viewManager:ViewManager;\n\n\t\tprivate var hud:GameHUD;\n\n\t\t[PostConstruct]\n\t\tpublic function postConstruct():void\n\t\t{\n\t\t\thud = new GameHUD();\n\n\t\t\tviewManager.addView(hud);\n\t\t}\n\t}\n}\n```\n### removeView\nCalls to removeView will remove the specified view from the stack.\n### removeAll\nCalls to removeAll will remove all views from the stack. This is called automatically when calling `setView()`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCreativeBottle%2FstarlingMVC","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCreativeBottle%2FstarlingMVC","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCreativeBottle%2FstarlingMVC/lists"}