charlie_says Posted April 30, 2014 Share Posted April 30, 2014 Hi, I'm sure I'm missing something very obvious, but, can anyone point out where I'm going wrong here:shotBMD = game.add.bitmapData(8, 8);shotBMD.context.drawImage(game.cache.getImage('shot'), 0, 0, 8, 8);var col = shotBMD.getPixel32(3,3);// Uncaught TypeError: Cannot read property '27' of undefined Link to comment Share on other sites More sharing options...
rich Posted April 30, 2014 Share Posted April 30, 2014 Which version of Phaser? If 2.0.4 does this example work for you: https://github.com/photonstorm/phaser-examples/blob/master/examples/display/bitmapdata%20pixel.js Link to comment Share on other sites More sharing options...
charlie_says Posted April 30, 2014 Author Share Posted April 30, 2014 Thanks Rich, was on 2.0.3, updating to 2.0.4 resolves it. Link to comment Share on other sites More sharing options...
charlie_says Posted April 30, 2014 Author Share Posted April 30, 2014 Actually, it doesn't appear to be working correctly, no matter which x and y I choose all I get returned is: Object {r: 0, g: 0, b: 0, a: 1, h: 0…} Link to comment Share on other sites More sharing options...
rich Posted April 30, 2014 Share Posted April 30, 2014 BitmapData.getPixel32 will give you an rgba color value (warning: endianess applies)BitmapData.getPixel and BitmapData.getPixelRGB (which is what the example above uses) gives you a color object like you posted above. The difference between the two is that getPixel works across the slower data array, but is really useful in some cases. Link to comment Share on other sites More sharing options...
charlie_says Posted April 30, 2014 Author Share Posted April 30, 2014 I don't think I'm confused about how it should work, I think the returned data is incorrect. this code:bgBMD = game.add.bitmapData(350, 130);bgBMD.draw(game.cache.getImage('bg'), 0, 0);var col;col = bgBMD.getPixel32(1,1)console.log(col)col = bgBMD.getPixel32(10,10)console.log(colcol = bgBMD.getPixel32(100,100)console.log(col)col = bgBMD.getPixel32(150,100)console.log(col)col = bgBMD.getPixel(1,1)console.log(col)col = bgBMD.getPixel(10,10)console.log(col)col = bgBMD.getPixel(100,100)console.log(col)col = bgBMD.getPixel(150,100)console.log(col) returns this: 0 play.js:540 play.js:560 play.js:580 play.js:60Object {r: 0, g: 0, b: 0, a: 1, h: 0…}play.js:62Object {r: 0, g: 0, b: 0, a: 1, h: 0…}play.js:64Object {r: 0, g: 0, b: 0, a: 1, h: 0…}play.js:66Object {r: 0, g: 0, b: 0, a: 1, h: 0…} from this image: Link to comment Share on other sites More sharing options...
rich Posted April 30, 2014 Share Posted April 30, 2014 With your image, and more importantly the following code, I get this:var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() { game.load.image('pic', 'assets/wip/bg.png');}var bmd;function create() { bmd = game.make.bitmapData(800, 600); bmd.draw('pic', 0, 0); bmd.update(); game.add.sprite(0, 0, bmd); var col; col = bmd.getPixel32(1,1) console.log(col) col = bmd.getPixel32(10,10) console.log(col) col = bmd.getPixel32(100,100) console.log(col) col = bmd.getPixel32(150,100) console.log(col) col = bmd.getPixel(1,1) console.log(col) col = bmd.getPixel(10,10) console.log(col) col = bmd.getPixel(100,100) console.log(col) col = bmd.getPixel(150,100) console.log(col) }0042847647374284764737Object {r: 0, g: 0, b: 0, a: 1, h: 0…}Object {r: 0, g: 0, b: 0, a: 1, h: 0…}Object {r: 65, g: 82, b: 100, a: 255, h: 0…}Object {r: 65, g: 82, b: 100, a: 255, h: 0…} Link to comment Share on other sites More sharing options...
charlie_says Posted April 30, 2014 Author Share Posted April 30, 2014 Aha! Thanks Rich. It appears that the "magic code" is bmd.update(). Can you explain the difference between game.add.bitmapData and game.make.bitmapData? Link to comment Share on other sites More sharing options...
rich Posted April 30, 2014 Share Posted April 30, 2014 Nothing at all. game.make is the more sensible one to use, but removing it from game.add would have broken the API, so I left it in. Link to comment Share on other sites More sharing options...
Recommended Posts