Jump to content

Letting HUD shake


Karuzo

Recommended Posts

Hey Guys,

so i wanted to shake a hud if a player is sprinting/walking than the offset should be +20.

but that doesn't work.

Hope you can help me.

Regards,

KRZO.

local lplayer = getLocalPlayer() 
local g_root = getRootElement() 
local sWidth,sHeight = guiGetScreenSize()  
local Width,Height = 300,150 
local X = (sWidth/2) - (Width/2) 
local Y = (sHeight/2) - (Height/2) 
local offset = 0 
local turn = true 
  
function renderShake() 
  local state = getPedMoveState(lplayer) 
  if state.sprint or state.walk or state.powerwalk then 
    if turn then 
      offset = offset + 1 
      if offset > 20 then 
    turn = false 
      end 
    else 
      offset = offset - 1 
      if offset < -20 then 
    turn = true 
      end 
    end 
  else 
    if offset > 0 then 
      offset = offset - 1 
    elseif offset < 0 then 
      offset = offset + 1 
    end 
    turn = true 
  end 
  dxDrawRectangle(X+500,(Y+270)-offset,Width,Height,tocolor(44, 62, 80,225),false) 
end 
addEventHandler("onClientRender",g_root,renderShake) 

Link to comment

If you read wiki carefully, you should see that getPedMoveState returns a string which move state the ped is in at the moment.

So your code should be:

if state == "sprint" or state == "walk" or state == "powerwalk" then 

instead of

if state.sprint or state.walk or state.powerwalk then 

on line 12.

Link to comment
  • Moderators
if state.sprint or state.walk or state.powerwalk then 

Where did you see getPedMoveState was returning a table ?! It returns a string:

if state == "sprint" or state == "walk" or state == "powerwalk" then 

Also, just wanted to say that this script is resolution dependent. 20 pixels won't be the same percentage of the screen height on all resolutions. The high resolution the player will have, the less the rectangle will shake (almost invisible on really high resolution).

Same about X+500 and Y+270

Link to comment
if state.sprint or state.walk or state.powerwalk then 

Where did you see getPedMoveState was returning a table ?! It returns a string:

if state == "sprint" or state == "walk" or state == "powerwalk" then 

Also, just wanted to say that this script is resolution dependent. 20 pixels won't be the same percentage of the screen height on all resolutions. The high resolution the player will have, the less the rectangle will shake (almost invisible on really high resolution).

Same about X+500 and Y+270

Woops, didn't read the wiki carefully.

Thank you.

To the another problem: How should i solve the resolution prob ?

Link to comment
  • Moderators

As always by using percentages (so relative to the screen).

So the numbers will be basically between 0 and 1 (0 = 0% of the screen and 1 = 100% of the screen)

But of course, the dx functions only accept absolute values (pixels) so you just have to multiply the relatives value by the current screen width or height, depending on the axes (X or Y).

So you will then just say: I want it to shake the rectangle only by 10% of the screen height and 10% = 0.1 like in the example of teQuilla. You will then multiply the relative value you will get by the current screen height to get the value in pixels.

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