2

So I'm a flash game developer trying to make the transition from AS2 to AS3+OOP, only 3 years or so after everybody else did. There's a plethora of free, very helpful material to go through and I've been spending 2-3 days wrapping my head around some of it, but by now I feel like I just want to start and try it out and learn the harder stuff as I go (just like I did with AS2).

I get the benefits of (most aspects of) OOP and I was once forced to learn to do a couple of games in Processing which had me write a few classes and I really liked the whole inheritance thing, which is the biggest reason I'm keen to move on, I think.

I just wanted to ask about game structure - is my current way of AS2 programming (see the example below, with some pseudo code) close to the way you'd organize things in OOP, or are there some big flaws in my game logic/structure that I can't see? The way I understand I would have to do differently in AS3/OOP is to have a class for moving stuff such as the player, hero, missiles etc, then have an enemy class that extends that class, then have classes for each enemy that extends the enemy class, unlike now where each "class" is instead an object and a function called from the main game loop, and sub-classes are instead "if"-clauses in each function. But except for that, is my programming style on the right track or do I need to re-think the logic behind my code for it to work effectively in an AS3/OOP setting?

Any advice will be much appreciated!

function initializeF() {
    initializeVariablesF();
    startGameF();
}

function initializeVariablesF() {
    enemyO = new Object(); //which will contain each enemy instance
    enemyA = new Array(); //which will be a list of all the enemies, maybe superfluous?
    playerShotsA=new Array();
    //and so on...
    enemyDataO = new Object();
    enemyDataO.goblin = new Object();
    //and then some vars relating to the goblin, sort of a class without methods, right?
}

function startGameF() {
    this.onEnterFrame = function() { //my game loop
        checkKeysF(); //checks which keys are pressed, saves it to a global object
        playerMovementF(); //moves the player about depending on which keys are pressed
        playerShotsF(); //deals with the missiles/shots/lasers the player has shot
        enemyCreatorF(); //decides when to create a new enemy
        enemyF(); //deals with all enemies in the enemyA
        enemyShotsF(); //deals with the missiles/etc the enemies have created
    };
}

function enemyCreatorF(){
    //randomly creates an enemy through a "factory" function:
    if (random(20)==1){
        attachEnemyF(enemyDataO.goblin, ...and some values like position etc)
    }
}
function attachEnemyF(a_enemyType, ... a bunch of values like position){
    //create a new enemy object
    var enemy=enemyO[new unique enemy name]
    enemy.enemyType=a_enemyType
    enemy.clip=attachMovie(a_enemyType,"clip", [values like x and y passed on])
    enemyA.push(enemy) 
}
function playerShotsF(){
    for (every shot in playerShotsA){
        //move it about
        for (every enemy in enemyA){
            //if it hits an enemy, add damage to the enemy
        }
    }
}

function enemyF() {
    for (every enemy in enemyA) {
        //check if it's dead/less health than zero, if so remove it from the array, remove it's clip, null it's object
        //if it's not, move it about, maybe have it shoot
        //if it touches the player, decrease the player's health
        //different behavior depending on the enemy's type by "if" or "switch" statements
    }
}
Marcus
  • 136
  • 2
  • 8
  • Make an enemy class that handles anything pertaining to one single enemy. Also make an enemies class that handles the functions and tracking of the enemy classes – The_asMan Oct 20 '11 at 17:43

1 Answers1

1

I'm not sure the question is a good fit for SO as it's mostly asking for opinions, but anyway:

  1. Having a base class with basic functions such as "move", "hitTest", "render", etc. is indeed what you should do. Then have the player's avatar, enemies, etc. inherit from this class.

  2. The F suffix (and even O and A suffixes) are quite superfluous, since any good AS3 editor will already tell you whether the class member is a function, or an array, object, etc.

  3. You don't need to store your enemies in both an array and an object, it's going to make it unnecessary complicated to remove or add an enemy. Instead, just store them all in an array and use simple loops to inspect their properties.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • When working with objects I prefer to store them in an ArrayCollection as that gives you some better functions. – The_asMan Oct 20 '11 at 17:27