[Solved] Gridlocked player with smooth movement

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
Dzemal
Prole
Posts: 5
Joined: Tue Sep 27, 2016 2:59 pm

[Solved] Gridlocked player with smooth movement

Post by Dzemal »

Hey guys,

i followed this tutorial: https://www.love2d.org/wiki/Tutorial:Gridlocked_Player , but i can't get my program to work. I wanted to implement smooth movement but i get an error.(The error is attached)

Thanks for help in advance

Code: Select all

function love.load()
	player = {
		x = 256,
		y = 256,
		speed = 150
	}
	map = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
		{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
		{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    
	}
end
 
function love.update(dt)
  if love.keyboard.isDown("w") then
    if testMap(0, -1) then
      player.y = player.y - player.speed*dt
    end 
  end
  if love.keyboard.isDown("s") then
    if testMap(0, 1) then
      player.y = player.y + player.speed*dt
    end 
  end 
  if love.keyboard.isDown("a") then
    if testMap(-1, 0) then
      player.y = player.y - player.speed*dt
    end 
  end 
  if love.keyboard.isDown("d") then
    if testMap(1, 0) then
      player.y = player.y + player.speed*dt
    end 
  end 

end
 
function love.draw()
	love.graphics.rectangle("fill", player.x, player.y, 32, 32)
	for y=1, #map do
		for x=1, #map[y] do
			if map[y][x] == 1 then
				love.graphics.rectangle("line", (x-1) * 32, (y-1) * 32, 32, 32)
			end
		end
	end
end
 
 
function testMap(x, y)
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x]   == 1 then
		return false
	end
	return true
end
Attachments
Screenshot_1.png
Screenshot_1.png (15.88 KiB) Viewed 1948 times
Last edited by Dzemal on Wed Apr 12, 2017 5:47 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Gridlocked player with smooth movement

Post by zorg »

Don't just copy code and expect it to run; think, people! :awesome:

The tutorial has grid_y and grid_x fields defined inside the table called player, you didn't write them in.

Code: Select all

	player = {
		grid_x = 256, -- You're missing these two.
		grid_y = 256,
		act_x = 200,   -- these two are missing as well, but since your code didn't use them, whatever.
		act_y = 200,
		speed = 10
	}
But glancing at your code again, maybe changing those to just player.x and player.y will work.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests