Jump to content

Question about accessing prototypes and such


wichiwichi
 Share

Recommended Posts

Hi there!

 

I'm playing with this example http://gamemechanicexplorer.com/#homingmissiles-5  of the Game Mechanics explorer.

 

So I want to change the direction of every missile. In the example the direction is changed in Missile.prototype.update = function(). I am creating other sprites and I want the missiles to go in that direction. In order to do that I'm trying this code inside the function GameState.prototype.update = function()

    this.missileGroup.forEachAlive(function(m) {        var min = 99999999;        var distance = 0;        var vel_vec = undefined;        this.baddiesGroup.forEachAlive(function(c) {            distance = this.game.math.distance(m.x, m.y, c.x, c.y);            if (distance < min) {                min = distance;                /* this I can do, but it changes position only                m.x = c.x;                m.y = c.y;                */                /* this I cannot do */                m.setDirection(m.x, m.y, c.x, c.y);            }        }, this);           }, this);

Where

Missile.prototype.setDirection = function(x1, y1, x2, y2){/* some code which I cannot access */}

So my question is: why I can't access the setDirection function if I'm having access to the instance of missile?

 

And also, more generally, is this the correct approach? Or should I try to change the direction of missiles inside GameState.prototype.update = function()?

 

And, last question and specifically about Phaser, are these two update functions (Gamestate and Missile) called every frame? Is this some kind of Phaser feature? I read that every frame is called Gamestate update and Gamestate render.

 

Thanks a lot!

Link to comment
Share on other sites

 

So my question is: why I can't access the setDirection function if I'm having access to the instance of missile?

 

You should be able to, post your complete Missile code.
 

And also, more generally, is this the correct approach? Or should I try to change the direction of missiles inside GameState.prototype.update = function()?

 

The approach you are using is fine.

 

And, last question and specifically about Phaser, are these two update functions (Gamestate and Missile) called every frame? Is this some kind of Phaser feature? I read that every frame is called Gamestate update and Gamestate render.

 

Phaser calls a number of functions every frame, besides the state functions that you mentioned, Missile has update called every frame because it inherits from Sprite, Phaser will call Sprites update function every frame.

Link to comment
Share on other sites

Oh my, I spent the whole night trying and today did some refactoring and now somehow it's working. Thanks!

 

So one more question, do you think it's optimum the two nested forEachAlive? Or should I calculate this once every 10 frames or so?

 

Thanks again

Link to comment
Share on other sites

Optimal? No, but it is almost certainly good enough for what you are trying to do, it will only become a problem at a high number of baddies.

 

If you want to optimize it (which I don't necessarily think you should) you can look into spatial partitioning to reduce the number of baddies you will have to loop through. Phaser has one built in option you can use called QuadTree.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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