title: Javascript : Phaser call a method inside another method class
body: <p>I am facing a problem during my development in javascript with Phaser. I try to call a method inside a method of a class but I have an error.</p>
  
  <p>Here is my class :</p>
  
  <pre><code>import Enemy from '../classes/Enemy';
  import Turret from '../classes/Turret';
  import Bullet from '../classes/Bullet';
  
  
  class GameScene extends Phaser.Scene {
  
      constructor() {
          super({
              key: 'GameScene'
          });
      }
  
      preload()
      {
          this.load.image('turret', './www/assets/tour_simple.png');
      }
  
      drawGrid(graphics)
      {
          graphics.lineStyle(1, 0x0000ff, 0.8);
          for(var i = 0; i &lt; 8; i++)
          {
              graphics.moveTo(0, i * 64);
              graphics.lineTo(640, i * 64);
          }
          for(var j = 0; j &lt; 10; j++)
          {
              graphics.moveTo(j * 64, 0);
              graphics.lineTo(j * 64, 512);
          }
          graphics.strokePath();
      }
  
      canPlaceTurret(i, j)
      {
          return this.map[i][j] === 0;
      }
  
      placeTurret(pointer)
      {
          var i = Math.floor(pointer.y/64);
          var j = Math.floor(pointer.x/64);
          if(this.canPlaceTurret(i, j)) {
              var turret = this.turrets.get();
              if (turret)
              {
                  turret.setActive(true);
                  turret.setVisible(true);
                  turret.place(i, j);
              }
          }
      }
  
      damageEnemy(enemy, bullet)
      {
          // only if both enemy and bullet are alive
          if (enemy.active === true &amp;&amp; bullet.active === true) {
              // we remove the bullet right away
              bullet.setActive(false);
              bullet.setVisible(false);
  
              //todo mettre dans une classe constante
              var BULLET_DAMAGE = 25;
              // decrease the enemy hp with BULLET_DAMAGE
              enemy.receiveDamage(BULLET_DAMAGE);
          }
      }
  
  
  
      create() {
          this.add.text(200, 230, 'good!', { fill: '#0f0' });
  
          var graphics = this.add.graphics();
  
          this.map = [[ 0,-1, 0, 0, 0, 0, 0, 0, 0, 0],
              [ 0,-1, 0, 0, 0, 0, 0, 0, 0, 0],
              [ 0,-1,-1,-1,-1,-1,-1,-1, 0, 0],
              [ 0, 0, 0, 0, 0, 0, 0,-1, 0, 0],
              [ 0, 0, 0, 0, 0, 0, 0,-1, 0, 0],
              [ 0, 0, 0, 0, 0, 0, 0,-1, 0, 0],
              [ 0, 0, 0, 0, 0, 0, 0,-1, 0, 0],
              [ 0, 0, 0, 0, 0, 0, 0,-1, 0, 0]];
  
          //TODO creer une classe qui dessine le path
          // the path for our enemies
          // parameters are the start x and y of our path
          this.path = this.add.path(96, -32);
          this.path.lineTo(96, 164);
          this.path.lineTo(480, 164);
          this.path.lineTo(480, 544);
  
          graphics.lineStyle(3, 0xffffff, 1);
          // visualize the path
          this.path.draw(graphics);
  
          this.enemies = this.physics.add.group({ classType: Enemy, runChildUpdate: true });
  
          this.nextEnemy = 0;
  
          var graphics = this.add.graphics();
          this.drawGrid(graphics);
  
          this.turrets = this.add.group({ classType: Turret, runChildUpdate: true });
          this.input.on('pointerdown', this.placeTurret);
  
          this.bullets = this.physics.add.group({ classType: Bullet, runChildUpdate: true });
  
          this.physics.add.overlap(this.enemies, this.bullets, this.damageEnemy);
      }
  
      update(time, delta) {
          if (time &gt; this.nextEnemy)
          {
              var enemy = this.enemies.get();
              console.log(enemy);
              if (enemy)
              {
                  enemy.setActive(true);
                  enemy.setVisible(true);
                  // place the enemy at the start of the path
                  enemy.startOnPath();
  
                  this.nextEnemy = time + 2000;
              }
          }
      }
  
  
  }
  
  export default GameScene;
  </code></pre>
  
  <p>And an error occurs during my game :</p>
  
  <blockquote>
    <p>TypeError: this.canPlaceTurret is not a function</p>
  </blockquote>
  
  <p>The error is from the <code>placeTurret</code> method where I call the <code>canPlaceTurret</code> method.</p>
  
  <p>I have tried some things like adding an attribute to my class : <code>self = this;</code> and call my function with <code>self.canPlaceTurret</code> but there is always the problem. I think it is a scope problem but I don't know how to solve that.</p>
  
  <p><strong>Another important information : we are using Webpack</strong></p>
  
  <p>Thanks for reading :).</p>