Jump to content

How to get image dimensions before loading it?


Skender
 Share

Recommended Posts

Hello,

 

I'm using spritesheets in my project. Unfortunately, I don't know the frame dimensions at the beginning, but I can calculate them given the image dimensions. I need to a possibility to either get the image dimensions before i create the spritesheet or a way to change them after creating the spritesheet. What I'm currently doing looks like this:

game.load.spritesheet("001", "assets/spritesheets/001.png", 19, 17, 16);

I tried serveral ways to get the image dimensions like:

var image = game.cache.getImage("001");

This only returns a string, like <img name="001" src="assets/spritesheets/001.png"> and I can't figure out how to get dimensions by using the string. Even if I could, I don't know how to change the spritesheet frame size and frame count afterwards.

 

Thanks in advance for help.

Link to comment
Share on other sites

I think you're almost there, try something like:

var width = game.cache.getImage("001").width;var height = game.cache.getImage("001").height;

I suspect you want to specify the spritesheet in terms of frames across by frames high, instead of specifying the exact size of each frame.  It can get quite tedious having to continually work out the frame size when the graphic is constantly changing,

Link to comment
Share on other sites

Thanks for the response, I will try that.

My next problem is, that I have to get the image in the cache somehow.

If I load it in the cache via load.spritesheet(), I need a possibility to acces the spritesheet afterwards to change the frame width and height. If this is not possible I would try something like load.image, get the dimensions of this object and then create the spritesheet, knowing the frame dimensions.

The problem with that approach is, that it seems like the only "constructor" of spritesheet requires a filepath and cannot take an object of type image.

I don't want to load the image twice.

Any ideas?

Link to comment
Share on other sites

  • 11 months later...

I think you're almost there, try something like:

var width = game.cache.getImage("001").width;var height = game.cache.getImage("001").height;

I suspect you want to specify the spritesheet in terms of frames across by frames high, instead of specifying the exact size of each frame.  It can get quite tedious having to continually work out the frame size when the graphic is constantly changing,

 

There is no property 'width' for CachedImage in phaser 2.4.4. What should I do? Thanks

Link to comment
Share on other sites

  • 3 years later...
 Share

  • Recently Browsing   0 members

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