Jump to content

displayObject.getBounds is not a function when using .overlap


Draxy
 Share

Recommended Posts

I have a two player mini game, with all the main logic happening in this: 

function SpriteDoObj(player){ //  global function

  this.player = player;

  this.atk = function(opponent) {
    this.opponent = opponent;
    if (this.player.overlap(opponent)) {
      //call miss sound here
      console.log("miss");
      return;
    } else if (this.isSameAtkPosition(opponent)) {
      //call block sound
      console.log("block");
      return;
    } else {
      console.log("hit!");
      //call damage sound
      opponent.health --;
    }

  };

} //has been simplified

And I make two instances of SpriteDoObj with:

this.blueSpriteDo = new SpriteDoObj(blue_player);
this.redSpriteDo = new SpriteDoObj(red_player);

//.. also the players are made like this:

  this.players = this.game.add.physicsGroup();

  var red_player = this.players.create(window.innerWidth, this.world.height - 490, 'red_spritesheet');
 
 var blue_player = this.players.create(0, this.world.height - 490, 'blue_spritesheet');

And the atk function is called like this:

      if (blue_keyMap.atk.isDown) {
        this.blueSpriteDo.atk(this.redSpriteDo);
        //the parameter for the atk function
        //passes the opposite player, that becomes the opponent
      }

The error is  phaser.js:47751 Uncaught TypeError: displayObject.getBounds is not a function

And I'm not sure why this is happening, mayhaps something to do with how I'm passing the objects around, but it looks good to me - though I'm a noob.

Any Ideas?

Link to comment
Share on other sites

If this code is arranged like this in the same scope, then that might be the problem.

this.blueSpriteDo = new SpriteDoObj(blue_player);
this.redSpriteDo = new SpriteDoObj(red_player);

//.. also the players are made like this:

  this.players = this.game.add.physicsGroup();

  var red_player = this.players.create(window.innerWidth, this.world.height - 490, 'red_spritesheet');
 
 var blue_player = this.players.create(0, this.world.height - 490, 'blue_spritesheet');

It looks like you are trying to pass blue_player as an argument to SpriteDoObj before it is defined.

Link to comment
Share on other sites

Also, from your code, you seem to think things are a display object when they aren't. Your variable/property names are misleading. When you do

if (blue_keyMap.atk.isDown) {
        this.blueSpriteDo.atk(this.redSpriteDo);
        //the parameter for the atk function
        //passes the opposite player, that becomes the opponent
      }

Passing in this.redSpriteDo is not passing in a display object. It is passing in the object returned by the constructor function SpriteDoObj. Calling SpriteDoObj doesn't create a display object for you. You already have your display object when you call

var red_player = this.players.create( ... );

That is the display object that you should be using, and the display object that the overlap function is expecting. Seeing as you store a reference to the red_player object inside of

function SpriteDoObj(player){ //  global function

  this.player = player;

  ...
}

you should pass in that .player property as the argument for the atk function, like so

this.blueSpriteDo.atk(this.redSpriteDo.player);

 

 

Your problem seems to be caused by kind of messy code.

I would recommend adding the functions you want the players to have (such as atk) onto the player objects themselves. I doubt you need this extra SpriteDoObj wrapper around the actual display objects.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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