tween.lua

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: tween.lua

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: tween.lua

Post 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
When I write def I mean function.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: tween.lua

Post 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
JaseMourne
Prole
Posts: 13
Joined: Sat Aug 11, 2012 7:37 pm

Re: tween.lua

Post 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.
Attachments
Game.love
(216.09 KiB) Downloaded 189 times
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: tween.lua

Post by Roland_Yonaba »

JaseMourne wrote:Hi, kiki.
Sorry. Couldn't resist. That's lövely.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: tween.lua

Post 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.
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: tween.lua

Post 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.
When I write def I mean function.
JaseMourne
Prole
Posts: 13
Joined: Sat Aug 11, 2012 7:37 pm

Re: tween.lua

Post 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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: tween.lua

Post 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
Last edited by kikito on Mon Apr 01, 2013 3:26 pm, edited 3 times in total.
When I write def I mean function.
User avatar
headchant
Party member
Posts: 105
Joined: Fri Sep 03, 2010 12:39 pm
Contact:

Re: tween.lua

Post by headchant »

Did I ever say how awesome this library is?

Well: This library is awesome. Great work.
Post Reply

Who is online

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