Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Enlarge Touch area of an asset/sprite — Gideros Forum

Enlarge Touch area of an asset/sprite

DikkesnoekDikkesnoek Member
edited July 2012 in General questions
Hello,

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.

Thanks,

Marc

Likes: mehmetuysal

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • Hi Marc,

    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()).
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • techdojotechdojo Guru
    edited July 2012
    Sorry @Mells - actually it's easy :) .

    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) then return end
        if event.y < y or event.y > (y+h) then return end
     
        -- 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
  • @techdojo ha, at least I've tried ;)
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Thanks again. First I tried to enlarge the sprite with some extra transparent space.
    But this will also reduce the available space in my sprite sheet.

    Regards,

    Marc
  • ar2rsawseenar2rsawseen Maintainer
    Hmm, how about
    function Object:touchListener(event)
        --scale object
        self:setScale(2) --or whatever scale you want
        if self:hitTestPoint(event.x, event.y) then
            --you've got a touch event
        end
        --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.
  • ar2rsawseenar2rsawseen Maintainer
    @Scouser, yes you're right. It was just a spontaneous idea
Sign In or Register to comment.