Reputation: 1738
I now use starling framework to do a game in flash. However I am new to architecture in game and I think what I have done here in my game is not really good.
I have a Screen class which is used to display the content to stage.
public class Screen {
private var button : Button
private var controller : Controller
public function Screen(){
controller = new Controller(button)
}
}
public class Button{
private var controller : Controller
private var button: Button
public function Button(){
button.addEventListener(Event.TRIGGERED, onTrigger)
}
private function onTrigger(e:Event){
controller.notify(buttonTriggered);
}
}
//in the controller class, I have a list of controller which controls other components
//those are added to Screen class (character, ...)
public class Controller{
public function Controller(button){
}
public function notify(event){
switch(event){
//notify to other controller with this event
}
}
}
Do you have any suggestions for this architecture. Thank you very much for all of your feedbacks.
Upvotes: 0
Views: 358
Reputation: 32221
I create a great solution for screens in gazman-sdk.com.
When you think about screens mechanism what are it's main tasks:
on resume
when screen getting to the top of the stackon pause
when screen is no longer on top of the stackon create
when screen is created and dispose
when screen is destroyed.opneScreen(new GameScreen());
. Because the class who calling this method know who GameScreen
is and you want to avoid this.I build a solution using Multiton pattern, it has multi-representation for screens stack model, that is used both for popups, and screens. Each screen is encapsulated and starts and closes by listening to system events.
Unfortunately I did not created a full tutorial for that yet. When I do it will be published here. How ever I did a running example of this mechanism in my Component Explorer project. You can find it's source code on github. And live example at here.
I am constantly active on this site so if you choose to implement it in your project I be able to help you with any questions you will have on the way.
Cheers and good luck.
Upvotes: 1
Reputation: 178
regarding your button class, i assume you want a button that can be clicked with the mouse. if that is the case, you should change Event.TRIGGERED to MouseEvent.CLICK, and the e:Event parameter in onTrigger to e:MouseEvent. Then the function will trigger whenever you click the button.
Upvotes: 0