Conditional tweening confusion

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
blur_like_sotong
Prole
Posts: 2
Joined: Sun Sep 02, 2018 6:40 pm

Conditional tweening confusion

Post by blur_like_sotong »

I apparently have a fundamental misunderstanding of how updating works during a tween. My goal is to have a draw function that activates a tween (color fade) when a key is pressed. I'm using the HUMP library, following this documentation of the tween function:

hump_tween.png
hump_tween.png (114.32 KiB) Viewed 8155 times

Below is the latest code I've tried. However, after pressing the key ('a'), instead of setting the background to green and fading to black over 10 seconds, the background fades between green and black about 1/s over the 10 seconds. Also, when it completes, I am unable to activate the tween again. Alternatively, if I place the same Timer.tween call within the love.load() function and press ('a'), the fade occurs slowly, as intended, but begins at an intermediate time within the 10 seconds (ie, the later I push the key, the more faded the starting point). Again, this cannot be reactivated with another key press.

Code: Select all

Timer = require 'hump.timer'

function love.load()
    color = {0, 255, 0}      
end

function love.update(dt)
    Timer.update(dt)
end

function love.keypressed(key)
    if key == 'escape' then love.event.quit() 
    elseif key == 'a' then fade = true
    end
end

function love.draw()
    love.graphics.setBackgroundColor({0, 0, 0})
    if fade == true then
        Timer.tween(10, color, {0, 0, 0}, 'linear', function() fade = false end) 
        love.graphics.setBackgroundColor(color)
    end
end
I feel I am missing a very basic concept. Given the behavior that I observed, I first assumed that it was a misunderstanding of the Timer.update(dt). But related forum posts have me more confused (note: Timer.after is a similar function in the HUMP library, but the first argument being delay rather than duration). From viewtopic.php?t=82424 :
palmettos wrote: Fri Jul 08, 2016 2:32 pm The number argument to Timer.after is the number of seconds Timer waits to call the function argument after Timer.after is called. At what time the entire love program starts is irrelevant.

Say you put Timer.after(3, function() print('hello') end) in the love.mousepressed callback. Anytime the user presses a mouse button, 'hello' will print to the console 3 seconds later.
and...
vrld wrote: Fri Jul 08, 2016 2:33 pm You do misunderstand. I's the time in seconds after which the function will be called, counted from the call to Timer.after. The ... code will start the tween three seconds after the call, not three seconds after the game has started.
Any clarity here is greatly appreciated. Thanks!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Conditional tweening confusion

Post by bartbes »

The exact symptoms you're describing are.. weird, but I'm fairly sure I know what the issue is: you're creating a new tween every frame.

You probably want something like this:

Code: Select all

Timer = require 'hump.timer'

function love.load()
    color = {0, 255, 0}      
end

function love.update(dt)
    Timer.update(dt)
end

function love.keypressed(key)
    if key == 'escape' then love.event.quit() 
    elseif key == 'a' then fade = true
        Timer.tween(10, color, {0, 0, 0}, 'linear', function() fade = false end)
    end
end

function love.draw()
    love.graphics.setBackgroundColor({0, 0, 0})
    if fade == true then
        love.graphics.setBackgroundColor(color)
    end
end
Optionally with some more code to prevent two tweens from running when you press a again.
And if I'm making suggestions anyway, you don't need the 'fade' variable at all if you just make sure 'color' is always set to the right values.
blur_like_sotong
Prole
Posts: 2
Joined: Sun Sep 02, 2018 6:40 pm

Re: Conditional tweening confusion

Post by blur_like_sotong »

Thank you, bartbes, you're right. The issue was calling the tween in every frame in love.draw (or only at the start of the script in love.load). The corrected code you wrote works the way I was hoping.

Thanks again!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 54 guests