Jump to content

[SOLVED] Calculating progressbar for song


Tails

Recommended Posts

Hello fellow MTA scripters,

So, I've been trying to solve this calculation for some time now.

What I have is the 'secs' which is the duration of the song.

I'm trying to fill up a bar going from 0 to 479 in width but I must calculate how much to add for every second in the song until it gets to 479.

I've tried all sorts of things, but this is what I have at the moment and I just don't know what to do anymore.

Does anyone know how to solve the math problem? Any help is much appreciated!

progressBarMin = 0      -- = 0% 
progressBarMax = 479    -- = 100% 
  
width = secs / progressBarMax 
  
incremental = width+width 
  
setTimer( 
    function() 
        width = width+incremental 
    end 
,1000,secs 
) 
  
  
dxDrawRectangle(17, 175, width, 4, tocolor(255, 255, 255, 255), false) 

Edited by Guest
Link to comment

Where do I type that in? Is 'progress', the seconds?

When I type

setTimer( 
                function() 
                    width = (secs * 479) / 100 
                end 
            ,1000,secs 
            ) 

I get expected vector 2 at argument 3 for the dxDrawRectangle.

Or

width = (secs * 479) / 100 
             
            setTimer( 
                function() 
                    width = width+width 
                end 
            ,1000,secs 
            ) 

Not sure what to do here.

Link to comment
function ...(...) 
    playSound(...) 
    tick = getTickCount() 
end 
  
addEventHandler("onClientRender", root, 
    function() 
        local progress = math.floor( ( getTickCount() - tick ) / 1000 ) 
        local width =  ( progress * 479 ) / secs 
        dxDrawRectangle(17, 175, width, 4, tocolor(255, 255, 255, 255), false) 
    end 
) 

Link to comment

Thank to all who've tried to help. I managed to fix this using some very simple math. It's how I had it in mind from the start.

Here's the final code:

width = 0 
maxWidth = 480 
increment = maxWidth/secs -- secs is defined elsewhere for the length of the song 
  
setTimer(function() 
        width = width+increment 
    end 
,1000,secs) 
  
function displayTitle()                          
    dxDrawRectangle(24, 192, 480, 8, tocolor(255, 255, 255, 255), false) 
    dxDrawRectangle(24, 192, width, 8, tocolor(55, 55, 55, 255), false) 
end 
addEventHandler("onClientRender",root,displayTitle) 

@Sasu,

Someone else came up with something very similar using getTickCount too. It worked so I'll assume yours will work just as well.

I won't be using it, though, as I wanted to come up with something much simpler and straightforward. But thanks for your contribution because it certainly helps.

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...