Timer Help

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.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Timer Help

Post by coke905 »

K so i understand how to do quads no and i have right_1 - right_4 as quads with each individual image,

img = love.graphics.newImage("Images/Player_Sprite_Sheet.png") # my spritesheet

right_1 = love.graphics.newQuad(0,0,39,64, 156, 128) # first pic
right_2 = love.graphics.newQuad(39,0,39,64, 156, 128) # second pic
right_3 = love.graphics.newQuad(78,0,39,64, 156, 128) # third pic
right_4 = love.graphics.newQuad(117,0,39,64, 156, 128) # fourth pic

It'll be easy to change the image but how would i set a 'timer' or something so that it changes the picture every 2 seconds or something.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Timer Help

Post by Nixola »

First of all, don't use right_1, right_2 and so. You can create a table and then put in there your quads:

Code: Select all

right = {} --create a table
right[1] = love.graphics.newQuad(0,0,39,64, 156, 128) --first pic
right[2] = love.graphics.newQuad(39,0,39,64, 156, 128) --second pic
right[3] = love.graphics.newQuad(78,0,39,64, 156, 128) --third pic
right[4] = love.graphics.newQuad(117,0,39,64, 156, 128) --fourth pic
Then you create a timer and some variables:

Code: Select all

--in love.load()
timer = 0
frames_number = 4
frames_interval = 2 --in seconds

--in love.update()
timer = timer + dt/frames_interval --You add the time passed between the last 2 frames every frame, dividing it by the interval so you'll have the timer increasing by one every time the animation has to change frame

if math.floor(timer) >= frames_number then --math.floor rounds ¡down! a number to the nearest integer
    timer = timer - frames_number
end --we're having this "if" so timer doesn't exceed the frames number


--in love.draw()
love.graphics.drawq(tileset, right[math.floor(timer)], x, y) --This line simply draws the current frame, rounding down "timer" because otherwise we'd try to draw a non-existant quad.
That should be it, I didn't test it though. Do you know how do lua tables work?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Timer Help

Post by coke905 »

sorta i think its like for i,v in ipairs(anim) do

end



but im not REALLY familair and im confused why you dont set i = 0 because every forloop ive used its for i =0, 10 or whatever i want to set i to.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Timer Help

Post by coke905 »

im getting an error in the draw function
i dont know much about tables so i dont know how to fix it

Code: Select all


function love.load()
	img = love.graphics.newImage("Images/Player_Sprite_Sheet.png")
	
	PlayerX = 150
	PlayerY = 500
	
	anim = {}
	anim[1] = love.graphics.newQuad(0,0,39,64, 156, 128)
	anim[2] = love.graphics.newQuad(39,0,39,64, 156, 128)
	anim[3] = love.graphics.newQuad(78,0,39,64, 156, 128)
	anim[4] = love.graphics.newQuad(117,0,39,64, 156, 128)
	
	
	timer = 0
	frames_number = 4
	frames_interval = 2 -- seconds
end

function love.update(dt)
	timer = timer + dt/frames_interval 
	if math.floor(timer) >= frames_number then 
		timer = timer - frames_number
	end
	
end

function love.mousepressed()

end

function love.draw()
	love.graphics.drawq(img, anim[math.floor(timer)], PlayerX, PlayerY)
end

coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Timer Help

Post by coke905 »

Extremely sorry for triple posting but i realized the problem in the code that you gave me, it was the array with my background in c++ i realized that arrays start at zero so i just had to switch it to 0 1 2 3 thanks btw im extremely grateful.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Timer Help

Post by coke905 »

K my tileset has right and left images on it 4 frames, i have the quads laid out for me, but now how would i make it so if i press "Left" it sorta sets the timer to start at the 5th element at the table, i have right movement working just like you wrote, but its hard when the left images are the "end" of the table. I could do it by making a new table but id like to know if i could do it without making a new table

Code: Select all


function love.load()
	tileset = love.graphics.newImage("Images/Player_Sprite_Sheet.png")
	
	PlayerX = 150
	PlayerY = 500
	PlayerSpeed = 120
	right = {}
	right[0] = love.graphics.newQuad(0,0,39,64, 156, 128)
	right[1] = love.graphics.newQuad(39,0,39,64, 156, 128)
	right[2] = love.graphics.newQuad(78,0,39,64, 156, 128)
	right[3] = love.graphics.newQuad(117,0,39,64, 156, 128)
	
	right[4] = love.graphics.newQuad(0, 64, 39, 64, 156, 128)
	right[5] = love.graphics.newQuad(39, 64, 39, 64, 156, 128)
	right[6] = love.graphics.newQuad(78, 64, 39, 64, 156, 128)
	right[7] = love.graphics.newQuad(117, 64, 39, 64, 156, 128)

	timer = 0
	frames_number = 4
	frames_interval = 0.06 -- seconds
	
end

function love.update(dt)
	if love.keyboard.isDown("right") then
		PlayerX = PlayerX + PlayerSpeed * dt
		
		timer = timer + dt/frames_interval 
		if math.floor(timer) == frames_number then 
			timer = timer - frames_number
		end
	elseif love.keyboard.isDown("left") then
		PlayerX = PlayerX - PlayerSpeed * dt

		timer = timer + dt/frames_interval
		if math.floor(timer) >= frames_number then
			timer = timer - frames_number
		end
	end
	
end

function love.mousepressed()

end

function love.draw()
	love.graphics.drawq(tileset, right[math.floor(timer)], PlayerX, PlayerY)
end
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Timer Help

Post by coffee »

Really, what's your problem? Four posts in-a-row should be probably this forum record! Can't you remember this?
Roland_Yonaba wrote:You made 3 posts in a row. Use Edit button, instead of making tons of posts in a row.
This is not your blog, a chat or a IRC room (and even there you usually can't msg repeatedly so many times in a row).
EDIT BUTTON,
EDIT BUTTON,
EDIT BUTTON,
EDIT BUTTON!
Dude you surely can make someone lost the spirit of want to help you...
coke905 wrote:i dont know much about tables so i dont know how to fix it
Basically then it's almost worthless keep doing things with Lua since you will forever mega-posting for every problem you get.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Timer Help

Post by Robin »

I suggest you read Chapter 11 of Programming in Lua very carefully and try every example they give (http://www.lua.org/pil/11.html).
Help us help you: attach a .love.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Timer Help

Post by coke905 »

LOL my guide i didn't know that there is an EDIT button well they will be one post for now on.

My problem is that i have 2 directions in my sprite sheet. starting to the right then 64 pixels down it starts to the left. Im wondering if i need a table per direction then just put an if statement in the draw method or can i just set the timer to where the animation starts

right = {}
right[0] = image0
right[1] = image1
right[2] = image2
right[3] = image3

-- Now the animation starts going to the left
right[4] = imageLeft
right[5] = imageLeft
right[6] = imageLeft
right[7] = imageleft

the images arnt actually images i just put a name for it. But now when i press the Left key how would i sorta set the timer to the left animation so right[4] and if the key is still being pressed and timer > 7 then timer = 4 again, i tried this but i got errors i probably did it wrong.

then again im extremely sorry i didnt know there was a edit button
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Timer Help

Post by Nixola »

You should use two tables, it would be waay simpler.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 5 guests