Classes, Sprite Keys etc.

Recommended Posts

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) {, 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: = 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 :D I appreciate for any help! Thank you!

Share this post

Link to post
Share on other sites

(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.


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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.