Jump to content

HealthBar


Recommended Posts

Posted

Oi, eu estava fazendo um script em que usei HealthBar e tem para colete .. Porem quando perco vida a barra vai desaparecendo no sentindo da direita para a esquerda .. Eu gostava que me ajudassem a mudar o sentindo para desaparecer do sentido da esquerda para a direita..

  
    local sWidth,sHeight = guiGetScreenSize() 
    local Width = sWidth-400 
    local health = getElementHealth(getLocalPlayer()) 
    local armor = getPedArmor(getLocalPlayer()) 
    local HealthBar = sWidth*( health / Width ) 
    local lineaarmor = sWidth*( armor / Width ) 
     
    dxDrawRectangle(sWidth*0.79,sHeight*0.072,HealthBar,sHeight*0.03,tocolor(255,0,0,250) ,false) 
    dxDrawRectangle(sWidth*0.79,sHeight*0.104,lineaarmor,sHeight*0.03,tocolor(0,223,220,250) ,false)     
  
  

Posted

algum valor vc tem que colocar negativo e eu acho q da certo.

tente

  
    local sWidth,sHeight = guiGetScreenSize() 
    local Width = sWidth-400 
    local health = getElementHealth(getLocalPlayer()) 
    local armor = getPedArmor(getLocalPlayer()) 
    local HealthBar = sWidth*( -health / Width ) 
    local lineaarmor = sWidth*( armor / Width ) 
    
    dxDrawRectangle(sWidth*0.79,sHeight*0.072,HealthBar,sHeight*0.03,tocolor(255,0,0,250) ,false) 
    dxDrawRectangle(sWidth*0.79,sHeight*0.104,lineaarmor,sHeight*0.03,tocolor(0,223,220,250) ,false)    
  
  

560x95_FFFFFF_09FF00_050505_000000.png

"Querer não é poder, mas tentar é avançar"!

Posted

Editei e adaptei o código, para posicionar corretamente caso o jogador tenha a saúde maior que 100.

Tente isto:

local sWidth,sHeight = guiGetScreenSize() 
local Width = sWidth-400 
local maxHealth = getPedMaxHealth(localPlayer) 
local health = getElementHealth(localPlayer) 
local armor = getPedArmor(localPlayer) 
  
local hpX, hpY = sWidth*0.79, sHeight*0.072 
local hpW = (health / maxHealth) * Width 
local armorX, armorY = sWidth*0.79, sHeight*0.104 
local armorW = (armor / 100) * Width 
  
dxDrawRectangle(hpX - hpW, hpY, hpW, 22, tocolor(255,0,0,250)) -- HP bar 
dxDrawRectangle(armorX - armorW, armorY, armorW, sHeight*0.03, tocolor(0,223,220,250)) -- Armor bar 
  
  
function getPedMaxHealth(ped) 
    -- Output an error and stop executing the function if the argument is not valid 
    assert(isElement(ped) and (getElementType(ped) == "ped" or getElementType(ped) == "player"), "Bad argument @ 'getPedMaxHealth' [Expected ped/player at argument 1, got " .. tostring(ped) .. "]") 
  
    -- Grab his player health stat. 
    local stat = getPedStat(ped, 24) 
  
    -- Do a linear interpolation to get how many health a ped can have. 
    -- Assumes: 100 health = 569 stat, 200 health = 1000 stat. 
    local maxhealth = 100 + (stat - 569) / 4.31 
  
    -- Return the max health. Make sure it can't be below 1 
    return math.max(1, maxhealth) 
end 
  

Obs: Width vai retornar um valor muito alto, a barra vai ficar muito comprida. Além disso, você está ajustando a altura para todas as resoluções, você vai precisar ajustar também a largura, de modo que ambas fiquem com a mesma diferença para todas resoluções.

Please do not PM me with scripting related question nor support, use the forums instead.

Posted

Muito obrigado deu perfeitamente .. mas ja que eu usava Width pra aumentar a barra .. com esse novo codigo aumentou bastante .. Como eu posso fazer para diminuir e aumentar agora a barra ? ( e que fique correto para todas as resoluçoes )

Posted

Width é o largura máxima do retângulo, só abaixar o valor e deixar do tamanho que você deseja.

Para ajustar essa largura para todas resoluções, só fazer o seguinte:

Use o valor definido em Width e a resolução em que você definiu o valor, em seguida, use-os em um método de porcentagem. Assim:

(valor_de_Width * 100) /sWidth 

Isso vai retornar quantos por cento Width é de sWidth (sWidth: largura total da tela)

Um exemplo:

sWidth sendo de 1280, e Width sendo um valor de 280:

280 * 100) /1280 --> 21.87% 

Você vai usar esta porcentagem retornada para converter a porcentagem do retângulo para o valor, desta forma:

(porcentagem * sWidth) /100 

--> Este é o valor de Width

Se quiser, pode usar math.ceil para arredondar o valor para cima.

Obs: porcentagem é o valor retornado acima, no exemplo que usei, seria 21.87 (Você pode ignorar o 7 após o 8 na porcentagem, já que pode não fazer diferença).

Please do not PM me with scripting related question nor support, use the forums instead.

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