Re: Simple Tiled Implementation - STI v0.7.4
Posted: Mon May 19, 2014 12:30 am
Join the #love IRC channel and pm me (Karai)
Code: Select all
local sti = require "Libs.STI"
function love.load()
...
level.load(sti)
...
end
function love.update(dt)
...
level.update(dt)
...
end
function level.draw()
...
level.draw()
...
end
function level.load(sti)
...
map = sti.new(maps/level1)
end
function level.update(dt)
...
map.update(dt)
end
function level.draw()
map:draw()
...
end
i'm checking for collisions like this, but whenever i go to move my player, i get an error that looks like this -Karai17 wrote:Code: Select all
local sti = require "sti" function love.load() map = sti.new("map") collision = map:getCollisionMap("Collision Layer") player = { x = 24, y = 36 } -- player is standing on the 24,36 block of the map end function love.keypressed(key, isrepeat) if key == "up" then if collision.data[player.y - 1][player.x] ~= 1 then -- the block above us is not collidable, so we can walk there! player.y = player.y - 1 end return end if key == "down" then if collision.data[player.y + 1][player.x] ~= 1 then -- the block below us is not collidable, so we can walk there! player.y = player.y + 1 end return end if key == "left" then if collision.data[player.y][player.x - 1] ~= 1 then -- the block to the left of us is not collidable, so we can walk there! player.x = player.x - 1 end return end if key == "right" then if collision.data[player.y][player.x + 1] ~= 1 then -- the block to the right of us is not collidable, so we can walk there! player.x = player.x + 1 end return end end
Code: Select all
function bolo_load()
img = love.graphics.newImage("gfx/bolo.png")
img:setFilter("nearest")
anim = newAnimation(img, 34, 38, 0, 0)
x = 0
y = 0
spd = 90
dir = 1
grassmap = sti.new("maps/grass")
grasscol = grassmap:getCollisionMap("collisions")
end
function bolo_update(dt)
if love.keyboard.isDown("up") and grasscol.data[y - 1][x] ~= 1 then
y = y - spd * dt
end
if love.keyboard.isDown("down") and grasscol.data[y + 1][x] ~= 1 then
y = y + spd * dt
end
if love.keyboard.isDown("left") and grasscol.data[y][x - 1] ~= 1 then
x = x - spd * dt
dir = -1
end
if love.keyboard.isDown("right") and grasscol.data[y][x + 1] ~= 1 then
x = x + spd * dt
dir = 1
end
end
Karai17 wrote:You are having scope issues. You are using global x and y values for both you rmap indices and your movement speed.
Code: Select all
function bolo_load()
img = love.graphics.newImage("gfx/bolo.png")
img:setFilter("nearest")
anim = newAnimation(img, 34, 38, 0, 0)
bolo = {
x = 40,
y = 40
}
spd = 90
dir = 1
grassmap = sti.new("maps/grass")
grasscol = grassmap:getCollisionMap("collisions")
end
function bolo_update(dt)
if love.keyboard.isDown("up") and grasscol.data[bolo.y - 1][bolo.x] ~= 1 then
bolo.y = bolo.y - spd * dt
end
if love.keyboard.isDown("down") and grasscol.data[bolo.y + 1][bolo.x] ~= 1 then
bolo.y = bolo.y + spd * dt
end
if love.keyboard.isDown("left") and grasscol.data[bolo.y][bolo.x - 1] ~= 1 then
bolo.x = bolo.x - spd * dt
dir = -1
end
if love.keyboard.isDown("right") and grasscol.data[bolo.y][bolo.x + 1] ~= 1 then
bolo.x = bolo.x + spd * dt
dir = 1
end
end
sorry, i'm somewhat new to lua so i'm kind of unclear on how to fix this (i came from a GML background). i have what you have in terms of movement (bolo.x = bolo.x + spd) so i'm unsure of what to do next.Karai17 wrote:Yes. bolo.x = bolo.x + spd * dt is the problem. You are changing x and y to non-indexed numbers.