需要建议如何正确设计我正在创建的游戏的层次结构

时间:2014-01-08 22:29:47

标签: javascript jquery hierarchy

所以我正在创造一个小游戏(之前从未这样做过),但它引起了我的注意,让我很好奇所以我在这里。

我正在尝试为游戏中的对象设计层次结构。

到目前为止,有些关于物体。

/* 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. 
     * */
}

我怎样才能构建好的层次结构。我的意思是一些物品不会拥有所有这些物品和一些物品。

由于

1 个答案:

答案 0 :(得分:0)

听起来你正在寻找JS没有语法的OOP类继承。有几种方法可以模拟这个(google 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;
}