Skender Posted November 4, 2014 Share Posted November 4, 2014 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 More sharing options...
stupot Posted November 4, 2014 Share Posted November 4, 2014 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 More sharing options...
Skender Posted November 4, 2014 Author Share Posted November 4, 2014 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 More sharing options...
bagusflyer Posted November 4, 2015 Share Posted November 4, 2015 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 More sharing options...
Batzi Posted November 4, 2015 Share Posted November 4, 2015 There is no property 'width' for CachedImage in phaser 2.4.4. What should I do? Thanksvar image = game.cache.getImage('imageName');console.log(image.width);Worked for me! Link to comment Share on other sites More sharing options...
stuartjnelson Posted December 19, 2018 Share Posted December 19, 2018 Thanks for the advice! How performant is this technique? If I used it a lot in my game would it affect performance? game.cache.getImage("001").width Link to comment Share on other sites More sharing options...
Recommended Posts