Hi guys,
Is there a way to detect if a mouse click happened on the transparent background of an image (of an object)? I'm sorry if this question's been asked before. I did go through many old threats but couldn't find the answer I was looking for.
Quick answer is yes. You have two choices, as far as I remember, if you want this for all your images, then go to the game settings and there's an option called something like "pixel perfect" turn it to false, and I belive that'll do it (I'm not 100% sure though).
If if you want this for only a few images and the others react normal to alpha, then edit your spirte, and give the alpha 99% transparency. Player won't notice it, but the engine will. ;)
Cassiebsg, thank you for a quick reply :) Your solution suggested me another approach to my problem. However, I think what I had in mind was a way to check whether or not the given pixel of the image is a part of the transparent background.
i take it your image has a solid green colour for its background. You know you can just have no background solid colour, right? But leave transparency as is when importing sprites. Are you wanting to make sure that green isn't being used as part of the object? I'm confused as to what you are trying to achieve.
This is taken from doing it for a GUI, but the same concept should apply - draw a Dynamic Sprite version of the object image without transparency, check the color of where the mouse is and that should tell you whether it's transparent or not. (Snarky did this for me in another thread somewhere)
bool MouseOverPixelPerfect(this Button*, int mouseOverGraphic, String label)
{
// Calculate mouse coordinates relative to button sprite canvas
int x = mouse.x - this.X - this.OwningGUI.X;
int y = mouse.y - this.Y - this.OwningGUI.Y;
// Get the color on button sprite canvas at mouse cursor pixel
DynamicSprite* buttonMask = DynamicSprite.CreateFromExistingSprite(this.NormalGraphic, false);
DrawingSurface* maskSurface = buttonMask.GetDrawingSurface();
int pixelColor = maskSurface.GetPixel(x,y);
maskSurface.Release();
buttonMask.Delete();
if(pixelColor == COLOR_TRANSPARENT) // if this doesn't work then try your harcoded number, 63519
{
// Transparent, so don't highlight
this.MouseOverGraphic = this.NormalGraphic;
return false;
}
// Otherwise, highlight
this.MouseOverGraphic = mouseOverGraphic;
return true;
}
Privateer Puddin', thank you so much for your help. This is exactly what I needed! I've slightly adjusted the code you posted and it works like a charm.
ManicMatt, I've made a jigsaw puzzle where you drag pieces with the mouse. However, because most of the pieces don't have perfect rectangular shapes, I wanted to prevent dragging them when the player clicks on their transparent background.
Oh, well if the code works for you, great! I don't really understand why all of that is necessary though if one uses a transparent background instead of a purple or green etc background, but hey ho it works pixel perfect for me. (Not for a GUI)
Yes, I thought actually you wanted to increase the clickable area of small objects/characters in rooms and was wondering how to detect the alpha of an object for that purpose. (laugh)
Quote
I thought actually you wanted to increase the clickable area of small objects/characters
No, I haven't got this issue (yet!), but when I do I will use your suggestion ;)