views:

1197

answers:

4

Game description: A quiz with different levels and different types of visual questions for each level.
OOP so far: GameBoard (where one answers questions), Dialog, HighScore, Controller/LevelController?

I have a document class called Controller which initializes the game:

public function Controller() 
{
    initGame();
}    

function initGame() 
{
    xmlGameData = levels, questions xml
    highscore:HighScore = new HighScore();
    startGame();
}

function startGame() 
{
    levelController = new LevelController( this, xmlGameData );
}

Then I'm starting to pass around a reference to the "Document class / main timeline" to my different object which in themselves extends MovieClip or Sprite.

public function LevelController(docClass, xml)
{
   mainTimeLine = docClass;
   xmlGameData = xml;

   loadGameBoard(); 
   nextLevel();
} 

function loadGameBoard()
{
   gameBoard = new GameBoard(mainTimeLine, this);
   mainTimeLine.addChild(gameBoard);
}

This gets pretty messy after some time and I'd like to know better ways of controlling the different objects (like GameBoard and HighScore) and states (Levels) and actions (AnswerQuestion, NextQuestion etc.) from, maybe, a single point of control.

+1  A: 

umm... I'm assuming your question is "What is a better structure for managing a game than what I have here" Though that's not explicitly stated that's what I'll try to answer

I don't think polymorphism is the best solution for what your doing. OOP is more than just a series of objects. It's grouping the functionality into useful objects.

I would suggust moving more functionality into the Controller

1.) a main object with all the following functionality that will be present 
    throuhout the game
    a.) go to level (next/previous)
    b.) keep track of score
    c.) controll the state of the game
    d.) anything else that exists throughout the game

2.) level objects that handle level specific information
    a.) interactivity of the questions such as buttons etc.
    b.) managing the correct answer and the impact it has on the score in main
Fire Crow
+3  A: 

What I've taken to doing for controlling the 'UI' and 'Support' functions of my games (screen flow, score submission, level advancement) is to place a series of static functions in a main game controller class that handles all the logic for showing/hiding game screens, advancing an iterator through a levelData array and handling all the server side submits from one central location.

It's basically a class with a series of static functions such as:

public static function LoadUserData() {}
public static function SaveUserData() {}
public static function PlayNextLevel() {}
public static function SubmitScore() {}
public static function ShowLevelPlayScreen() {}
public static function ShowLevelPauseScreen() {}
public static function ShowGameOverScreen() {}

etc...

There is only ever going to be one of these controllers, so packing in static functions does the trick, keeps it easy to access from anywhere in the code, and is syntactically cleaner to call than making it into a Singleton.

JStriedl
this is a mistake made very often. no offense, but when it comes to OOP, this approach disobeys about every of its principles. this is one good post on the subject: http://tinyurl.com/256wxd . other than that, feel free to google "singletons are evil". and and using classes as global objects is even worse, since singletons at least allow reuse through polymorphism.
back2dos
back2dos - I have to disagree with some of the points of that article. As programmers we should always strive to do the simplest thing first. Why over engineer something, create an interface, factory, etc if it might never be used.
Aaron M
@back2dos <- In enterprise programming, sure, you are correct. However, in a casual flash game programming where you typically only touch 80% of the code once, doing the simplest thing that could possibly work makes a lot more sense.@Aaron M <- that's exactly my thought process.
JStriedl
@JStriedl - depends on the client. It might be a simple flash game but then the client wants changes, extra features. If you have poor architecture to start with you can shoot yourself in the foot later down the track. I am not a fan of design patterns but sticking to some basic OOP principles is a must for me based on my experiences. Also in this case static functions make it harder for debugging since they can be called from anywhere.
Allan
@Allan - of course. I don't mean to imply that because I use the simplest solution that my code is sloppy. I re-use a good chunk of custom written classes between projects and make the items that should be configurable...configurable. The core engine is a good candidate for including a simple collection of static utility functions though, as it's unlikely to need to be re-purposed dramatically during the course of development, as long as your up front design was at least mostly on the mark.
JStriedl
+1  A: 

Here's how I do it.

Since this is Flash, I make my game designer friendly. That means I instantiate object per frame. I know people will downvote me, but I know a lot of designers who thanked me for this (They didn't have to modify any code while they screwed around with the graphics).

Basically, make every visible object (that can be 'skinned') subclass a MovieClip. Have it communicate through custom events, to where it would be usually at - such as the 'board' (which is also a MovieClip).

Here is a code example of a simple Word Search game that will help you better understand of what I am talking about.

LiraNuna
hey. i can't open the .fla (guess it's CS4), so i can't fully judge. it seems to me, this a very pragmatic and efficient way to develop design-oriented games. OTOH, i think this approach is not very suitable for big/extensible games, that need an awful load of code, and where graphics actually are mere assets. the biggest problem i see, is that a lot is determined by what is in the .fla, which is a proprietary binary format. not good for tooling, version control, testing etc.
back2dos
A: 

Fire Crow has the right idea. What you want to do is to separate what changes in your game from the game itself. So your Controller object will contain game functionality that applies to all levels, such as the score, the display sprite, GUI info, and an array of levels with a reference to the current level. Your game consists of a series of levels, so they should naturally be represented by Level objects. Each level knows how to draw itself and handle inputs from the player. The level object can inform the Controller when it is complete, and the Controller can then display the next level in the array.

Pedro Estrada