Zampano Posted January 27, 2017 Share Posted January 27, 2017 Hi there! I'm new around here, and I'm quite new to JS and especially Phaser. I did a little bit of AS3 a few years back but I'm still pretty rusty, so right now I'm not really able to pull off what I have in mind. I hope you guys can help me out a bit! So, first things first, I am using a multi-state, multi-file-structure to keep it clean. I have succeeded in writing two classes which both have their own file, expanding Sprite and Group. This here is the constructor for the Class that uses Group, nothing fancy. Board = function (game) { Phaser.Group.call(this, game); }; Board.prototype = Object.create(Phaser.Group.prototype); Board.prototype.constructor = Board; Board.prototype.update = function() { }; 1. My first Problem is this: In this class, I have a function that is supposed to add Sprites to the Group in a specific manner: Board.prototype.build = function(Board_plan) { for (var i=0; i < 3; i++) { for (var ii=0; ii < Board_plan[1].length; ii++) { sprite = this.create(ii*100, i*140, 'board_blank'); } } }; In my preloader state, I load board_blank, and I'm able to use it in my main State without problems. When I create a new "Board" and then use this function, I get "Uncaught TypeError: Cannot read property 'getImage' of undefined" though. If I don't give a Sprite to the function and let it use the default, it works like a charm. I tried to retrieve the Sprite via cache (providing game to the function, then game.cache.getImage), didn't work either. So, how can I access my Sprite key from within extzernal classes or make them global or something? 2. Here's another one: The Sprite Class I mentioned earlier is meant for a certain type of game objects that I want to keep throughout my states. It carries a number of variables with specific values for each instance I'm creating. A bit like characters with different stats, which can be changed at any time and should therefore not be cleared ever. I understand that I have to instanciate them as global, which works so far. The problem is, if I add them to existing in one state, then switch to the next, my global variable is empty, I suppose because all sprites are cleared at the end of a state? So how would I solve this? Would It maybe be a better Idea to not expand Sprite, but also Group instead and then just add new instances of sprites on demand instead of relying on a single "base sprite"? There's more, but I think that's enough for now I appreciate for any help! Thank you! Link to comment Share on other sites More sharing options...
Zampano Posted January 28, 2017 Author Share Posted January 28, 2017 Anyone? D: Link to comment Share on other sites More sharing options...
samme Posted January 28, 2017 Share Posted January 28, 2017 (1) Cannot read property 'getImage' of undefined isn't a missing cache item. It's hard to tell from the code but it could be a bad `game` reference was set/passed somewhere. I think you'll have to debug that statement, find the undefined reference, and trace it back to your code. In sprite = this.create(ii*100, i*140, 'board_blank'); sprite is a Display Object and board_blank is a key identifying an image in the cache. The key is enough to retrieve the image (if it exists). If it doesn't, you'll get a console warning and the sprite will show the "missing" texture (X), but no error. (2) If you just a need a history of sorts to read and write to throughout the game, you can use something like game.stats = { player: { /* … */ } } If you want to use more "permanent" objects, you might instead create a new class (Character?) that can hold a sprite instance. Link to comment Share on other sites More sharing options...
Zampano Posted January 29, 2017 Author Share Posted January 29, 2017 Thank you very much for your answer! The first error really did disappear after I sorted out some unnecessary complicated processes that lead up to that line For the second problem, I'm now using a global variable that holds the character instance which holds the specific sprite key. Then I just add sprites to the state whenever needed using that key. Link to comment Share on other sites More sharing options...
Recommended Posts