Jump to content

Code design question


Meowts
 Share

Recommended Posts

Hey,

 

So I've been reading up on code design patterns in relation to games, and there's one concept I was wondering is even possible using Javascript. I've tried it and it hasn't worked, and I just want to double check that JS is even capable of doing this.

 

Basically it's the idea of reusing one particular component (in this case, function prototype) in multiple instances. In the example I made, it was a dead simple version of this, where a Player object has an Arm, and a CPU_Player object also has an Arm. So for both 'classes', it'd look something like this:

Player = function(game){    this.game = game;    this.arm = null;    this.sprite = null;    this.group = null;}Player.prototype = {    create : function(){        this.sprite = this.game.add.sprite(etc etc);        this.group = this.game.add.group();        this.arm = new Arm(this.game);        this.arm.create(this);    }}//And for the arm...Arm = function(game){    this.game = game;    this.sprite = null;}Arm.prototype = {    create : function(parentObj){        this.sprite = this.game.add.sprite(parentObj.x, parentObj.y, 'key');                //Also wondering what is the best way to handle this...        parentObj.sprite.addChild(this.sprite);        //or...        parentObj.group.add(this.sprite);    }}

... so in languages like C++ or Java this is a pretty standard implementation of aggregation. However when I was trying it out in Javascript, a few bizarre things happened - I swear I'm not messing it up hard somewhere but that's always an option. Regardless, the point I'm wondering is, is this kind of architecture something that's possible using Javascript? Say I end up with 2 Players and 5 CPU_Players and they each have an arm, is it possible to organize my code this way? Or should I be creating multiple instances on a Sprite level instead? (So like, one prototype, creates 5 CPU players as opposed to five prototypes each creating one CPU player)

Link to comment
Share on other sites

Javascript is an incredibly flexible language. I'm sure what you're trying to accomplish is quite possible and probably simple. But, my brain isn't working well today so I'm not sure exactly what you're shooting for and what's going wrong. It sounds like you're trying to favor composition over inheritance. If so, there's a library called stampit I've used in the past which makes this easier.

 

If you're looking for a more classical style of inheritance then Douglas Crockford has a nice approach, although it may be dated a bit. Javascript does some weird stuff with prototype chain where in some cases object properties aren't instance safe, etc but I'd need to read a refresher on that to explain it well. There are a few great books on the topic if its something you're serious about learning. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...