for some reason my collision detection won't work very well. I followed the steps in this thread: http://love2d.org/forums/viewtopic.php?f=4&t=2096
It does stop my player, but not when bumping into tiles but instead at random (probably not-so-random) places. Would anyone be kind enough to take a look at my code? My main.lua:
Code: Select all
require "LICK"
function love.load()
love.graphics.setMode(640,480,false,true,30);
love.graphics.setCaption("LOL");
love.graphics.setFont(14)
text = "Alien";
player_img = love.graphics.newImage("img/NEUTRAL_ALIEN.png");
bgimg = love.graphics.newImage("img/ye.png");
player = {}
player.x = 50
player.y = 361
player.speed = 200
-- MAP --
love.graphics.setFont(12)
-- map-variabler
map_w = 640
map_h = 480
tile_w = 32
tile_h = 32
map={
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 4, 4, 4, 4, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
map_display_w = #map[1]
map_display_h = #map
--tiles
tile = {}
for i=0,4 do
tile[i] = love.graphics.newImage( "img/tiles/tile"..i..".png" )
end
end
function map2world(mx,my)
return mx*tile_w, my*tile_h
end
function world2map(x,y)
return math.floor(x/tile_w), math.floor(y/tile_h)
end
function draw_map(mx, my)
for y=1, map_display_h do
for x=1, map_display_w do
love.graphics.draw(tile[ map[y] [x] ], map2world((x-1), (y-1)))
end
end
end
-- SLUT PÅ MAP
function love.update(dt)
fps = love.timer.getFPS()
local tileX, tileY = world2map( player.x, player.y )
local leftTileX, leftTileY = tileX - 1, tileY
local leftTileNo = map[leftTileY][leftTileX]
local rightTileX, rightTileY = tileX - 1, tileY
local rightTileNo = map[rightTileY][rightTileX]
if love.keyboard.isDown("left") and leftTileNo == 0 then
player.x = player.x - player.speed*dt
player_img = love.graphics.newImage("img/ALIENWHEELSHIT.png");
elseif love.keyboard.isDown("right") then
player.x = player.x + player.speed*dt
player_img = love.graphics.newImage("img/ALIENWHEELSHIT2.png");
else
player_img = love.graphics.newImage("img/NEUTRAL_ALIEN.png");
end
end
function love.draw()
love.graphics.print("FPS: "..fps, 40, 40)
draw_map()
love.graphics.draw(player_img,player.x,player.y,0,1,1,0,0)
love.graphics.print(text, player.x + 15, player.y - 30);
end