Hit test on transparent objects?

I’d like to use an image or rect to create a hit area for a smaller underlying sprite, but I’d also like that hit area to be invisible. Setting the hit objects alpha to 0 makes it not hit testable, is there any way to get around this? Or another way of creating a hit area without using images or display objects?

Thanks! [import]uid: 120 topic_id: 15971 reply_id: 315971[/import]

Why not just use math without any display objects at all? Just test to see if the point X,Y is within the bounds of your hit area.

Here is a link to a solution using a similar method:

http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds

You would want to edit that code so it doesn’t actually use the bounds of a display object though, and hardcode in the values directly. [import]uid: 49447 topic_id: 15971 reply_id: 59154[/import]

To make invisible things hitTestable, simply set the isHitTestable parameter of the object to true.

local trigger = display.newRect( 0, 0, 50, 50 )  
trigger:setFillColor( 255,0,0 )  
trigger.x = 269  
trigger.y = 140  
trigger.alpha = 0  
trigger.isHitTestable = true  
trigger:addEventListener("touch", triggerHandler)  

I use this all the time and it works great.

I set a fillcolor so i can place the triggers accurately. just temporarily set their alpha to 0.5

Hope this helps!
Joe [import]uid: 8444 topic_id: 15971 reply_id: 59160[/import]

I suppose you could set up some detection if player hits a certain X and Y coordinate utilizing math.
For what it’s worth you CAN do it with invisible objects. I have the alpha on the invisibleBox set to .10, but you can set to 0 and see the same results.

Now I am sure you could do some welding to get the invisibleBox to tie to the “player” then make the player is sensor or something…

--Here we go for some fun  
  
display.setStatusBar( display.HiddenStatusBar )  
  
local physics = require("physics")  
physics.start()  
physics.setGravity(0, 10)  
physics.setPositionIterations(32)  
  
local game = display.newGroup()  
game.x = 0  
game.y = 0  
  
local \_W = display.contentWidth  
local \_H = display.contentHeight   
local ball = display.newCircle (\_W/2 -10, \_H/2 - 200, 20)  
physics.addBody(ball)  
ball:setFillColor(255,0,0)  
game:insert(ball)  
  
local invisibleBox = display.newRect (\_W/2 -25, \_H/2,50,50)  
invisibleBox.alpha = 1  
invisibleBox:setFillColor(0,255,0)  
game:insert(invisibleBox)  
physics.addBody(invisibleBox ,"static")  
  
local Player = display.newRect (\_W/2 -23, 483, 46,46)  
Player:setFillColor(0,0,255)  
  
local function invisibleBoxCollision(event)  
 if event.phase == "began" then  
 print("collided: InvisibleBox Hit!!")  
  
ball:removeSelf( )   
 print("collided: Ball Removed!!")  
 end   
  
end  
  
--event listnener on chasebox, so if ball touches it - game over  
invisibleBox:addEventListener("collision",invisibleBoxCollision)  
  

[import]uid: 61600 topic_id: 15971 reply_id: 59157[/import]

@firemaplegames

Interesting!

Could you post the triggerHandler function you are calling when touched?

I never knew about the isHitTestable? I’m still noob, so yea that explains that part :slight_smile:

See, I was thinking of some weird ass logic like

Set a player up (a square)

Then set another object that is alpha 0 that is just 1 pixel larger than the player to set up a hit box of sorts. Then using some logic like if it passes the outer pixel then something happens, if it gets past the inner pixel barrier then something else happens. Like doodle jump logic with the platforms (go through it one way, then becomes something you can bounce on)…

I don’t know, my mind gets creative on things but doesn’t always know what to do about it… [import]uid: 61600 topic_id: 15971 reply_id: 59163[/import]

Thanks for the quick feedback guys. @nicholasclayg I used that to disable hit testing on some masked objects, don’t know why I didn’t think of doing it for the invisible object… works like a charm though.

And I thought about using math, but for most of the objects I’m going to eventually use bitmaps so I can make irregular hit areas.

[import]uid: 120 topic_id: 15971 reply_id: 59183[/import]

Well, i made the alpha to 0.01 and it gets the tap event (or touch if you want) and it doesn’t show at all, or at least is almost not visible :stuck_out_tongue:

You can try it to see if that works for you or you can try even other numbers like 0.001 to see if that works (at least i know 0.01 works) [import]uid: 75034 topic_id: 15971 reply_id: 59245[/import]