AramCP Posted December 25, 2016 Share Posted December 25, 2016 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); game.physics.arcade.enable(dollars); }, create: function() { dollars = game.add.group(); DollarCheck(); }, 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 More sharing options...
WiLD11 Posted December 25, 2016 Share Posted December 25, 2016 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. Link to comment Share on other sites More sharing options...
AramCP Posted December 25, 2016 Author Share Posted December 25, 2016 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. WiLD11 1 Link to comment Share on other sites More sharing options...
Recommended Posts