【问题标题】:need suggestion on how to correctly design hierarchy for a game I'm create [closed]需要关于如何为我正在创建的游戏正确设计层次结构的建议[关闭]
【发布时间】:2014-01-08 22:29:46
【问题描述】:

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

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

关于我目前拥有的一些物品。

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

我怎样才能从中构建出良好的层次结构。我的意思是有些对象不会拥有所有这些,有些会。

谢谢

【问题讨论】:

    标签: javascript jquery hierarchy


    【解决方案1】:

    听起来您正在寻找 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;
    }
    

    【讨论】:

    • 好吧,我正在尝试学习继承,但这不是我想要的,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多