I'm making a game, and encounter with many problems and want help! Here's a screensnap for you to figure out quickly what kind of game it is: As you can see, a player can click the chicken to attack it, a very basic operation. The chicken is a spriter animation, made with Spriter. All of its parts are drawn to a canvas firstly, and then the canvas is drawn to the screen.
There're irregular entities in the game, so I want accurate hit test, rather than the bounding-box-method hit test.
I use the following code to do the hit testing work( Spriter in the code is a thirdparty library name ).
But here's a problem. I found a terrifying warning in the love wiki, which is really a concern:
canvas.getPixel():
This function can be very slow: it can cause the CPU to wait for the GPU to finish all the work currently in its queue, which can be a whole frame's worth (or more.)
Code: Select all
function Spriter:testHit(pos_x, pos_y, x, y, scale_x, scaley)
local canvas = self:getCanvas()
x, y = self.offsetX - (pos_x - x) / ((self.scalex or 1) * (scale_x or 1)), self.offsetY - (pos_y - y) / ((scaley or 1) * (self.scaley or 1))
if not (x >= 0 and y >= 0 and x < canvas:getWidth() and y < canvas:getHeight()) then return false end
[color=#FF0000]local _,_,_,a = canvas:getPixel(x, y)[/color]
if a > 0 then return true end
end
Can I use canvas:getPixel() here?
How do you do the accurate hit test?
thanks a lot!