开发者

2d Graphics - sprite click detection

开发者 https://www.devze.com 2023-03-27 11:37 出处:网络
I\'m working on a small app that uses sprites which are rendered using a canvas and simple drawBitmap.

I'm working on a small app that uses sprites which are rendered using a canvas and simple drawBitmap.

Once the user touch the screen I need to know which sprite was clicked on. I'm able to achieve this goal when I treat each sprite as a rectangular which has the width and height of the image. However, some of the sprites takes only small portion of the entire rectangular and I would like to ignore when user clicked inside the rectangular but not on the internal shape.

Any ideas what could be a good method to do that?

Edit: Just to be more clear, lets say I have a sprite with size of 200x200, the sprite is an image of an airplane from above and the airplane has long long wings. Since the wings are long there will lots of "dead" areas in the sprite. I would like to detect when user 开发者_Go百科clicks the airplane itself only and not the "dead" area.

Thanks.


You will need to create a 2d array of all the pixels in the bitmap. Mask the pixels to either a 0(transparent) or a 1(has color). Then when you click inside of a rectangle you will just need to get the width offset and the height offset within the rectangle. This gives you your indices for mapping to the pixel array. Then check and see if the index in the pixel array contains a 1 for a value. If so then you clicked on the actual image. Does that make sense?


You have to check for the area where your Bitmap gets drawn, not another rectangular shape. Just treat every sprite (which may have different sizes) as a single rectangle, whose width and height equals to the width and height of the sprites.

Since you elaborated your question I'll give another suggestion.

When you have detected a click on the sprite, simply check (in the Bitmap's area) the current pixel via the Bitmap.getPixel() function. You can then easily check if the color at the specified position is something you're interested in, otherwise you can just skip detecting the touch.

0

精彩评论

暂无评论...
验证码 换一张
取 消