Jump to content

animate function


Recommended Posts

addEventHandler("onClientRender", root,
    function()
		x = animate(0, 364, "Linear", 3000, nil)
        dxDrawRectangle(x, 183, 550, 380, tocolor(0, 0, 0, 156), false)
        dxDrawRectangle(364, 563, 550, 4, tocolor(237, 171, 123, 156), false)
        dxDrawRectangle(440, 295, 390, 37, tocolor(216, 213, 213, 254), false)
        dxDrawRectangle(440, 373, 390, 37, tocolor(216, 213, 213, 254), false)
    end
)

Using this - https://wiki.multitheftauto.com/wiki/Animate

It says, expected function at argument 5 got nil, does it have to be a function, all I want to do is move it left to right using this function.

Update - 

Okay I've done something like this, it works but, the animation isn't completing, it just stops half way -

addEventHandler("onClientRender", root,
    function()
		x = animate(0, 364, "Linear", 2000, function() end)
        dxDrawRectangle(x, 183, 550, 380, tocolor(0, 0, 0, 156), false)
        dxDrawRectangle(364, 563, 550, 4, tocolor(237, 171, 123, 156), false)
        dxDrawRectangle(440, 295, 390, 37, tocolor(216, 213, 213, 254), false)
        dxDrawRectangle(440, 373, 390, 37, tocolor(216, 213, 213, 254), false)
end)

 

Edited by raysmta
Link to comment
local anims, builtins = {}, {"Linear", "InQuad", "OutQuad", "InOutQuad", "OutInQuad", "InElastic", "OutElastic", "InOutElastic", "OutInElastic", "InBack", "OutBack", "InOutBack", "OutInBack", "InBounce", "OutBounce", "InOutBounce", "OutInBounce", "SineCurve", "CosineCurve"}

function table.find(t, v)
    for k, a in ipairs(t) do
        if a == v then
            return k
        end
    end
    return false
end

function animate(f, t, easing, duration, onChange, onEnd)
    assert(type(f) == "number", "Bad argument @ 'animate' [expected number at argument 1, got "..type(f).."]")
    assert(type(t) == "number", "Bad argument @ 'animate' [expected number at argument 2, got "..type(t).."]")
    assert(type(easing) == "string" or (type(easing) == "number" and (easing >= 1 or easing <= #builtins)), "Bad argument @ 'animate' [Invalid easing at argument 3]")
    assert(type(duration) == "number", "Bad argument @ 'animate' [expected function at argument 4, got "..type(duration).."]")
    assert(type(onChange) == "function", "Bad argument @ 'animate' [expected function at argument 5, got "..type(onChange).."]")
    table.insert(anims, {from = f, to = t, easing = table.find(builtins, easing) and easing or builtins[easing], duration = duration, start = getTickCount( ), onChange = onChange, onEnd = onEnd})
    return #anims
end

function destroyAnimation(a)
    if anims[a] then
        table.remove(anims, a)
    end
end

addEventHandler("onClientRender", root, function( )
    local now = getTickCount( )
    for k,v in ipairs(anims) do
        v.onChange(interpolateBetween(v.from, 0, 0, v.to, 0, 0, (now - v.start) / v.duration, v.easing))
        if now >= v.start+v.duration then
            if type(v.onEnd) == "function" then
                v.onEnd( )
            end
            table.remove(anims, k)
        end
    end
end)
local x = 0
animate(0, 364, "Linear", 4000, function(movedX) x = movedX end)

function draw()
    dxDrawRectangle(x, 183, 550, 380, tocolor(0, 0, 0, 156), false)
    dxDrawRectangle(364, 563, 550, 4, tocolor(237, 171, 123, 156), false)
    dxDrawRectangle(440, 295, 390, 37, tocolor(216, 213, 213, 254), false)
    dxDrawRectangle(440, 373, 390, 37, tocolor(216, 213, 213, 254), false)
end
addEventHandler("onClientRender", root, draw)

tested and working don't put the animate function with onClientRender and animate returns a table not an int

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...