Jump to content

Call method inside method


Maria
 Share

Recommended Posts

Hi

 

I know this is most like a total stupid newbie question. I'll ask it anyway :)

 

I had my simple click and drive game for different games with letters as one single js file. Now I am trying to implement it as game states as explained here http://www.emanueleferonato.com/2014/08/28/phaser-tutorial-understanding-phaser-states/

 

My thegame.js starts with the drivingHandler

theGame.prototype = {   create: function(){//start DrivingHandler to get the cars going    this.DrivingHandler(); },

The drivinghandler checks if the click counter is still below winning point

  DrivingHandler: function() {      //loads the next car      if (BasicGame.GameCounter < BasicGame.ClicksToWin) {            //not enough right clicks, still in game          var RandomVehicle = BasicGame.vehiclesList[Math.floor(Math.random() * BasicGame.vehiclesList.length)];          this.driveCar(RandomVehicle);      } else {          //YouWin();          this.game.state.start("GameOver",true,false);      }  },

and if not, it selects a random vehicle and starts the driving function.

 

The driving function lets the car drive across the screen and fires the Itemclicked if, well, the car was clicked and checks if the car is out of game world, in which case it lets another car drive

      //add input listener      Car.inputEnabled = true;         Car.events.onInputDown.add(this.ItemClicked, this);            //check if out of screen      Car.checkWorldBounds = true;      Car.events.onOutOfBounds.add(this.DrivingHandler, '');      //destroy the car if out of World      Car.outOfBoundsKill = true;

When I start the game it is loaded and the car drives to the middle of the screen. But when I click the car, I get an javascript error.

 

Uncaught TypeError: this.driveCar is not a function

 

and it points to the line 

this.driveCar(RandomVehicle);

 from the drivinghandler function.

 

How do I correctly point to the game? I know the error is produced, since in the first call of the drivingHandler this is referring to the game. On the second call in the driving function I call the drivingHandler with this.DrivingHandler, in which case this is referring to the car. But if I change this.DrivingHandler so something else (theGame or without this I get another error)

 

Thanks in advance!

 

Link to comment
Share on other sites

You have to be careful with variables scope in JS. When coding this : 

function functionA(){  this.myVar = 0; // functionA.myVar  this.functionB = function(){     this.myVar = 0; // functionB.myVar!  }}

If you want in functionB to access myVar from functionA, whether it is a var or a function, you can use this trick : 

function functionA(){  this.myVar = 0; // functionA.myVar  var self = this;  this.functionB = function(){     self.myVar = 0; // functionA.myVar!  }}

So be careful where you place your methods. Maybe you could also write a separate object with several useful methods in it and call it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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