2011-10-20 23 views
2

所以我是一個flash遊戲開發者,試圖從AS2過渡到AS3 + OOP,在大家都做了3年左右之後。有很多免費的,非常有用的材料可以通過,我已經花了2-3天的時間來圍繞它的一些內容,但現在我覺得我只是想開始嘗試並學習更​​難的東西隨我去(就像我用AS2做的一樣)。我的班級AS2程序設計精神接近OOP嗎?

我從OOP的(大部分方面)中受益,我曾經被迫學習在Processing中做幾個遊戲,這讓我寫了幾個類,我非常喜歡整個繼承的東西,這是最大的我想,我渴望繼續前進。

我只是想問一下關於遊戲結構 - 我目前的AS2編程方式(參見下面的例子,帶有一些僞代碼)接近於你在OOP中組織事物的方式,或者有一些大的缺陷我無法看到的遊戲邏輯/結構?我的理解是,AS3/OOP必須採取不同的行動,例如玩家,英雄,導彈等移動物體,然後有一個敵人級別擴展這個級別,然後爲每個擴展的敵人敵人階級,與現在不同的是,每個「類」代替了主遊戲循環中調用的對象和函數,子類在每個函數中代替「if」條。但除此之外,我的編程風格是否正確,還是需要重新思考我的代碼背後的邏輯,才能在AS3/OOP環境中有效地工作?

任何意見將不勝感激!

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 
    } 
} 
+0

使敵人職業能夠處理與單個敵人有關的任何事物。還可以創建一個處理敵人類的功能和跟蹤的敵人類 –

回答

1

我不知道這個問題是一個非常適合這樣它的大部分要求的意見,但無論如何:

  1. 具有基本功能,如「移動」一個基類, 「hitTest」,「渲染」等的確是你應該做的。然後讓玩家的頭像,敵人等繼承這個類。

  2. 在F後綴(甚至是O和一個後綴)是非常多餘的,因爲任何好的AS3編輯器將已經告訴你了類成員是否是一個函數或數組,對象等

  3. 你不需要將你的敵人存放在一個陣列和一個物體中,這將使得移除或增加一個敵人變得不復雜。相反,只需將它們全部存儲在一個數組中,並使用簡單的循環來檢查它們的屬性。

+0

使用對象時,我更願意將它們存儲在ArrayCollection中,因爲這會爲您提供更好的功能。 –