themyth92
themyth92

Reputation: 1738

Game architecture in AS3

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

Answers (2)

Ilya Gazman
Ilya Gazman

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:

  • Managing a screens stack
  • Informing the screens with stack changes:
    • call on resume when screen getting to the top of the stack
    • call on pause when screen is no longer on top of the stack
    • And call on create when screen is created and dispose when screen is destroyed.
  • Also you want to encapsulate the screen logic inside the screen some how. So your code will not have lines like: opneScreen(new GameScreen());. Because the class who calling this method know who GameScreen is and you want to avoid this.
  • And finally when your screens mechanism is up and running you don't want to write another identical mechanism for popups as it's pretty much the same.

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

haag
haag

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

Related Questions