Cacaroto

[HELP] Problem with interpolateBetween and hiding a rectangle / text

Recommended Posts

I was scripting a radio system but there's no way to let me move the text, the animation of the rectangle is done but it doesn't make any effect with the text. Also I was trying to show all this for a period of 5 seconds, but when I added the timer, the rectangle and the text only appear but not dissapear, can someone help me?

 

local sX, sY = guiGetScreenSize()
local open = false
local radio = playSound("http://www.energy981.com/playlist/Energy98_128WM.asx")
setSoundVolume(radio, 0)

function drawRadio()
	ls,rs = getSoundLevelData(radio)
	meta = getSoundMetaTags(radio);
	title = meta.stream_title or "Unknown"
	longTitle = dxGetTextWidth(title, (1/1920)*sX, "default-bold", false)

	dxDrawCircle ((1880/1920)*sX, (125/1080)*sY, 20, 0, 360, tocolor(9, 27, 79, 200), tocolor(9, 27, 79, 200), 32, 1, false)
	dxDrawCircle ((1880/1920)*sX, (125/1080)*sY, 100*((ls+rs)/230768), 0, 360, tocolor(0, 225, 255, 50), tocolor(0, 225, 255, 25), 32, 1, false)
	dxDrawImage((1868/1920)*sX, (113/1080)*sY, (24/1920)*sX, (23/1080)*sY, "radio.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
end

function knowRadioSong()
	start = getTickCount()
	addEventHandler("onClientRender", root, outputRadioSongName)
end
addCommandHandler("radio", knowRadioSong)

function outputRadioSongName()
	pos = ((1875-longTitle)/1920)*sX
	setTimer ( function()
			pos = (1925/1920)*sX
	end, 5000, 1)

	local now = getTickCount()
	local elapsedTime = now - start
	local endTime = start + 1000
	local duration = endTime - start
	local progress = elapsedTime / duration
	local x, y, z = interpolateBetween(pos, (160/1080)*sY, 0, pos, (160/1080)*sY, 0, progress, "OutBack")
	local x1, y1, z1 = interpolateBetween((0/1920)*sX, (-365/1080)*sY, 0, (-35/1920)*sX, (-365/1080)*sY, 0, progress, "OutBack")

	dxDrawRoundedRectangle(x, y, ((15+longTitle)/1920)*sX, (30/1080)*sY, tocolor(255, 255, 255, 200), tocolor(255, 255, 255, 200), false)
	dxDrawText(title, (1848/1920)*sX, (159/1080)*sY, (1883/1920)*sX, (190/1080)*sY, tocolor(0, 0, 0, 255), (1/1920)*sX, "default-bold", "right", "center", false, false, false, false, false)
end

function openRadio()
	if open == false then
		open = true
		addEventHandler("onClientRender", root, drawRadio)
		setSoundVolume(radio, 1)
	else
		open = false
		removeEventHandler("onClientRender", root, drawRadio)
		removeEventHandler("onClientRender", root, outputRadioSongName)
		setSoundVolume(radio, 0)
	end
end
bindKey("m", "down", openRadio)

 

Edited by Cacaroto

Share this post


Link to post

You should never create a timer on every frame like you're doing right now. It will create 60 timers in a second, 300 in 5 seconds (meaning you'll have up to 30 timers running at any one time just from this code).

Looking at the code, you don't even need the timer if you're using interpolateBetween, so remove lines 25 through 28. The pos variable can and should be local to make lookup and reading faster on Lua.

Your problem appears to be in duration math.

local endTime = start + 1000
local duration = endTime - start

-- should just be
local duration = 1000

Second problem is that you don't seem to move the text at all, just the rectangle, but I hope you can figure that one out on your own.

Edited by MrTasty

Share this post


Link to post

It works now, but I don't know how to make the regresive animation only with interpolateBetween, can you help me? By the way, I need help removing the music of maps. I used a loop to mute every sound and turn on the radio one, and the same to mute the radio and turn on the other sounds, but the problem comes when I start a gamemode map and that map has music, I tried using onGamemodeMapStarts which is a function of mapmanager but it doesn't work, if you could help me with that too...

 

function knowRadioSong()
	start = getTickCount()
	addEventHandler("onClientRender", root, outputRadioSongName)
	pos = ((1875-longTitle)/1920)*sX
	pos2 = (1880/1920)*sX
	setTimer ( function()
			pos = (1925/1920)*sX
			pos2 = (-500/1920)*sX
	end, 5000, 1)
end
addCommandHandler("radio", knowRadioSong)

function outputRadioSongName()
	local now = getTickCount()
	local elapsedTime = now - start
	local duration = 1000
	local progress = elapsedTime / duration
	local x, y, z = interpolateBetween((1925/1920)*sX, (160/1080)*sY, 0, pos, (160/1080)*sY, 0, progress, "OutBack")
	local x1, y1, z1 = interpolateBetween(((1920+longTitle)/1920)*sX, (175/1080)*sY, 0, pos2, (175/1080)*sY, 0, progress, "OutBack")

	dxDrawRoundedRectangle(x, y, ((15+longTitle)/1920)*sX, (30/1080)*sY, tocolor(255, 255, 255, 200), tocolor(255, 255, 255, 200), false)
	dxDrawText(title, x1, y1, x1, y1, tocolor(0, 0, 0, 255), (1/1920)*sX, "default-bold", "right", "center", false, false, false, false, false)
end

function openRadio()
	if open == false then
		open = true
		addEventHandler("onClientRender", root, drawRadio)
		for v, k in ipairs(getElementsByType("sound")) do
			setSoundVolume(k, 0)
		end
		setSoundVolume(radio, 1)
	else
		open = false
		removeEventHandler("onClientRender", root, drawRadio)
		removeEventHandler("onClientRender", root, outputRadioSongName)
		for v, k in ipairs(getElementsByType("sound")) do
			setSoundVolume(k, 1)
		end
		setSoundVolume(radio, 0)
	end
end
bindKey("n", "down", openRadio)

 

Edited by Cacaroto

Share this post


Link to post
On 01/09/2019 at 12:01, Cacaroto said:

Any idea, I tried many things but still can't do what I want...

 

You could try this for your sound issue.

 

addEventHandler("onClientResourceStart", root, 
function () 
	if open == false then
		setSoundVolume(source, 0)
		local resourceRoot_ = source
		setTimer(function () 
			if isElement(resourceRoot_) then
				setSoundVolume(resourceRoot_, 0)
			end
		end, 1000, 1)
	end
end, true, "low")

 

Share this post


Link to post

Okay, I fixed my problem with the animation with a very easy way :D

But the way, the problem I have with the sound is when the map is changing, i used onGamemodeMapStart but still doesn't work

Share this post


Link to post

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.