Okay heres the deal, trying to create a simple parallax game.
Currently i have it if u click left it goes left and when your in 300px of the edge of the box its moves the background instead of the player. How can i detect if i need to generate more land?
function InitPlayer()
player = love.graphics.newImage("images/player.png")
playerwidth = 32
playerheight = 32
playerx = 500
playery = 359
end
function UpdatePlayer(dt, map_w)
if love.keyboard.isDown("left") then
if playerx+playerwidth <= 300 then
for i = 1, map_w/64 do
GroundMap[i] = GroundMap[i] + 75 * dt
end
for i = 1, #TreeMap do
TreeMap[i][2] = TreeMap[i][2] + 75 * dt
end
else
player = love.graphics.newImage("images/playerl.png")
playerx = playerx - 75 * dt
end
end
if love.keyboard.isDown("right") then
if playerx+playerwidth >= map_w-300 then
for i = 1, map_w/64 do
GroundMap[i] = GroundMap[i] - 75 * dt
end
for i = 1, #TreeMap do
TreeMap[i][2] = TreeMap[i][2] - 75 * dt
end
else
player = love.graphics.newImage("images/playerr.png")
playerx = playerx + 75 * dt
end
end
if love.keyboard.isDown("down") then
player = love.graphics.newImage("images/player.png")
end
end
function DrawPlayer()
love.graphics.draw(player, playerx, playery)
end
function InitGround(map_w)
grass = love.graphics.newImage("images/grass.png")
GroundMap = {}
for i = 1, map_w/64 do
GroundMap[i] = i*64-64
end
end
function UpdateGround()
end
function DrawGround(map_w)
for x = 1, map_w/64 do
love.graphics.draw(grass, GroundMap[x], 388)
end
end
function UpdateGround( x, y, w, h ) -- x and y are the lower left corner of the view-able screen. w is the width of the screen, h is the height.
if not GroundMap[math.floor( x / 64 )] then
GroudnMap[math.floor( x / 64 )] = math.floor( x / 64 ) * 64 - 64
elseif not GroundMap[math.ceil( x / 64 )] then
GroudnMap[math.ceil( x / 64 )] = math.ceil( x / 64 ) * 64 - 64
end
if not GroundMap[math.floor( ( x + w ) / 64 )] then
GroudnMap[math.floor( ( x + w ) / 64 )] = math.floor( ( x + w ) / 64 ) * 64 - 64
elseif not GroundMap[math.ceil( ( x + w )/ 64 )] then
GroudnMap[math.ceil( ( x + w ) / 64 )] = math.ceil( ( x + w ) / 64 ) * 64 - 64
end
end
By the way, I would recommend not having the variables so hard-coded. That's just me, though.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
function UpdateGround( x, y, w, h ) -- x and y are the lower left corner of the view-able screen. w is the width of the screen, h is the height.
if not GroundMap[math.floor( x / 64 )] then
GroudnMap[math.floor( x / 64 )] = math.floor( x / 64 ) * 64 - 64
elseif not GroundMap[math.ceil( x / 64 )] then
GroudnMap[math.ceil( x / 64 )] = math.ceil( x / 64 ) * 64 - 64
end
if not GroundMap[math.floor( ( x + w ) / 64 )] then
GroudnMap[math.floor( ( x + w ) / 64 )] = math.floor( ( x + w ) / 64 ) * 64 - 64
elseif not GroundMap[math.ceil( ( x + w )/ 64 )] then
GroudnMap[math.ceil( ( x + w ) / 64 )] = math.ceil( ( x + w ) / 64 ) * 64 - 64
end
end
By the way, I would recommend not having the variables so hard-coded. That's just me, though.
Cant seem to get this working - UpdateGround(0, 388, map_h, map_w)
You would need to use player.x - half the screen width, player.y - half the screen height. By the way, I can't test it since you're still using 0.8.0.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim