Page 1 of 1

love.timer.sleep

Posted: Sun Mar 02, 2014 12:08 am
by Findo777
function attack(s)
debounce = false
bulletxpos = xpos
bulletypos = ypos
love.graphics.setColor(255,255,255)
love.timer.sleep(s * 1)
Bullet = love.graphics.draw(bullet,bulletxpos,bulletypos - less)
love.timer.sleep(s * 1)
EXP1 = love.graphics.draw(exp1,bulletxpos,bulletypos - less)
love.timer.sleep(s * 1)
EXP2 = love.graphics.draw(exp2,bulletxpos,bulletypos - less)
love.timer.sleep(s * 1)
EXP3 = love.graphics.draw(exp3,bulletxpos,bulletypos - less)
space_pressed = false
debounce = true
end


I call it with s being 1


Problem: instead of animating then waiting 1 second, then animating more, it just waits 4 seconds then zooms through the animation/
It also pauses all the scripts, not just this function
What is the problem? :(


I think you may have to use delta time.
If you know what to do, just please write out my script in that way and tell me which function to put it in,
I have been confused on this for days.

Re: love.timer.sleep

Posted: Sun Mar 02, 2014 2:43 am
by verilog
Questions of this nature should be posted in the Support and Development section. Anyway, in what part of the game loop are you calling the attack(s) function? Perhaps inside the draw function? I'm not entirely sure what are you trying to accomplish, but It seems that you need to read how löve works: here's an excellent overview by the great kikito that should give insight of how the framework works and how to set-up a simple game loop.

As for the love.timer.sleep halting the current thread activity, well, it is working as intended. From the wiki: [love.timer.sleep] pauses the current thread for the specified amount of time. If you're running your code in just one thread, it will completely pause the main loop. If you're trying to animate something, an animation is usually rendered one frame per x amount of time (usually [milli]seconds) within the love.draw() function, no need to use love.timer.sleep.

Now, if you are trying to update an image's position (x and y), and generally updating the state of your simulation, that's accomplished inside the love.update(dt) function:

Code: Select all

function love.update(dt)
 bullet.x =  bullet.x + bullet.vx * dt -- horizontal position
 bullet.y =  bullet.y + bullet.vy * dt -- vertical position
end
Next, you just render the image according to the new position (that gets updated every frame):

Code: Select all

function love.draw()
 love.graphics.draw(bullet.img, bullet.x, bullet.y) -- remember to load the resources first! (see love.load())
end
Please, do not forget to use the

Code: Select all

[/b] tags next when referring to code. Even better, how about posting the complete love file?

Re: love.timer.sleep

Posted: Sun Mar 02, 2014 3:03 am
by davisdude
I'm not sure how to do it using love.timer.sleep, but I have an animation utility you may use if you'd like. :)

Re: love.timer.sleep

Posted: Sun Mar 02, 2014 3:37 am
by Findo777
I am calling the attack() in a draw function because it draws, anyhow,
could you show me how to add...like... .5 a wait before each animation?
I do not need it to move, just to animate, (its an explosion).

Thanks for your help, and sorry for using the wrong forum.

Re: love.timer.sleep

Posted: Sun Mar 02, 2014 3:48 am
by davisdude
Using my utility, (linked above), you do this:

Code: Select all

Animation = require( 'Animation' )

function love.load() 
	local BallImage = love.graphics.newImage( 'Animation.png' )
	local BallImageWidth = BallImage:getWidth()
	local BallImageHeight = BallImage:getHeight()
	local BallQuad = {
		love.graphics.newQuad( 0, 0, 82, 70, BallImageWidth, BallImageHeight ), 
		love.graphics.newQuad( 82, 0, 82, 70, BallImageWidth, BallImageHeight ), 
		love.graphics.newQuad( 164, 0, 82, 70, BallImageWidth, BallImageHeight ), 
		love.graphics.newQuad( 0, 82, 82, 70, BallImageWidth, BallImageHeight ), 
		love.graphics.newQuad( 82, 82, 82, 70, BallImageWidth, BallImageHeight ), 
		love.graphics.newQuad( 164, 82, 82, 70, BallImageWidth, BallImageHeight ), 
	}
	Ball = Animation.New( BallImage, {
		Bouncing = {
			Delay = .5, -- This is half a second. Change it to meet your needs. 
			unpack( BallQuad ),
		}
	} )
end

function love.draw() 
	Ball['Bouncing']:Draw()
end

function love.update( dt ) 	
	Ball['Bouncing']:Update( dt )
end

Re: love.timer.sleep

Posted: Sun Mar 02, 2014 5:25 am
by Findo777
uh..... I don't see any of my explosion variables there

Re: love.timer.sleep

Posted: Mon Mar 03, 2014 3:09 am
by davisdude

Code: Select all

Animation = require 'Animation'

bullet = love.graphics.newImage( 'bullet' ) -- Other picture files, etc. 
-- Other pictures and other variables. 
BulletShouldAnimate = false

function love.load()
	Bullet = Animation.New{
		exp1, 
		exp2, 
		exp3, 
		Delay = .5, -- Whatever the delay between pictures is. 
	}
end

-- Other code

function attack(s)
	debounce = false
	bulletxpos = xpos
	bulletypos = ypos
	love.graphics.setColor(255,255,255)
	love.timer.sleep(s * 1)
	Bullet = love.graphics.draw(bullet,bulletxpos,bulletypos - less)
	love.timer.sleep(s * 1)
	EXP1 = love.graphics.draw(exp1,bulletxpos,bulletypos - less)
	love.timer.sleep(s * 1)
	EXP2 = love.graphics.draw(exp2,bulletxpos,bulletypos - less)
	love.timer.sleep(s * 1)
	EXP3 = love.graphics.draw(exp3,bulletxpos,bulletypos - less)
	space_pressed = false
	debounce = true
	
	expShouldAnimate = true
end
Two things:
1. This would be a lot easier if you gave us the whole code.
2. No offense, but you could have actually tried to do it yourself. It's a good way to learn. :)

Re: love.timer.sleep

Posted: Mon Mar 03, 2014 4:40 pm
by Jeeper
I belive he missunderstands what love.timer.sleep is used for... Someone should add a huge red text on the wiki saying that it is NOT used as a timer.

Re: love.timer.sleep

Posted: Tue Mar 04, 2014 5:33 am
by Findo777
Jeeper wrote:I belive he missunderstands what love.timer.sleep is used for... Someone should add a huge red text on the wiki saying that it is NOT used as a timer.

Hey uh, I have another post called "Delay on Loop" and I could reallly use some help on that.. thanks
I realized .sleep stops the whole thing, so I'm going to stick with dt