Drawing a character over a tileset

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
Mendokuse
Prole
Posts: 4
Joined: Sat Mar 26, 2011 3:53 am

Drawing a character over a tileset

Post by Mendokuse »

Hi.

I followed the first section of this tutorial:

https://github.com/kikito/love-tile-tutorial/wiki

And everything was fine, so I decided to try and make a character move on the screen (move block by block) but I can't figure out how to get it done.

So far I have this:

A drawCharacter() function that runs after drawMap() in main

Code: Select all

function drawCharacter()

	if love.keyboard.isDown("up") then
	print("up")
		for rowIndex = 1, #tileTable do
			local row = tileTable[rowIndex]
				for columnIndex = 1, #row do
					local char = row[columnIndex]
					if tileTable[rowIndex][columnIndex] == 'C' then
						print("C")
						--local x = (columnIndex)*tileW
						--local y = (rowIndex)*tileH
						love.graphics.drawq(tileset, quads['C'], (columnIndex-2)*tileW, (rowIndex-1)*tileH)
						end
					end
			end
	end
end
My thought process here is to make a function that reads where the character is currently at (C) and then draws the character ('C' in quad) at a block that's one above (columnIndex-2).

My problem is that nothing changes on the screen when I press up. The console print commands work fine.

Anyone have any ideas?
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Drawing a character over a tileset

Post by Ensayia »

An immediate problem that I see: You can only call love.graphics function from within love.draw().

Is this function run in love.draw()?

EDIT: Looking again I see you are checking the keyboard state, you will likely need to do this every frame. I would assign the function to a variable and call it in love.update() so that any function can access the keyboard state. Example:

Code: Select all

k_down = love.keyboard.isDown("down") -- assuming down arrow in this case
Then:

Code: Select all

if k_down then
     -- do stuff
end
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Drawing a character over a tileset

Post by BlackBulletIV »

Ensayia wrote:You can only call love.graphics function from within love.draw().
That's not the complete story. You can only call love.graphics functions that actually draw something within love.draw(). This is because of this code inside love.run():

Code: Select all

if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
if love.graphics then
    love.graphics.clear()
    if love.draw then love.draw() end
end
love.update() is called, then everything on-screen is cleared. Then everything is re-drawn. This is why you can't draw anything inside love.update()*.

*Unless of course you redefine love.run(). But drawing in love.update() is a very bad practice.
Mendokuse
Prole
Posts: 4
Joined: Sat Mar 26, 2011 3:53 am

Re: Drawing a character over a tileset

Post by Mendokuse »

I decided to change it about and now I'm using two layers, the second layer has the character on it and blank (transparent) tiles for everything else.

However, when I run the code, I only see layer 2 (it's drawn after) and the blank tile spots are just black (which is the LOVE2D background because Layer 1 is no longer being drawn)

Anyone know how I could correct this?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests