简体   繁体   中英

Javascript object interaction (OOP)

It's hard to get the exact specific information on OOP you're searching for. I tried to keep it as short as possible:

I'm currently developing a jump n run in HTML5. 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. But since Game.level doesnt actually exist at the level objects declaration point it feels kinda wrong and dirty. 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 ?

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.
It's not uncommon for objects to have a cyclic structure in 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.

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

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.

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. 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,

game.level = new Level(8);

or even better, make it a method call on the Game object as in my example above - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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