Jump to content

changing sprite based on user input


yakfactorial
 Share

Recommended Posts

I'm making a cat themed game where users can choose their sprite by clicking a button, and the sprite will change color based on the value of the button they click. selectedCat is a global variable, and catClass is the class of the 3 buttons the user will click. I'm saving the data in local storage to the global selectedCat variable, and then trying to access it later when I initialize the player with     player = game.add.sprite(32, game.world.height - 150, 'selectedCat'); . However, when I do that, I get an error message saying "texture with key selectedCat not found". I'm guessing that's because selectedCat isn't attached to any of the spritesheets, and the even though the spritesheets have the same variable names as the buttons do, they're not connected. How can I connect selectedCat to its corresponding spritesheets? Any help is much appreciated.

submit_script.js

game_script.js

Link to comment
Share on other sites

It's better to post the actual code up and not the script files. Easier for everyone.

Not too sure about the whole of your question, but the error is because it's looking for the spritesheet 'selectedCat' that should have been loaded in the preloader:

this.load.image('selectedCat', 'assets/catSheet.png');

The sprite object loads whichever image you pass to it with the name as given above. If you use a sprite sheet or atlas, you load the sprite as normal and can set the frame name as required:

this.blueCat = this.add.sprite(this.game, 10, 10, 'catSpriteSheet');
this.blueCat.frameName = 'BlueCat';

 

Link to comment
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.

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

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...