It looks like you're new here. If you want to get involved, click one of these buttons!
-- Create menu group local menuGroup = Sprite.new() stage:addChild(menuGroup) -- Help button image helpBTN = Bitmap.new(Texture.new("helpBtn.png")) helpBTN:setPosition(439, 11) helpBTN.name = "helpBTN" menuGroup:addChild(helpBTN) local function testButton() print"BANG" end helpBTN:addEventListener(Event.MOUSE_UP, testButton, helpBTN) |
Comments
Mouse and Touch events are dispatched to all objects on the stage. Therefore you need to test whether the mouse coordinate is hitting the object or not. If you change the testButton function as:
Also, there is an easy to use Button class in the examples (Graphics/Button). I recommend you to use it directly in your game.
game play screen. My concern is, if I use the Button class for my menu screen and then
switch to my game play screen, won't all the Button class event listeners still be running
in my game play screen, or can I delete remove the Button class after using?
Sorry for all the questions, like I said, I'm still new to LUA. Thank you.
Button class event listeners:
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
Thanks.
It was a hard decision We choose this method because of efficiency. (we don't want to do hit test for every mouse/touch events and leave the decision to the programmer)
@Millerszone
mouse and touch events are dispatched to the sprites that are on the stage. Therefore, when you remove your menu screen from the stage, none of its children will receive mouse/touch events and everything will be ok
I thought the event listeners would always be running in the background if you didn't remove.
So now I will only remove the screen, that's good to know.
But, ENTER_FRAME event is a little bit different. It's dispatched to all Sprites. I can recommend using ADDED_TO_STAGE and REMOVED_FROM_STAGE events to control the ENTER_FRAME event as:
i see.. but from an oo view; adding a touch listener to an object and receiving a callback from it implies that a touch event just occured on _that_ object IMHO. this confuses me. anyway.. Using _only_ Runtime object to add those listeners and get rid of sprite touch listener may be a more appropriate decision. Or you could automagically hit test only those sprites with event listeners attached. just my 2 cents 8)
As I said, it was a hard decision
I all understand your points. Mouse down and up events are ok, but mouse move events can sometimes be problematic when they only occurs on the specified object (e.g. while dragging)
We don't want to add mouse/touch events to a Runtime object because you can stop propagation of the event so that only the top most can receive the events.
Still I don't know which way is better
I've had a MOUSE_DOWN Listener before and everything worked fine, but decided then, I rather would use multitouch, so went to TOUCHES_BEGIN and since then it prints me an error
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
When a touch event occurs, the event table contains two fields: 'touches' and 'allTouches'. You should use this fields instead of 'x' and 'y'. For example:
Likes: yarnee
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
Please change it
Another way to think of it would be to think of the stage as big container and just add the touch handler to that and the propagate the events yourself - I did this with my first Gideros app and it worked well, however @atilim's system is easier (and more flexible) as you don't have to explicitly manage all of your buttons in a single place.
QUICK TIP.
After a really close examination of how UI buttons work on iOS I discovered another little "tip", which is especially useful for smaller buttons.
Some buttons on iOS have an "extension" around them, which is picked up when moving and releasing (but not pressing) so that if you touch a button you can move your finger slightly which might "just" go out of bounds but is still treated as a hold / press, it's a small thing but it makes a BIG difference to the user, it just makes the buttons "work" without being fiddly - as I said, it's especially useful on smaller buttons - especially tab bars - those of you with iOS devices, try it out!
I've added that functionality to my button / widget library (beta release coming soon), and it it DOES make a difference - it's the little things like this that most people don't notice but it just makes the app "feel" better on a subconscious level.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill