fade using tween

tween semesterön really Nicke but i havent figurer out quite how they work yet. been looking around the forum and found some code which i got inspired by. howver, it’s not working ad i expect it to do. probably because i dont fully understand tweens.

im trying to fade out a colored rect() by changing the alpha value of the fill color for the rect(). in My main i have

-- set alpha for score fx
    brushAlpha = {alpha = 255}
    -- colors
    colors = {
        color(255, 0, 0, brushAlpha.alpha),    -- red
}

then in my run time code i call scoreFx() at given points and would expect the rect() to fade out but it stays solid.

function Brush:scoreFx()
    tween(1, brushAlpha,{alpha = 0})
end

any ideas?

Do you fill the rect with colors[1] ? If so You need to update that every frame. So you can do:

colors[1]=color(255,0,0,brushAlpha.alpha)--update the color
fill(colors[1])
rect()--draw the rect

@coder thanks. i thought that was what i was doing with this code?

draw()
for i=1,6 do
        for j=1, 6 do
            palette[i][j]:draw()
            fill(255,255,255)
            local x = palette[i][j].pos.x
            local y = palette[i][j].pos.y
            --text(palette[i][j].ind, x, y)
        end
    end

and

function Brush:draw()
    pushStyle()
    fill(self.col)
    rectMode(CENTER)
    rect(self.pos.x, self.pos.y, self.size, self.size)
    popStyle()
end

doing a print() of brushAlpha.alpha show that the alpha value nevet chages. and thats’s were im not sure what i am doing :slight_smile:

try fill(self.col.r, self.col.g, self.col.b, brushAlpha.alpha)

@fikabord Here’s an example.


function setup()
    r={x=WIDTH/2,y=HEIGHT/2,alpha=255}
    tween(5,r,{alpha=0},{easing=tween.easing.linear,loop=tween.loop.pingpong})
end
function draw()
    background(0)
    fill(255,0,0,r.alpha)
    rect(r.x,r.y,200,200)
end

@jakattak @dave1707 thanks works like a charm!

im however, not sure i understand the difference :slight_smile:

doesnt self.col contain the same info?