Jump to content

associating an image with a class object


jjwooyoung
 Share

Recommended Posts

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

Link to comment
Share on other sites

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");
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...