2014-01-08 38 views
0

因此,我正在創建一個小遊戲(以前從未這樣做過),但它引起了我的注意並讓我好奇,所以在這裏。需要關於如何正確設計遊戲的層次結構的建議我創建

我想設計遊戲內物體的層次結構。

一些關於我到目前爲止的對象。

/* this is the basic object - every object in game will have that */ 
var fnObject = function (node){ 
    this.nNode = node || ''; 
    this.id = ''; 
    this.sType = 'pc'/*pc,npc,physics,obstacle*/; 
    this.oPosition = { 
     marginTop : 0, 
     marginLeft : 0 
    }; 
    /* 
    * and more properties. 
    * */ 
} 
/* not all objects will have those */ 
var fnPhysics = function(){ 
    this.iFriction = ''; 
    this.iAcceleration = ''; 
    this.iGravity = ''; 
    this.iWind = ''; 
    this.iIncline = ''; 
    this.iSpeed = 1; 
    this.iMoveDistant = 5; 
    /* 
    * and more properties. 
    * */ 
} 

/* Only objects that can move will have those */ 
var fnControls = function(){ 
    this.fnGetMvDist = function(){ 
     //.. 
    } 
    this.fnDoMove = function(){ 
     //.. 
    }; 
    this.fnMoveRight = function(){ 
     //.. 
    } 
} 

/* not all objects will have those */ 
var fnStats = function(){ 
    this.hp = 100; 
    this.manaLeft = 100; 
    this.livesLeft = 5; 
    /* 
    * and more properties. 
    * */ 
} 

我怎樣才能構建出良好的層次結構。我的意思是有些物體不會有所有這些和一些將。

感謝

回答

0

這聽起來像你正在尋找OOP類繼承其JS沒有語法。有幾種方法在這裏模擬這種(谷歌JS OOP)只是其中的一個方法去實現它:

var fnPhysics = function(){ 
    var fnObjectInstance = new fnObject(); 
    fnObjectInstance.iFriction = ''; 
    fnObjectInstance.iAcceleration = ''; 
    fnObjectInstance.iGravity = ''; 
    fnObjectInstance.iWind = ''; 
    fnObjectInstance.iIncline = ''; 
    fnObjectInstance.iSpeed = 1; 
    fnObjectInstance.iMoveDistant = 5; 
    return fnObjectInstance; 
} 
+0

好吧,我想學習繼承,而這樣做,但是這個心不是我所期待的感謝。 –