所以我是一個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
}
}
使敵人職業能夠處理與單個敵人有關的任何事物。還可以創建一個處理敵人類的功能和跟蹤的敵人類 –