0 Members and 2 Guests are viewing this topic.
HitBoxSquare = class()function HitBoxSquare:init(xIn, yIn, width, height) self.x = xIn self.y = yIn self.width = width self.height = heightendfunction HitBoxSquare:contains(xMouse, yMouse) local sw = self.width local sh = self.height return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)end
function on.mouseDown(x, y) mouseX = x mouseY = y --Other code hereend
function on.mouseMove(x, y) mouseX = x mouseY = y --Other code hereend
hitBox = HitBoxSquare(x, y, width, height)
if hitBox:contains(mouseX, mouseY) then --Do something hereend
------------------------------------------------- Checking multiple hitboxes, the fast way. ---- ElementCoder, 2012 -------------------------------------------------HitBoxSquare = class()function HitBoxSquare:init(xIn, yIn, width, height) self.x = xIn self.y = yIn self.width = width self.height = heightendfunction HitBoxSquare:contains(xMouse, yMouse) local sw = self.width local sh = self.height return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)endhb1 = HitBoxSquare(10, 10, 10, 10)hb2 = HitBoxSquare(10, 25, 10, 10)hb3 = HitBoxSquare(10, 40, 10, 10)hitbox = {hb1, hb2, hb3}hitboxname = {"Hitbox 1", "Hitbox 2", "Hitbox 3"}function on.paint(gc) gc:drawRect(10, 10, 10, 10) gc:drawRect(10, 25, 10, 10) gc:drawRect(10, 40, 10, 10)endfunction on.mouseMove(x, y) mouseX, mouseY = x, y for i = 1, 3 do if hitbox[i]:contains(mouseX, mouseY) then print(hitboxname[i].." contains the mouse!") end endend
hitBox1 = HitBoxSquare(x, y, widht, height)hitBox2 = HitBoxSquare(x, y, widht, height)hitBox3 = HitBoxSquare(x, y, widht, height)
hitBox1:contains(mouseX, mouseY)hitBox2:contains(mouseX, mouseY)hitBox3:contains(mouseX, mouseY)
hitBox={}hitBox[1] = HitBoxSquare(x, y, widht, height)hitBox[2] = HitBoxSquare(x, y, widht, height)hitBox[3] = HitBoxSquare(x, y, widht, height)
for num=1,10 dosomethingend