Trouble using Tween()?

function setup()
   function anim()
       spr1 = sprite("Cargo Bot:Codea Icon", 300, 200)
       tween(5, spr1, CurrentTouch.x)
       end
end

function draw()
    background(40,40,50)
    strokeWidth(5)
    anim()

end

Hey guys! I recently made a thread about animation issues, that’s when I first got Codea. Since then I decided the best way to learn was to dive in and try something crazy… and learn from it. I had issues with tween() in the start so I just did the usual ~~~ xPosition = xPosition + 1 ~~~ etc, etc, etc. But I figured if I was going to really implement animations tween was the way to go, since it’s meant to be like that.

Backstory out of the way, I can’t get this code to work. The error is that the subject must be a table or userdata. I am quite confused. I’ve tried C+Ping the example “Basic” for animations but it doesn’t work… I believe it’s dependent on other class files but I’m not sure what exactly. If I can’t animate sprites this way I’ll have a really hard time animating in general for a game or something. Help?

hello @treshure. there are a number of mistakes in your code, way to many for me to explain them all (sorry, i dont mean to be rude here, but it is true). I find it easier to show you a possible way to achieve what you mean to. study it for yourself, read the function documentation, and try to make changes ( one at a time). good luck.

function setup()
    pos ={x=300,y=300} -- the table that will hold the position and be changed by the tween function
end
function touched(t). -- use touched(), currentTouch is not so good.
    if t.state==BEGAN -- start a tween only when the touch began, not move or ended
    and not tw -- and make sure the tween is not running already
    then
        tw = tween(1, -- in 1s
            pos, {x=t.x, y=t.y}, -- change x,y fields of tabled pos to these values
            tween.easing.linear, -- there must be something here, so i put linear
            function() tw = nil end) -- delete tw when the tween is finished
    end
end
function draw()
    background(40,40,50)
    strokeWidth(5)
    sprite("Cargo Bot:Codea Icon", pos.x,pos.y)

end

ps: make sure you finish the current tween before you start another one.

Thank you so much for the response! I’m extremely new to Codea so I was expecting the wake up call sooner or later. My current knowledge stems from screwing around. I’ll definitely put this in my folder and study it hard for future endeavors.