李孟昇
李孟昇

Reputation: 89

How to get loaded image property in phaser.js?

I have a problem for getting properties of loaded image in Phaser.js.

Now I resolve it by accessing private variable (a suck method I known...):

var image = game.textures.get("imageA") 
console.log("width",image.frames.__BASE.width);

Does anyone has a better solution to get these properties?

Thanks a lot.

Upvotes: 2

Views: 754

Answers (1)

winner_joiner
winner_joiner

Reputation: 14695

Depending on your UseCase, you could use the getSourceImage or get method, of the Texture object.

It is abit longer, also but works (if you need/want the html element ):

  var image = game.textures.get("imageA");
  console.info(image.getSourceImage().width);

here the link to the documentation

Or you could use the get function of the texture (if you need/want the phaser frame):

  var image = game.textures.get("imageA");
  console.info(image.get().width);

here the link to the documentation

The parameter for get and getSourceImage are optional, but you could enter a name/index of a frame, if you need a specific frame.

Upvotes: 3

Related Questions