AdvTiledLoader layer collision

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
goosingout
Prole
Posts: 7
Joined: Thu Feb 27, 2014 10:42 pm

AdvTiledLoader layer collision

Post by goosingout »

I am trying to get my character to collide with the tiled map that has been implemented into the game. for example when I jump it does fall through the solid layer.

Code: Select all

Gamestate.level1 = Gamestate.new( )
local state = Gamestate.level1

loader = require("AdvTiledLoader.Loader")
HC = require("HardonCollider")

function state:init( )
	windowx = love.graphics.getWidth()
	
	bg1 = {}
	bg1.img = love.graphics.newImage("/images/Citybackgroundnight.png")
	bg1.x = 0
	bg1.width = bg1.img:getWidth()

	bg2 = {}
	bg2.img = love.graphics.newImage("/images/Citybackgroundnight.png")
	bg2.x = windowx
	bg2.width = bg2.img:getWidth()
	
	tx = 0
	ty = 0
	loader.path = "maps/"
	map = loader.load("level1.tmx")
	map:setDrawRange (tx,ty, map.width * map.tileWidth, map.height * map.tileHeight)

	player = {
        x = 0,
        y = 0,
		y_velocity = 0,
        ninja = love.graphics.newImage("/images/Character1.png") 
		}

	speed = 200
	gravity = 400
    jump_height = 350
	
    winW, winH = love.graphics.getWidth(), love.graphics.getHeight()
end

function state:update( dt )
	if dt > 0.05 then
		dt = 0.05
	end

	bg1.x = bg1.x - speed * dt
	bg2.x = bg2.x - speed * dt
	
	if bg1.x < -800 then
		bg1.x = bg1.x + bg1.width
	end
	if bg2.x < 0 then
		bg2.x = bg2.x + bg2.width
	end

	if player.y_velocity ~= 0 then
        player.y = player.y + player.y_velocity * dt
        player.y_velocity = player.y_velocity - gravity * dt	
    end	
	
	tx = tx - (speed * dt)

	function love.keypressed(key)
		if key == "right" then
			speed = 400
		elseif key == "left" then
			speed = 50
		elseif key == "up"   then
			if player.y_velocity == 0 then
				player.y_velocity = jump_height
			end
		end
	end	
	
	function love.keyreleased(key)
		if key == "left" then
			speed  = 200
		elseif key == "right" then
			speed = 200
		end
	end
	
end

function state:draw( )
love.graphics.setColor(255,255,255,255)
	love.graphics.draw(bg1.img, bg1.x, 0)
	love.graphics.draw(bg2.img, bg2.x, 0)
	
	love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 10, 10)
	
	local ftx, fty = math.floor(tx), math.floor(ty)
	love.graphics.push()
	love.graphics.scale(scale)
	love.graphics.translate(ftx, fty)
	map:draw()
	love.graphics.pop()
	
    love.graphics.translate(30, 360)
    love.graphics.draw(player.ninja, player.x, -player.y, 0, 1, 1, 1, 50)
end
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: AdvTiledLoader layer collision

Post by SneakySnake »

Neither Advanced Tiled Loader, nor HardonCollider does any collision handling for you.
HardonCollider only lets you detect collisions between shapes, but you have to handle them.

Here is a tutorial for creating a platformer using Advanced Tiled Loader and HardonCollider: http://www.headchant.com/2012/01/06/tut ... ve-part-1/

It might be a bit outdated though.
Post Reply

Who is online

Users browsing this forum: deedubya, Google [Bot] and 6 guests