-
-
How is the water placed there? You should test against the logical structure of the game world, not its visual representation.
-
The water is a bitmap object which is drawn into the background's BitmapData using copyPixels() method.
I've just tried to use getPixel() and traced the result like so:
Actionscript Code:
var pixel:* = MissionConstructor.map.containerBMP.bitmapData.getPixel(this.x, this.y); trace(pixel.toString(16));
It traces colors like this when the object's over some grass or anything else but water:
1e8ca7
1c8ba9
1c85a9
1982aa
107dab
1172a6
106ca3
And this sort of colors when it's over water surface:
e6aa4
d75af
b6ca8
b72b2
969a9
b79b7
97bb7
d82be
a71ae
b71b0
a7ab7
e90cc
97cb7
96faa
I definitely see the difference, and I guess I have to use this method but the problem now is that I can't figure out a way for the object to tell them apart..
I mean I need to compare this colors in some way... but how?
-
So you've got a separate bitmapdata with just the water in it? Can you test to see whether that bitmap has non-empty pixels at the corresponding location?
Ideally, you would have some gameworld representation that tells your other code where to draw grass and water, etc. Checking against that representation would make more sense than trying to compare pixel values.
You could deconstruct the pixel value returned and see whether the RGB channels fall into certain ranges. For instance, it seems that there is a noticable gap between the red values of the water and non-water. I would have expected larger gaps in the green and blue channels, but that may be misleading. It might suffice to test whether the pixel is "mostly blue", by comparing the blue channel to the red and green and seeing whether it is the highest value.
-
So you've got a separate bitmapdata with just the water in it?
Yes, but it's only used to copy pixels from it, it's never added to the display list itself
Ideally, you would have some gameworld representation that tells your other code where to draw grass and water, etc. Checking against that representation would make more sense than trying to compare pixel values.
I thought about it, but this way would slow the game down. And I want it to be as optimized as possible.
You could deconstruct the pixel value returned and see whether the RGB channels fall into certain ranges. For instance, it seems that there is a noticable gap between the red values of the water and non-water. I would have expected larger gaps in the green and blue channels, but that may be misleading. It might suffice to test whether the pixel is "mostly blue", by comparing the blue channel to the red and green and seeing whether it is the highest value.
That's exactly what I need, but I have no idea on how to do it. I'd really appreciate it if you could show me some example
-
BitmapData objects don't need to be on the display (in fact, they can't. Bitmaps can.) to be in memory and available for computation. Similarly, your gameworld representation would not be a DisplayObject that gets rendered, but just a logical object that describes the world. The Model in Model-View-Controller, if you like.
To deconstruct a pixel value into RGB, just use bitshifting and bitwise AND operations.
Code:
var pixel:uint = 0x3355aa;
var r:uint = (pixel >> 16) & 0xff; //0x33;
var g:uint = (pixel >> 8) & 0xff; //0x55;
var b:uint = pixel & 0xff; //0xaa;
Edit: Note that if you will be doing lots of pixel-level tests, it is vastly faster to use getVector to get a Vector of pixel values and then compare against that, than it is to call getPixel or getPixel32 multiple times.
Last edited by 5TonsOfFlax; 07-19-2010 at 01:34 PM.
-
BitmapData objects don't need to be on the display (in fact, they can't. Bitmaps can.) to be in memory and available for computation.
Yeah, I've messed it up a little. I got a bitmap with water and the pixels are copied from it like this:
Actionscript Code:
ks_bitmapData.copyPixels(dirtAndStuff.bitmapData, new Rectangle(0, 360, 370, 340), new Point(dirtNstuffPath[i].@x, dirtNstuffPath[i].@y), null, null, true);
well, no matter
Similarly, your gameworld representation would not be a DisplayObject that gets rendered, but just a logical object that describes the world. The Model in Model-View-Controller, if you like.
I think it's way too complicated for me at the moment.
To deconstruct a pixel value into RGB, just use bitshifting and bitwise AND operations.
Code:
var pixel:uint = 0x3355aa;
var r:uint = (pixel >> 16) & 0xff; //0x33;
var g:uint = (pixel >> 8) & 0xff; //0x55;
var b:uint = pixel & 0xff; //0xaa;
Thanks I'll try this
-
This works! All objects now can define water
Actionscript Code:
var pixel:uint = MissionConstructor.map.containerBMP.bitmapData.getPixel(this.x, this.y); var r:uint = (pixel >> 16) & 0xff; var g:uint = (pixel >> 8) & 0xff; var b:uint = pixel & 0xff; if (b > 130) { trace("I'm in water"); }
5TonsOfFlax, thanks again This helped me a lot
-
Don't even bother calculating red and green if you're not using them. Every little optimization helps, right?
-
Yeah, you're right. I gotta comment those two out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|