Page 3 of 4

Re: tween.lua

Posted: Sun Nov 18, 2012 10:09 pm
by mangadrive
a) I was going to look up how to post the Love file and found out they were just glorified zip files, which lead me to understand that I could just rename the demo here and look at the source code.

b) I wasn't running a print on the variable. Even though I *did* try that at one point, it wasn't addressed properly.

So yes, my fault entirely, but for the benefit of others who might make the same assumptions, I just made here is a better example:

Code: Select all

local tween = require 'tween'


local label = { text = "Tween!", x = 200, y = -10 }





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

function  love.load()

--text "falls down" from the top of the screen
  tween(3, label, { y = 300 }, 'outBounce')

end

function love.draw()
  love.graphics.setColor(255, 255, 255)
  love.graphics.print(label.text, label.x, label.y)
end



end



Thank you for writing this wonderful library that will help me finish my game that might be sometime in 2049.

Re: tween.lua

Posted: Sun Nov 18, 2012 10:16 pm
by kikito
Yep, you are missing some stuff. At least 3 important things.

1. You are actually not drawing anything related with "label". If you want to see it on the screen, you have to print it somewhere!
2. The variable "label" is a local variable created inside love.draw. Local variables which are not referenced from outside or returned by their function get destroyed when the function is finished.
3. You are creating many tweeners per second. Let me explain this a bit more.

In LÖVE, the functions love.draw and love.update are called may times per second. Usually more than 30.

If you put this line inside love.draw:

Code: Select all

tween(4, label, { y=300 }, 'outBounce')
It gets executed many times per second. You just need to create one, and then let it "live" (update) until it expires.

In order to make your example work, you must conserve the label between love.draw invocations. This can be done by declaring the `label` variable outside of all functions.

You also want to create a single tweener, not one every frame . A good place to do that is love.load, since it get executed only once at the beginning of the game.

And of course, you need to actually print the label somewhere! :ultraglee:

This will give you this:

Code: Select all

local tween = require 'tween'
local label -- declare label here so it is available to all the functions below

function love.load()
  label = { x=200, y=0, text = "hello" }
  tween(4, label, { y=300 }, 'outBounce')
end

function love.draw()
  love.graphics.draw(label.text, label.x, label.y) -- print the label! This is very important! :D
end

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

Re: tween.lua

Posted: Sun Nov 18, 2012 10:22 pm
by mangadrive
Thank you for the post :) My post above is where I sorted out semi-on my own but your elaboration (and this noob moment on behalf/experience in general) helps me understand the game loop a bit better for Love :)

Normally I'd create functions to do this kind of thing on key hits or game logic but I was just trying to DO it first ;) New language, new API, new shoes = funny walking :D

Re: tween.lua

Posted: Thu Mar 28, 2013 2:33 pm
by JaseMourne
Hi, kiki. Thanks for a wonderful lib. Runs smoothly and exactly as one would expect.

I have a problem, though. I'm trying to apply more than one tweens on a single variable, which some other tween libs (MOAI for instance) allow and handle well, but tween.lua does not. I have a block that periodically falls down by 1 block size (tetris-like game), then stays for a while, then falls again. So I'm tweening its y variable. Sometimes I need the block to move upwards by 1 block size, but I still need the periodic tween to do its job. This is problematic, as two tweens on a single variable result in a quirky and unexpected movement.

Is there any way to achieve this? Can two tweens work on a single variable?

Here I post the love file. Line 201 is where we apply the first tween, 202 the second. What I expect this to do is for the face never to move a pixel. Yes, the example is nonsensical - it only serves as an example.

Re: tween.lua

Posted: Thu Mar 28, 2013 3:56 pm
by Roland_Yonaba
JaseMourne wrote:Hi, kiki.
Sorry. Couldn't resist. That's lövely.

Re: tween.lua

Posted: Thu Mar 28, 2013 7:33 pm
by LuaWeaver
mangadrive wrote:I guess I'm missing something very valuable here or my super_noob status is level 9k+

Code: Select all

local tween = require 'tween'


function love.draw()

love.graphics.setColor(255,255,255,255)
love.graphics.print("test",0,0)


local label = { x=200, y=0, text = "hello" }
tween(4, label, { y=300 }, 'outBounce')


end


function love.update(dt)
  
tween.update(dt)

end
Still just a black screen with "test". No tweens.
You're running it every frame, so it's stuck there restarting the tween every time (I would assume). Instead, call tween in love.load() like Kikito's example.

Re: tween.lua

Posted: Fri Mar 29, 2013 8:03 am
by kikito
Hi jasemourne,

I am on my mobile for 3 days and can't type much. For your case, I would use cron.lua to periodically move the cell down one bit with a funtion. If you want to use tween that way, you will have to cancel and create the teen every time you move things up.

Re: tween.lua

Posted: Fri Mar 29, 2013 4:34 pm
by JaseMourne
My problem is not timing the events, that's all handled fine. What I'm looking for is to be able to tween relatively to the current value of a variable. So for instance if I, at the same time, apply two identical tweens that move the y var of an entity by 64 pixels, the resulting motion will be 128 pixels. Or if one tween is 64 and the other is -64, the resulting motion is 0 pixels. Basically, I want to tween a variable relative to its starting (current) value and not absolutely.

I'm having a difficulty explaining what I want. Is this clearer?

Re: tween.lua

Posted: Sat Mar 30, 2013 11:17 pm
by kikito
I understood you the first time. Right now, given the way tween.lua is built, you can't use it to modify it in a variable that is already being modified by something else (another tween, or plain Lua). I mean, you can, but the last operation will "override" the previous one.

One thing you could do is use three variables: the one for the position, one for the "up position", and one for the "down position". Have the tweens modify those variables instead of the position directly, and then calculate the position based on the value of up and down. For example, the average:

Code: Select all

position = (upPosition + downPosition)/2

Re: tween.lua

Posted: Sun Mar 31, 2013 3:15 pm
by headchant
Did I ever say how awesome this library is?

Well: This library is awesome. Great work.