Page 1 of 2
For loop problem
Posted: Tue May 19, 2020 4:03 pm
by leyohla
Sorry to post again on a similar question but I've been racking my brain and internet all day but still totally unsure of what I am doing wrong here...
Code: Select all
balls = {}
function addBalls(x, y)
local ball = Ball:init(x, y)
table.insert(balls, ball)
for i in ipairs(balls) do
love.graphics.draw(ball.x, ball.y)
end
end
So I want to spawn multiple balls when calling this 'addBalls' function. For each ball in the table 'balls', I want to draw a ball onto the screen whenever this function is called, but for some reason this is not happening.
Here's the file:
- Ball.lua
- (2.92 KiB) Downloaded 181 times
Here's how I call this function:
Code: Select all
if self.powerup:collides(self.paddle) then
powerup = false
addBalls(x,y)
end
Just a disclaimer to say I am
new to Lua and a
noob programmer !!
Re: For loop problem
Posted: Tue May 19, 2020 4:40 pm
by sphyrth
Code: Select all
for i in ipairs(balls) do
love.graphics.draw(ball.x, ball.y)
end
Don't put that in addBalls. Put that under love.draw.
And write it like this instead:
Code: Select all
for _, b in pairs(balls) do
love.graphics.draw(b.x, b.y)
end
Take note that i used
pairs instead of
ipairs, although that might work, too.
Re: For loop problem
Posted: Wed May 20, 2020 7:56 am
by leyohla
Thanks so much
@sphyrth
problem with multiple object access with mouse in ipairs
Posted: Wed May 27, 2020 6:06 am
by CanYouReply
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
Re: problem with multiple object access with mouse in ipairs
Posted: Wed May 27, 2020 8:11 pm
by 4vZEROv
I don't understand what you are trying to do, do you want to check if you click inside the big rectangle and not the small ones ?
Re: For loop problem
Posted: Thu May 28, 2020 4:22 pm
by CanYouReply
I have already got mouse click event inside the small rectangles, at the same time I want to check if I can get mouse click event outside the small rectangles. (please ignore the big rectangle).
That means when I click inside any small rectangles I will get a text display 'mouse clicked inside small rectangles' and when I click outside the small rectangles I will get text display 'mouse clicked outside small rectangles'
Re: For loop problem
Posted: Thu May 28, 2020 6:15 pm
by Andlac028
Something like this? If clicked in any small rectangle, set clickedInRectangle to true, else its false and at the end, check whetever clickedInRectangle is true or false.
Code: Select all
function love.update(dt)
-- getting mouse x, y positions (irrespective of clicks)
mouse_x = love.mouse.getX()
mouse_y = love.mouse.getY()
--ADDED
local clickedInRectangle = false -- whatever clicked in any small rectangle
--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'
--ADDED
clickedInRectangle = true -- make it true, because clicked in small rectangle
-- now do other process --
end
end
--ADDED
if not clickedInRectangle then --if not clicked in any small rectangle
textDisplay = 'You have clicked outside of the small boxes'
end
end
Re: For loop problem
Posted: Fri May 29, 2020 7:09 pm
by CanYouReply
Thanks. But as soon as the program starts, without any click, it shows ' you have clicked outside the rectangle'. Moreover I have to keep the mouse button pressed inside the small boxes to get a response that I have clicked inside the mouse button. Can you please rephrase the codes in such a way that once clicked inside the small rectangles, the text message will show 'mouse clicked inside' & it will remain in that way till user is clicking outside the boxes - when it will display 'clicked outside' ?
Re: For loop problem
Posted: Fri May 29, 2020 8:46 pm
by 4vZEROv
Code: Select all
function love.load(arg)
windowWidth = love.graphics.getWidth()
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
textDisplay = ""
gameState = 'start'
end
function love.update(dt)
end
function love.mousepressed(x, y)
local inside_small_boxes = false
for i, v in ipairs(smallBoxes) do
if CheckCollision(x, y, 0, 0, v.x, v.y, v.width, v.height) then
inside_small_boxes = true
break
end
end
if inside_small_boxes then
if gameState == 'start' then gameState = 'play' end
textDisplay = 'You have clicked inside one of the small boxes'
else
textDisplay = ''
end
end
function love.draw()
-- draw the big box
love.graphics.setColor(200, 200, 60, 255)
love.graphics.rectangle('line', 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
Re: For loop problem
Posted: Sat May 30, 2020 6:29 am
by CanYouReply
Thanks '4vZEROv'. Now it is showing click events inside as well as outside the small boxes. Perfect. I never thought to write the code in mouse pressed function and check. I was writing in update function. Thanks a lot.
Code: Select all
function love.mousepressed(x, y)
local inside_small_boxes = false
for i, v in ipairs(smallBoxes) do
if CheckCollision(x, y, 0, 0, v.x, v.y, v.width, v.height) then
inside_small_boxes = true
break
end
end
if inside_small_boxes then
if gameState == 'start' then gameState = 'play' end
textDisplay = 'You have clicked inside one of the small boxes'
else
textDisplay = 'You have clicked outside small boxes' -- this text is added here
end
end