Regarding my previous question (Sprites with random text), is it possible like in Xcode/Cocoa to enlarge the touch area of a sprite? I have a sprite, for example 80x80 px and want a touch area of 100x100 px (10 pixels extra around the sprite).
Also can you always use onMouse events instead of onTouches? They both work on the simulator.
I'm not sure if what you ask is available. But an alternative way would be to add a bigger child (touch area) to your sprite (text container), that would be responsible of event listeners (like a "bounding" or "collision" box). When this touch area detects an event, an action is triggered on its parent (getParent()).
When you add a touch listener to a sprite, ALL sprites with touch listeners receive all the events (unless one calls the event.stopPropagation() function), so all you have to do is instead of calling Sprite:testHitPoint() just call Sprite:getBounds() which returns the screen position and then adjust the size of the bounds and do the in box comparison yourself. It's exactly what I do when I press a button - I have an "extended" area around the sprite so you can slightly move off without loosing a potential press.
eg.
function Object:touchListener(event)local x,y,w,h = self:getBounds(stage)
x = x - extend
y = y - extend
w = w + extend + extend
h = h + extend + extend
if event.x < x or event.x >(x+w)thenreturnendif event.y < y or event.y >(y+h)thenreturnend-- hit this object...
event.stopPropagation()end
WhiteTree Games - Home, home on the web, where the bits and bytes they do play! #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
function Object:touchListener(event)--scale object
self:setScale(2)--or whatever scale you wantif self:hitTestPoint(event.x, event.y)then--you've got a touch eventend--scale back
self:setScale(1)
event.stopPropagation()end
@ar2rsawseen: That would only work if the object's anchor point was centred. As you currently can't set the anchor point for shapes or text areas then this method wouldn't work.
Comments
I'm not sure if what you ask is available.
But an alternative way would be to add a bigger child (touch area) to your sprite (text container), that would be responsible of event listeners (like a "bounding" or "collision" box).
When this touch area detects an event, an action is triggered on its parent (getParent()).
When you add a touch listener to a sprite, ALL sprites with touch listeners receive all the events (unless one calls the event.stopPropagation() function), so all you have to do is instead of calling Sprite:testHitPoint() just call Sprite:getBounds() which returns the screen position and then adjust the size of the bounds and do the in box comparison yourself. It's exactly what I do when I press a button - I have an "extended" area around the sprite so you can slightly move off without loosing a potential press.
eg.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
But this will also reduce the available space in my sprite sheet.
Regards,
Marc
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive