Jump to content

Varible is not defined?


Recommended Posts

Hi guys, as i advanced in my game, i divided it into states, and when i enter to the game state, the browser returns me an error saying that my varibale (dollar) is not defined. The thing is all was working well before i separated it into states. Thats what i have:

var dollars;
var dollar;

var gamestate = {

DollarCheck: function() {
   randomx = Math.random()*208;
   randomy = Math.random()*170;
   dollar = dollars.create(randomx,-18,'dollar');
   player.body.setSize(48, 28, 0, 0);

create: function() {
	dollars = game.add.group();

update: function() {
	player.body.velocity.x = 0;
	dollar.body.velocity.y = velocityy;


This is not all the code, just the dollar containing one, but i cant post the entire code if you want.  As i said when i run it, the browser says me that dollar is undefined in line (the line where is dollar.body.velocity.y = velocityy; ).

Do you see something wrong in the code guys? 

Link to comment
Share on other sites

10 minutes ago, WiLD11 said:

Initialize it in the create function.


This is because dollar depends on dollars, and dollars only gets made after you set dollar. Basically you are setting dollar to... something that doesn't exist.

I dont think so because dollar is inside a group, and that group is inside a function, and i call this function in the create function so all is ready to pass throught the update function.

Btw i solved it, i executed it in another browser, and i've noticed that i have to put a this, before the DollarCheck();

So instead of DollarCheck(); i wrote this.DollarCheck(); And now it works. But thanks anyway mate.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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