ashes999 Posted September 24, 2014 Share Posted September 24, 2014 Hi, Caveat: I'm new to Phaser, new to CoffeeScript, and fairly new (< 6 months) to Javascript. I've written a small-ish game in JS + CraftyJS in the past. I'm trying to convert the "hello world" Phaser app into its equivalent in CoffeeScript. Here's what I have working:class State constructor: () -> preload: () -> game.load.image('logo', 'phaser.png') create: () -> logo = game.add.sprite(game.world.centerX, game.world.centerY, 'logo') logo.anchor.setTo(0.5, 0.5)window.onload = () -> @game = new Phaser.Game(800, 600, Phaser.AUTO, '', new State)I've looked at the equivalent generated JS, but I can't quite figure out why this works; specifically, @game = new Phaser.Game(...) confuses me. TLDR: why do I need to assign the value to @game and not just game? Observations: window.game takes the value of the game variableThe State class has no references to game, or window, that I can seeChanging @game to game breaks the game. Can anyone explain this? It doesn't seem like it has to do with closures, or global variables, or anything else I can think of. Why does this work? Link to comment Share on other sites More sharing options...
ashes999 Posted September 29, 2014 Author Share Posted September 29, 2014 (Six days later) Anyone? Link to comment Share on other sites More sharing options...
xerver Posted September 29, 2014 Share Posted September 29, 2014 I'm not sure what you are confused about, you pass an object with the preload/create methods on it, and phaser calls those methods at the appropriate time. What is the confusing part? Link to comment Share on other sites More sharing options...
ashes999 Posted September 30, 2014 Author Share Posted September 30, 2014 Question updated. It's not the game state, but the assignment to "game" vs. "@game". I looked at the generated JS (obviously?) and some other stuff (see initial thoughts) -- I think it has something to do with CoffeeScript putting everything in an anonymous lambda (and thus limiting scope), but I can't quite tell what the difference is and what @game belongs to. Link to comment Share on other sites More sharing options...
chg Posted September 30, 2014 Share Posted September 30, 2014 Question updated. It's not the game state, but the assignment to "game" vs. "@game". I looked at the generated JS (obviously?) and some other stuff (see initial thoughts) -- I think it has something to do with CoffeeScript putting everything in an anonymous lambda (and thus limiting scope), but I can't quite tell what the difference is and what @game belongs to.Caveat: Well I don't know CoffeeScript at all, and got nowhere with Phaser... but I think you just answered your own question. Surely if you store the game object on a non-static variable that is local to a function and don't return it or anything that references it, then you can expect it to be disposed of when that function returns (or at least not long after) P.S. after writing the above I spend about 10 seconds to google "coffeescript at symbol", it seems it somewhat equivalent to "this" in javascript at least in regard to class members. Link to comment Share on other sites More sharing options...
ashes999 Posted October 8, 2014 Author Share Posted October 8, 2014 The answer is that the variable gets bound to window.game, which is appropriately global. Link to comment Share on other sites More sharing options...
Recommended Posts