Hi
I'm new to Love2d.
So here is the problem:
I tried to fill the screen with tiles but the screen went totally black when I ran my code.
The thing is, when I run the line of the code that draws the tile alone, it works.
here is my script that I've written so far:
tile = {}
tile.img = love.graphics.newImage("Assets/Green_concrete.jpg")
tile.x = 0
tile.y = 0
tile.espace = 60
function love.draw()
for i= 1, 12 do
for i= 1, 21 do
love.graphics.draw(tile.img,tile.x,tile.y,0,0.15)
tile.x = tile.x + tile.espace
end
tile.y = tile.y + tile.espace
end
end
thank you !
Drawing a line of tile
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Drawing a line of tile
Not sure, but you are having two loops rewriting each other using the same variable i.
Also, the syntax is:
And it seems you are using x scaling? That is probably unintended.
You will also need to reset tile.x after each row.
Also, the syntax is:
Code: Select all
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
You will also need to reset tile.x after each row.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Drawing a line of tile
Code: Select all
tile = {}
tile.img = love.graphics.newImage("Assets/Green_concrete.jpg")
tile.x = 0
tile.y = 0
tile.w, tile.h = love.grephics.getDimensions ()
function love.draw()
for j= 0, 12 do -- vertical
for i= 0, 21 do
local x = tile.x + i * tile.w
local y = tile.y + j * tile.h
love.graphics.draw(tile.img, x, y)
end
end
end
Re: Drawing a line of tile
No they don't rewrite each other, despite of using the same variable. Loop control variables are implicitly local. You can check with e.g.:
Code: Select all
for i = 1, 5 do
print(i)
for i = 10, 12 do
print(i)
end
end
--[[ Output:
1
10
11
12
2
10
11
12
3
10
11
12
4
10
11
12
5
10
11
12
--]]
Re: Drawing a line of tile
Ok, sure, Lua and thanks for the correction. Anyway, bad programming habits make the code less readable and hard to debug. I. E. Never use a variable iterator with the same name for main and subloops.pgimeno wrote: ↑Wed Sep 27, 2023 7:45 amNo they don't rewrite each other, despite of using the same variable. Loop control variables are implicitly local. You can check with e.g.:
Code: Select all
for i = 1, 5 do print(i) for i = 10, 12 do print(i) end end --[[ Output: 1 10 11 12 2 10 11 12 3 10 11 12 4 10 11 12 5 10 11 12 --]]
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Who is online
Users browsing this forum: No registered users and 6 guests