Hi,
I am new around here. Recently while experimenting with 'ipairs' I am finding an issue which I can't solve. May I request a slight, bit help from the experts....
This code is written in 'main.lua' file & I am finding a problem - which I can't overcome... I created a table for a bigger box. In this bigger box there are multiple smaller boxes. I am trying to access the presence of mouse clicks inside & outside of these multiple smaller boxes. Now when I am clicking inside these multiple boxes (not the big box) I can get response that mouse is being clicked inside these smaller boxes. However, when I am clicking outside these multiple boxes, I can't get any response on mouse click. I need both the responses. Can you please help ?? I am giving below the complete codes for your help. Thanks in advance. :
Code: Select all
function love.load(arg)
-- get window width
windowWidth = love.graphics.getWidth()
-- get window height
windowHeight = love.graphics.getHeight()
-- create table for the big box:
bigBox = {}
bigBox.width = 480
bigBox.height = 150
bigBox.x = windowWidth/2 - bigBox.width/2
bigBox.y = windowHeight/2 -bigBox.height/2
-- create small small boxes
smallBoxes = {}
for i=1, 3 do
local box = {}
box.width = 103
box.height = 103
if i == 1 or i == 2 or i == 3 then -- first row
box.x = i * (box.width + 10) + (windowWidth/2 - bigBox.width/2 + 10)
box.y = windowHeight/2 - bigBox.height/2 + 10
end
table.insert(smallBoxes, box)
end
-- communication of game with players
textDisplay = ""
-- initialising and getting mouse positions (without clicks)
mouse_x = love.mouse.getX()
mouse_y = love.mouse.getY()
--load game state as 'start'
gameState = 'start'
end
function love.update(dt)
-- getting mouse x, y positions (irrespective of clicks)
mouse_x = love.mouse.getX()
mouse_y = love.mouse.getY()
--Access the 'box' parameters (like: box.x, box.y, box.width, box.height) under 'smallBoxes' through 'ipairs' procedure
for ii, vv in ipairs(smallBoxes) do
-- now check click of mouse on box areas and check check if mouse then is inside the small boxes area or not
if love.mouse.isDown(1) and CheckCollision(mouse_x, mouse_y, 0, 0, vv.x, vv.y, vv.width, vv.height) then
gameState = 'play' -- At this point we can change game state from 'start' to 'play'
textDisplay = 'You have clicked inside one of the small boxes'
-- now do other process --
end
end
end
function love.draw()
-- draw the big box
love.graphics.setColor(200, 200, 60, 255)
love.graphics.rectangle('fill', bigBox.x, bigBox.y, bigBox.width, bigBox.height)
-- draw the small boxes
love.graphics.setColor(130, 95, 250, 255)
for i,b in ipairs(smallBoxes) do
love.graphics.rectangle('fill', b.x, b.y, b.width, b.height)
end
-- game communucation with players
love.graphics.setColor(0, 255, 255, 255)
love.graphics.print(": "..textDisplay, 20, 540)
end
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end