Jump to content

communication between classes


Recommended Posts

Hi there, I'll explain a bit what I mean by the title

I have a Game state with two different classes A & B on them. Both A & B are sprite extensions.
A is a completely autonomous being, working with a state machine, and has no idea if any object of the B class exists. B instances are simply items placed statically on the world.

In some of the FSM events, A needs to know where an intance of B is located, so it can move to that location to finish it's current action, all without leaving A's bounds.
In simple words: I need to submit a request from A to B so that B returns an attribute value.
Right now I was trying using Phaser's signals to do so, as I can dispatch the signal from A to make a callback function in B fire but is there any way to retrieve the return value of the callback function?

Right now, a signal from A fires the callback function on B perfectly, but it gives me an undefined value on the console.

A simplyfied code of what I mean:

/* class game */
var Game = function(){

Game.prototype.create = function(){
  this.A = new A();
  this.B = new B();

  this.A.events.someAction.add(this.B.getSomeAttribute, this.B);

/* class A */

var A = function(){
  this.events.someAction = new Phaser.Signal();

A.prototype.constructor = A;

A.prototype.someFunction = function(){
  this.events.someAction.dispatch(); // how to retrieve return value?

/* class B */
var B = function(){
  someAttribute = someValue;

B.prototype.constructor = B;

B.prototype.getSomeAttribute = function(){
   return someAttribute;

Am I understanding anything wrongly? it's there a better way to work around this?

I'm using Phaser 2.6.2. Any help will be much appreciated

Link to comment
Share on other sites

43 minutes ago, FlashRevolution said:

Am I wrong or is someValue nowhere defined? So maybe you could put a 'test' string there to return?

Because if someValue is not defined someAttribute will give you a undefined too.

ah sorry I forgot the this on B's class constructor in the example (my actual code is much more complex so I thought it'd be better to write an example rather than post the code). But someValue is defined.

I did check just in case by printing the return value of the same getter I call during my signal in game (B.getLocation()) in the console, and it does give me the value back. Same goes for the rest of the attributes on the class.

I'll try out with the string though, It would be nice to see if it's something from the signal or the class' code



An static string return does not seem to work either

Link to comment
Share on other sites


  • Recently Browsing   0 members

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