简体   繁体   English

Javascript对象交互(OOP)

[英]Javascript object interaction (OOP)

It's hard to get the exact specific information on OOP you're searching for. 很难获得要搜索的有关OOP的确切特定信息。 I tried to keep it as short as possible: 我试图使它尽可能短:

I'm currently developing a jump n run in HTML5. 我目前正在开发HTML5中运行的JumpN。 I have actually no real experience with developing games. 我实际上没有开发游戏的实际经验。 But I know how the basics are supposed to work. 但是我知道基本原理应该如何工作。 I just want to know if I'm doing it right. 我只想知道我是否做对了。

I have a game, player and level object. 我有一个游戏,一个玩家和一个关卡对象。

What I'm currently doing is the following: 我当前正在执行以下操作:

Game.player = new Player();
Game.level = new Level();

Game.level.load(Game.currentLevel);
...
Game.run();

Is that the best way or should I call them all on their own, eg: 那是最好的方法还是我应该一个个称呼它们,例如:

var Player = new Player();
var Level = new Level();
Level.load(Game.currentLevel);
...
Game.run();

They way I'm doing it right now (the first one) seems more logic to me. 他们认为我现在(第一个)正在这样做,这对我来说似乎更合逻辑。 But.. in the level objects functions I have to check for various variables from the game object or call a function of its self. 但是..在关卡对象函数中,我必须检查游戏对象的各种变量或调用其自身的函数。 Thus I have to write Game.level.funcName inside the Level objects functions. 因此,我必须在Level对象函数中编写Game.level.funcName。 But since Game.level doesnt actually exist at the level objects declaration point it feels kinda wrong and dirty. 但是,由于Game.level实际上并不存在于关卡对象声明点处,因此感觉有些错误和肮脏。 Here is another example: 这是另一个示例:

Level.prototype.reset = function() {
    this.load(Game.currentLevel);
};

The Game.currentLevel is hardcoded, isn't there any better way to detect which variable currently handles the game object, or is it totally ok the way I'm doing it ? Game.currentLevel是硬编码的,没有更好的方法来检测当前哪个变量处理游戏对象,或者我这样做的方式完全可以吗?

So the basic question is, whats the best way to let objects interact with each other ? 因此,基本问题是,使对象彼此交互的最佳方法是什么?

And one last question which is kinda offtopic, but what does ()(); 最后一个问题有点离题,但是()()是什么呢? do? 做? I sometimes see it beeing used like this: 我有时会看到它像这样被使用:

(function() {
    // Content
});

I hope you understand my concerns, thanks for your time and answers. 希望您能理解我的担忧,感谢您的宝贵时间和答复。 :) :)

I would recommend the first approach, because it's more modular. 我建议第一种方法,因为它更具模块化。
Your problem can be solved by simply passing a reference of the Game instance to the other components, so that they are aware of the game. 您可以通过简单地将Game实例的引用传递给其他组件来解决您的问题,从而使其他组件知道游戏。
It's not uncommon for objects to have a cyclic structure in javascript: 对象在javascript中具有循环结构的情况并不少见:

Game.level = new Level();
Game.level._game = Game;
//...
Level.prototype.reset = function() {
    this.load(this._game.currentLevel);
};

Of course that you can do it a bit more elegant by passing reference at initialization, but I think you got my point. 当然,您可以通过在初始化时传递引用来做到这一点,但是我认为您的意思是正确的。

I have answer to last question 我有最后一个问题的答案

Question: what does ()(); 问题:()()是什么? do? 做? I sometimes see it beeing used like this: 我有时会看到它像这样被使用:

(function() {
    // Content
});

It is the self executing closure . 这是自我执行的闭包 I will provide simplest explanation here. 我将在这里提供最简单的解释。 When we write java script function they need to be called to execute them. 当我们编写Java脚本函数时,需要调用它们来执行它们。

For example, 例如,

function alertMe(){
 alert('alerted');
}

alertMe(); // We need to call intentionally for execution of function.

Self executing closure doesn't require calling separately. 自执行闭包不需要单独调用。

For example, 例如,

(function(){
 alert('alerted');
})();

Above javascript executes automatically, when script is loaded. 脚本加载后,上面的javascript自动执行。 Same Question is answered on SO here . 这里,同样的问题也被回答。

I think the way you're doing things look pretty good. 我认为您的处事方式看起来不错。 About the last part of your question, that's called an immediate function. 关于问题的最后一部分,这称为立即函数。 It's a function that's called right after it's declared. 该函数在声明后立即被调用。 You can see more info about here: http://javascriptmountain.com/2011/06/functions/immediate-functions-in-javascript-the-basics/ 您可以在此处查看更多信息: http : //javascriptmountain.com/2011/06/functions/immediate-functions-in-javascript-the-basics/

Start with the user interaction and work backwards. 从用户交互开始,然后向后工作。 It's possible to get too much involved in the design process and come up with convoluted designs if that design is too flexible or is solving too many problems. 如果设计过于灵活或解决了太多问题,那么可能会过多地参与设计过程并提出复杂的设计。

Based on my limited knowledge of games, and even lesser of game programming, and what you've shown us, I believe there are two user interactions that you're dealing with here. 基于我对游戏的有限了解,甚至更少的游戏编程知识,以及您向我们展示的内容,我相信您在这里要处理两种用户交互。

  1. User picks a particular game level to play 用户选择要玩的特定游戏级别
  2. User resets that game level 用户重置该游戏级别

Storing the current level as a property of the game object is perfectly fine. 将当前关卡存储为游戏对象的属性非常好。 I can think of two methods that would be needed to handle these interactions both of which would make sense on a Game object. 我可以想到处理这些交互所需的两种方法,这两种方法在Game对象上都是有意义的。

function setLevel(levelNumber) {
   this.currentLevelNumber = levelNumber;
   this.level = new Level(levelNumber);
}

function resetCurrentLevel() {
   this.setLevel(this.currentLevelNumber);
}

I would break the connection from a Level object to the Game object, and load a level independently of the game as much as possible. 我会断开从Level对象到Game对象的连接,并尽可能独立于游戏加载一个关卡。 So instead of doing, 所以不要做

game.level = new Level();
game.level.load(game.currentLevel);

I'd push the burden of initializing a level upon the Level constructor itself as in, 我会像这样在Level构造函数上加重初始化一个级别的负担,

game.level = new Level(8);

or even better, make it a method call on the Game object as in my example above - setLevel(n) . 甚至更好的是,使其成为对Game对象的方法调用,如我上面的示例setLevel(n) The method will be responsible for ensuring that the game object is consistent when the level changes. 该方法将负责确保级别更改时游戏对象的一致性。

game.setLevel(8);

Since the reset function resetCurrentLevel internally uses this method, handling of level changes will be unified whether it's loading a new level, or resetting a current level. 由于reset函数resetCurrentLevel内部使用此方法,因此无论是加载新级别还是重置当前级别,都将统一级别更改的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM