StablexUI
StablexUI copied to clipboard
How to set hitArea of Button widget
Hi! Is there any way to set hitArea for Button widget?
Now i'm doing it like this: In xml:
<Button
left="302"
top="242"
skin:Img-src="'assets/views/MainMenu/menu/button_singleplayer_up.png'"
skinHovered:Img-src="'assets/views/MainMenu/menu/button_singleplayer_over.png'"
skinPressed:Img-src="'assets/views/MainMenu/menu/button_singleplayer_down.png'"
autoSize="true"
hitArea="@SPShape"
/>
In code:
var shape : Sprite = new Sprite();
shape.graphics.beginBitmapFill(Assets.getBitmapData('assets/views/MainMenu/menu/shape.png'));
shape.graphics.endFill();
addChild(UIBuilder.buildFn('Windows/Views/MainMenu.xml')(
{
SPShape : shape
}
));
And button fails to accept any hit (no mouse over or click). Buttons without hitArea works well. So maybe there is some problem in my graphic of hit area.
Try fixing your second code block like this:
var shape : Sprite = new Sprite();
var bmp = Assets.getBitmapData('assets/views/MainMenu/menu/shape.png');
shape.graphics.beginBitmapFill(bmp);
shape.graphics.drawRect(0, 0, bmp.width, bmp.height);
shape.graphics.endFill();
addChild(UIBuilder.buildFn('Windows/Views/MainMenu.xml')(
{
SPShape : shape
}
));
No still not working. But should it realy work with hitArea. Maybe you have some tests or tutorial where this works.
What target are trying? Since it's part of flash api perhaps it's not implemented in targets other than flash.
I'm trying flash target first of all other. If i manage to bring it to work in flash target i'll try else where.
I have an idea for you! Create an image with color 0 for areas of hit testing, then get mousex mousey of button and use them to read from hit test image, you can then decide what to do upon color read
Of course you will need to scale mouse x and mouse y using devise resolution, or scale the image.. use devise res/ 72 .. On Jan 11, 2014 1:42 PM, "gfcrba" [email protected] wrote:
I'm trying flash target first of all other. If i manage to bring it to work in flash target i'll try else where.
— Reply to this email directly or view it on GitHubhttps://github.com/RealyUniqueName/StablexUI/issues/108#issuecomment-32093775 .
Managed to solve this problem. You just nedd to add your shape as child
addChild(shape);
But when i do so it places itself default in 0,0 - not under it's button owner. I think that happens becouse i realy add 'shape' not to button but to scene directly.
I'll try to work it around to manage adding it as button child not as button's parent child.
PS: Also i read that you have to put mouseEnabled to false in shape, or it will recieve mouse events insted of button http://haxe.org/api/flash/display/sprite?version=12035.
To place shape directly under button add this in your xml defining button on-create="$this.addChild(@SPShape);" and don't forget to put visibility of the shape to false. Hope thi help someone.
PS: Not working for other targets then flash. Error no field hitArea. So to make it work cross platform we need some manual implementation of hitArea for them. I think that can be done like hopewise said before or by taking into account clicked target color. Then if point transparent you need to fire event for other targets in this point.