Jump to content

es2015 game referencing between classes


Recommended Posts

Dear people of this awesome community,

I am strugeling to get the spawning system working. I keep getting the error: src\js\objects\Barrier.js:9 Uncaught TypeError: Cannot read property 'width' of undefined(…);

Barrier.js :

import GlobalVars from "./GlobalVars";
import playGame from "../States/PlayGame";

class Barrier extends Phaser.Sprite {
  constructor(game, state, speed, tintColor) {
    this.game = game;
    this.state = state;
    var positions = [(this.game.width - GlobalVars.tunnelWidth) / 2, (this.game.width + GlobalVars.tunnelWidth)/2];
    var position = this.game.rnd.between(0, 1);
    Phaser.Sprite.call(this, game, positions[position], -100, "barrier");
    var cropRect = new Phaser.Rectangle(0, 0, GlobalVars.tunnelWidth /2, 24);
    game.physics.enable(this, Phaser.Physics.ARCADE);
    this.anchor.set(position, 0.5);
    this.tint = tintColor;
    this.body.velocity.y = speed;
    this.placeBarrier = true;

    console.log("barrier placed");

  update() {
    if (this.placeBarrier && this.y > GlobalVars.barrierGap) {
      this.placeBarrier = false;
      playGame.prototype.addBarrier(this.parent, this.tintColor);
    if (this.y > this.game.height) {
export default Barrier;
import GlobalVars from "../objects/GlobalVars";
import Barrier from "../objects/Barrier";

class playGame extends Phaser.State {
  constructor(game) {

  create() {
    console.log("game starts running");
    //setting general color
    this.tintColor = GlobalVars.currentColor;
    //smoke particle effect
    this.barrierGroup = this.add.group();
    this.addBarrier(this.barrierGroup, this.tintColor);
  update() {


  addBarrier(group, tintColor) {
    var barrier = new Barrier(this.game, this, GlobalVars.barrierSpeed, tintColor);
export default playGame;


I am following a book I bought called: create HTML5 vertical endless runner.

can anyone hint a tutorial or give a solution?

Thanks in advance :)

Very Kind regards,


Link to comment
Share on other sites

1 hour ago, drhayes said:

What's calling addBarrier?

EDIT: My guess is that the thing that is calling addBarrier is losing its context, so the value of "this" isn't what you think it is. I bet it's the window. The window doesn't have a game, so neither does your sprite.

in the book it is the prototype itself which is calling the addBarrier method. So I called it inside the class. But I have no idea how to use it or fix it :/ 

Link to comment
Share on other sites

I fixed the issue by passing in the game as a parameter. The problem was that I was referencing the game of the current class which was declared differently. So when I used the right declaration and passed it in as the game parameter then it worked like it should.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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