jjwooyoung Posted April 27, 2014 Share Posted April 27, 2014 I'm thinking of creating a mini chrome extension game for learning purposes. But I don't quite understand how an image sprite can be associated with a specific object. I've tried googling but not having any luck. Can someone provide some explanation? Let's say I have a folder with these files:- 123.gif- 234.gif And in my code, would I have something like this?if(val <= 240) { objCharacter.setImage() = "../123.gif";} else { ...} ... setImage() { // what goes here?}Or do I use an xml file? I've briefly looked at some gaming files and it seems like that's what's commonly used Quote Link to comment Share on other sites More sharing options...
giraluna Posted April 28, 2014 Share Posted April 28, 2014 Storing the object templates in a seperate data structure is the way to go. You can use XML, but json or plain javascript objects would probably be better in this case. Something like:var gameData = gameData || {};gameData.vehicles ={ formula: { sprite: "formula.png" topSpeed: 300 }, motorcycle: { sprite: "motorcycle.png", topSpeed: 200 }}function Vehicle(templateName){ var template = gameData.vehicles[templateName]; this.sprite = template.sprite; this.topSpeed = template.topSpeed;}var playerCar = new Vehicle("formula"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.