The online example of tween() can not run with the parameter "fillColor = color(255)"

When I try to run the example, I found that it can not run.

The error info is:

Error: [string "-- tween.lua ..."]: 133: attempt to perform arithmetic on local 'v' (a userdata value)

I tried the example on my two devices iPad with iOS8, mini with iOS7, the same problem.

If anyone found it before?

Here is the test code, copy from online help

-- TestProj

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    
    box = { x = 10, y = 10, width = 20, height = 20,fillColor = color(255) }
    
    -- tween(1, box, {x = 100, y = 100})
        
    tween(1, box,   {fillColor = color(255,0,0)})
 
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    rect(box.x,box.y,box.width,box.height)
    -- Do your drawing here
    
end

Only numbers can be tweened, not ‘userdata values’ such as colors. Therefore, you would have to tween the red, green, blue, and alpha values separately (in the target table).

It is possible, you just have to tweak the metadata values for the color class. Just copy and paste this code into your project:

mt = getmetatable(color())

mt.__add = function (c1, c2)
    return color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a)
end

mt.__sub = function (c1, c2)
    return color(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a)
end

mt.__mul = function (c, s)
    if type(c) ~= "userdata" then
        c, s = s, c
    end
    return color(c.r * s, c.g * s, c.b * s, c.a * s)
end

mt.__div = function (c, s)
    if type(c) ~= "userdata" then
        c, s = s, c
    end
    return color(c.r / s, c.g / s, c.b / s, c.a / s)
end

Thanks!

@Saturn031000 If the example code can not run, I think the online example should be modified, otherwise, beginners like me are very easy to be confused with the example. @Simeon

@Jordan When I add the ‘mt’ code , it is ok. It seems that you have showed a new way to change the Codea`s behavior from inside! Very good!

@binaryblues Here your code modified a little. Is this what you were trying to do.


-- TestProj

function setup()
    box = { x = 10, y = 10, width = 20, height = 20,fill(255,0,0) }
    tween(5, box, {x = 400, y = 400})
end

function draw()
    background(40, 40, 50)
    rect(box.x,box.y,box.width,box.height)
end

@dave1707 I am reading the tutorial “Codae for beginners” written by @Ignatz ,

and try all examples in it, my code is from here:

The tutorial is a good ebook, help me to understand the function tween() clearly. When I read the online help, the code is too simple to run; when I read the example project, it is so complex(mess the class,table together) and it is not good for beginners to understand the usage of tween().

@binaryblues Why that example is written like that will have to be answered by @Ignatz. I took your code above and tried to run it and got the same error. The fill command is used for color, so that’s what I tried and it worked.

I just copied the online help, will have to edit that

@dave1707 What my meaning is that the online help document code has some errors, and it maybe confuse the beginners, so we should correct it. It is a manual bug.

@Ignatz thanks for your tutorial! It is nice for beginners.

@binaryblues - thank you for the kind words

Another option you could use would be vec4’s. It’s works good and seems easier than editing metadata.

Support for using color objects in tween() will be available in the next version.