tobymcfly Posted May 31, 2018 Share Posted May 31, 2018 Hey guys! I'm making a game based on states. This is my Game.js (1st stage) fil starts with this. var SpaceChicken = SpaceChicken || {}; The problem is, everything is working except 2 functions. I have a function where you can pickUp boxes but the game crashes whenever i pick it up telling me this error: "" The code i've used looks like this: i have tried putting player.addChild(this.game.make.sprite(25, -150, 'box')); but then it says same error, just with this.make is not defined Any solutions or ideas to prevent this? Thanks:) Link to comment Share on other sites More sharing options...
samme Posted May 31, 2018 Share Posted May 31, 2018 You can use player.game.make.sprite(/* ... */) tobymcfly 1 Link to comment Share on other sites More sharing options...
tobymcfly Posted May 31, 2018 Author Share Posted May 31, 2018 oh wow thanks! It worked My other function that doesnt work is this. I cannot do this action where i free a chicken from the cage. it says game is not defined? here is the code! Link to comment Share on other sites More sharing options...
tobymcfly Posted May 31, 2018 Author Share Posted May 31, 2018 it seems like the problem is "Game.add.sprite(...) somehow? Link to comment Share on other sites More sharing options...
samme Posted May 31, 2018 Share Posted May 31, 2018 You may just want to use window.game = new Phaser.Game(/* ... */) That will create a global game variable. Link to comment Share on other sites More sharing options...
tobymcfly Posted May 31, 2018 Author Share Posted May 31, 2018 Well, this it what i would have to replace it with? And then it obviously won't work. It's weird that it says it isnt defined, when i alreadt defined it? But when i take my gaming code out of the state anatomy, it works fine Link to comment Share on other sites More sharing options...
samme Posted June 1, 2018 Share Posted June 1, 2018 If SpaceChicken is a global then you should be able to use SpaceChicken.game wherever you need it. If it's not global, you can add window.SpaceChicken = SpaceChicken; Link to comment Share on other sites More sharing options...
tobymcfly Posted June 1, 2018 Author Share Posted June 1, 2018 Sorry im really, really noob at phaser. Only been working with it for 1 week. How exactly do I know if a variable is global? And if I do add the window.spacechicken, what changes do i have to do in the code, cause it says spacechiken.game undefined. Link to comment Share on other sites More sharing options...
samme Posted June 1, 2018 Share Posted June 1, 2018 console.log(window.SpaceChicken === SpaceChicken); If that's true then SpaceChicken is already a global variable. You don't have to do much different in the other code, you can just use var game = SpaceChicken.game; to get a reference to the game in places where it's not available. Link to comment Share on other sites More sharing options...
tobymcfly Posted June 7, 2018 Author Share Posted June 7, 2018 Im a little confused now. Where do i put the phrase: console.log(window.SpaceChicken === SpaceChicken); ? Link to comment Share on other sites More sharing options...
Recommended Posts