Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript为我的游戏OOP创建对象_Javascript_Oop - Fatal编程技术网

javascript为我的游戏OOP创建对象

javascript为我的游戏OOP创建对象,javascript,oop,Javascript,Oop,我正在寻找一些示例或示例代码,以便正确组织我的游戏代码 我有一个游戏: var gameStructure = function () { this.playerLife = 100; } 如何创建新的游戏实例(因为将要进行多个游戏?) 我如何格式化游戏动作的代码 var attackPlayer = function (damage) { this.playerLife = this.playerLife - damage; } gameInstance.attackPlaye

我正在寻找一些示例或示例代码,以便正确组织我的游戏代码

我有一个游戏:

var gameStructure = function () {
  this.playerLife = 100;
}
如何创建新的游戏实例(因为将要进行多个游戏?)

我如何格式化游戏动作的代码

var attackPlayer = function (damage) {
    this.playerLife = this.playerLife - damage;
}

gameInstance.attackPlayer(50);
这不是真正的代码,我相信这是完全错误的,根本不是你应该如何编写javascript代码。有一件事我很困惑,那就是如何创建多个游戏实例。我不知道如何将变量设置为变量

我的意思是我需要:

var gameInstance1
gameInstance2
等等,取决于玩家的数量

现在我实际上是在数组中存储游戏实例

因此,我:

var gameInstances = [], gameid

var createNewGame = function () {
  gameInstances.push(gameInstanceName);
  gameid = gameInstances.indexOf(gameInstanceName); 
}
然后我通过gameInstances[gameid]引用我的游戏实例对象

是否可以这样做,或者这是不可取的,我应该使用OOP和带有new关键字的实例化


谢谢,请多多指教

JavaScript中的OOP等价物是将
attackPlayer
函数附加到
gameStructure
prototype

gameStructure.prototype.attackPlayer = function (damage) {
    this.playerLife = this.playerLife - damage;
};
gameStructure
的所有实例将继承
attackPlayer
函数,并且
将正确引用该实例

您的
createNewGame
函数只需调用
newGameStructure()
并将结果推送到数组中即可。
gameid
只是数组中的索引,可以从函数返回:

var gameInstances = [];

var createNewGame = function () {
    gameInstaces.push(new gameStructure());
    return gameInstances.length - 1;
};

使用上述代码,用法如下:

var gameId = createNewGame();
gameInstances[gameId].attackPlayer(100);

在Javascript中,您可以使用类似函数的对象:

function Vector2(x, y) // Constructor
{
    // Member variable
    this.x = x;
    this.y = y;

    Vector2.count++;

    // Member function
    this.length = function ()
    {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }
}
// Static members
Vector2.count = 0;

// Static Functions
Vector2.add = function (a, b)
{
    // Instantiation
    return new Vector2(a.x + b.x, a.y + b.y);
}


function Point(x, y)
{
    this.x = x;
    this.y = y;
}

// Single Inheritance
Point.prototype = new Vector2();

Point.distanceBetween = function (a, b)
{
    var diff = new Vector2(b.x - a.x, b.y - a.y);
    return diff.length();
}

Javascript中OOP最奇怪的地方是它本身的函数就是构造函数。我希望这能有所帮助。

你能详细说明你的答案吗?我如何使用attackPlayer功能?谢谢。@FriSource,我在回答中加了一个例子。
function Vector2(x, y) // Constructor
{
    // Member variable
    this.x = x;
    this.y = y;

    Vector2.count++;

    // Member function
    this.length = function ()
    {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }
}
// Static members
Vector2.count = 0;

// Static Functions
Vector2.add = function (a, b)
{
    // Instantiation
    return new Vector2(a.x + b.x, a.y + b.y);
}


function Point(x, y)
{
    this.x = x;
    this.y = y;
}

// Single Inheritance
Point.prototype = new Vector2();

Point.distanceBetween = function (a, b)
{
    var diff = new Vector2(b.x - a.x, b.y - a.y);
    return diff.length();
}