Jump to content

New in the community, help with dxDrawImage


#Switch

Recommended Posts

Hi, i've started to learn lua, and i did a script wich shows an image (dxDrawImage) and a line over the image (dxDrawLine), functions works properly, but i want to add an animation for those "objects", when a player die, image and line come up from the bottom of the screen.

Here's some code

dxDrawLine ( x*1, y*0.944, -x*0.05, y*0.944, tocolor ( 0, 255, 153, 255 ), 2 ) 
dxDrawImage ( x*0.4, y*0.875, 300, 50, 'images/dead_player.png', 0, 0, -120 ) 

so when a player die, line & images come up. i don't know how to add this animation.

Link to comment
  • Moderators

Bonsai, please don't post such script and call it as "solution" to help members that are still learning.

Seriously dude, did you even read your code back before clicking the "Submit" button ?

At least it made me laugh :)

Ok, try this, I made this code that will let you change settings to easilly modify the animation using interpolateBetween (first time I used this function :wink:):

local screenWidth, screenHeight = guiGetScreenSize() 
  
local startPos = {0.4, 1} --the relative position from where the image will start 
local endPos = {0.4, 0.875} --the relative position from where the image will stop 
local animType = "OutQuad" --the animation type, see [url=https://wiki.multitheftauto.com/wiki/Easing]https://wiki.multitheftauto.com/wiki/Easing[/url] 
local speed = 2 --the speed of the entire animation 
local hideTime = 3 --time in sec after the dead image will hidden (starting from the end of the anim) 
  
local animStep = 0 --do not modify, used to hold the progression of the animation 
local stopingTimer = nil  
  
--the distance between the line and the top of the image 
local lOffsetY = 0.07*screenHeight 
  
function showDeadImage() 
    animStep = 0 --start at the step 0 
    addEventHandler("onClientRender", root, animateDeadImage) 
end 
addCommandHandler("dead", showDeadImage) --you can obviously delete this line 
  
function animateDeadImage() 
    local x, y, _ = interpolateBetween( 
        startPos[1]*screenWidth, startPos[2]*screenHeight, 0, 
        endPos[1]*screenWidth, endPos[2]*screenHeight, 0, animStep/100, animType) 
     
    dxDrawImage(x, y, 300, 50, 'images/dead_player.png', 0, 0, -120) 
    dxDrawLine(0, y+lOffsetY, screenWidth, y+lOffsetY, tocolor( 0, 255, 153, 255 ), 2 ) 
  
    animStep = animStep + speed --steping the animation 
    if animStep >= 100 and not stopingTimer then 
        stopingTimer = setTimer(hideDeadImage, hideTime*1000, 1) --start the timer to make hide it 
    end 
end 
  
function hideDeadImage() 
    removeEventHandler("onClientRender", root, animateDeadImage) 
    stopingTimer = nil --reset it for our condition 
end 

I hope the comments will be enough to understand the code but ofc, ask if you don't really get something.

PS: I tested the code without the image (so only with the line) and it was working fine, so let me know how it's going with the image.

Regards,

Citizen

Link to comment
Bonsai, please don't post such script and call it as "solution" to help members that are still learning.

Seriously dude, did you even read your code back before clicking the "Submit" button ?

At least it made me laugh :)

I didn't call it a solution. :(

Its just supposed to be a guideline.

This way people still have to think about it and can't just copy/paste it, what he is probably going to do with your code.

And that will make him come back in the future, asking basic questions because you don't learn from copying,

and whats even worse, he might expect others to post a working code, as most of topic creators already do.

Link to comment
  • Moderators

Well I understood your point of view, but as u can see, that wasn't a really basic question, and I try to set comments as much as I can so that they will be able to understand that code.

But I know some guys who would just copy paste your code and coming back saying it's not working (I know it's stupid, but they do exist, specially if they are new to coding and seeing the lines of code as black magic :D).

Link to comment
Bonsai, please don't post such script and call it as "solution" to help members that are still learning.

Seriously dude, did you even read your code back before clicking the "Submit" button ?

At least it made me laugh :)

Ok, try this, I made this code that will let you change settings to easilly modify the animation using interpolateBetween (first time I used this function :wink:):

local screenWidth, screenHeight = guiGetScreenSize() 
  
local startPos = {0.4, 1} --the relative position from where the image will start 
local endPos = {0.4, 0.875} --the relative position from where the image will stop 
local animType = "OutQuad" --the animation type, see [url=https://wiki.multitheftauto.com/wiki/Easing]https://wiki.multitheftauto.com/wiki/Easing[/url] 
local speed = 2 --the speed of the entire animation 
local hideTime = 3 --time in sec after the dead image will hidden (starting from the end of the anim) 
  
local animStep = 0 --do not modify, used to hold the progression of the animation 
local stopingTimer = nil  
  
--the distance between the line and the top of the image 
local lOffsetY = 0.07*screenHeight 
  
function showDeadImage() 
    animStep = 0 --start at the step 0 
    addEventHandler("onClientRender", root, animateDeadImage) 
end 
addCommandHandler("dead", showDeadImage) --you can obviously delete this line 
  
function animateDeadImage() 
    local x, y, _ = interpolateBetween( 
        startPos[1]*screenWidth, startPos[2]*screenHeight, 0, 
        endPos[1]*screenWidth, endPos[2]*screenHeight, 0, animStep/100, animType) 
     
    dxDrawImage(x, y, 300, 50, 'images/dead_player.png', 0, 0, -120) 
    dxDrawLine(0, y+lOffsetY, screenWidth, y+lOffsetY, tocolor( 0, 255, 153, 255 ), 2 ) 
  
    animStep = animStep + speed --steping the animation 
    if animStep >= 100 and not stopingTimer then 
        stopingTimer = setTimer(hideDeadImage, hideTime*1000, 1) --start the timer to make hide it 
    end 
end 
  
function hideDeadImage() 
    removeEventHandler("onClientRender", root, animateDeadImage) 
    stopingTimer = nil --reset it for our condition 
end 

I hope the comments will be enough to understand the code but ofc, ask if you don't really get something.

PS: I tested the code without the image (so only with the line) and it was working fine, so let me know how it's going with the image.

Regards,

Citizen

WoW such solution!, thank you, it worked, but positions of the line are wrong, it took me a while to understand the code, after i fixed positions, i changed

addCommandHandler("dead", showDeadImage) 

and added a function to show images & line with this:

addEventHandler ( "onClientPlayerWasted", localPlayer, 
 function ( ) 
  showDeadImage ( ) 
 end 
) 

it works!, my question is if i remove timers, will the image & line dissapear after the map changes? (its for a Race DM server).

i'll try that.

@bonsai i won't copy & paste only, i want to learn lua, i already know Pawno (3 years), PHP, HTML & CSS. i guess in some months i'll be able to create much more things with lua, if i get the knowedge

Link to comment
  • Moderators

Yeah, great, but you created a useless anonymous function for that event handler. Let me explain.

The addEventHandler function takes a function as 3rd argument. It's the function that will be called if that event is triggered.

showDeadImage is a function too right ? so why not just give that function as 3rd argument of addEventHandler ?

addEventHandler("onClientPlayerWasted", localPlayer, showDeadImage) 

my question is if i remove timers, will the image & line dissapear after the map changes? (its for a Race DM server).

Sure, the scripts (client and server) aren't reloaded/restarted on map change, it just deletes the objects from the previous map and create new ones for the next and showing a picture on the screen for the transition.

By the way, the mta progressbar when new map is starting is just because that maps are seperated resources, that race gamemode is starting/stoping. So it will download the files that are from the map resource but the other resources are still running.

Hope it's a little more clear ;)

Link to comment
Yeah, great, but you created a useless anonymous function for that event handler. Let me explain.

The addEventHandler function takes a function as 3rd argument. It's the function that will be called if that event is triggered.

showDeadImage is a function too right ? so why not just give that function as 3rd argument of addEventHandler ?

addEventHandler("onClientPlayerWasted", localPlayer, showDeadImage) 

my question is if i remove timers, will the image & line dissapear after the map changes? (its for a Race DM server).

Sure, the scripts (client and server) aren't reloaded/restarted on map change, it just deletes the objects from the previous map and create new ones for the next and showing a picture on the screen for the transition.

By the way, the mta progressbar when new map is starting is just because that maps are seperated resources, that race gamemode is starting/stoping. So it will download the files that are from the map resource but the other resources are still running.

Hope it's a little more clear ;)

i didn't delete timers, i did this

local hideTime = 999999 
stopingTimer = setTimer(hideDeadImage, hideTime*999999, 1) 

a friend help me out with server side, so when a map changes it kills timer and images disapear ^^ isn't the most pro solution but works!

Link to comment
  • Moderators

Omg, why not just delete the timer ? I did it because I thought you would need it.

local screenWidth, screenHeight = guiGetScreenSize() 
  
local startPos = {0.4, 1} --the relative position from where the image will start 
local endPos = {0.4, 0.875} --the relative position from where the image will stop 
local animType = "OutQuad" --the animation type, see [url=https://wiki.multitheftauto.com/wiki/Easing]https://wiki.multitheftauto.com/wiki/Easing[/url] 
local speed = 2 --the speed of the entire animation 
local hideTime = 3 --time in sec after the dead image will hidden (starting from the end of the anim) 
  
local animStep = 0 --do not modify, used to hold the progression of the animation 
  
--the distance between the line and the top of the image 
local lOffsetY = 0.07*screenHeight 
  
function showDeadImage() 
    animStep = 0 --start at the step 0 
    addEventHandler("onClientRender", root, animateDeadImage) 
end 
addCommandHandler("dead", showDeadImage) --you can obviously delete this line 
  
function animateDeadImage() 
    local x, y, _ = interpolateBetween( 
        startPos[1]*screenWidth, startPos[2]*screenHeight, 0, 
        endPos[1]*screenWidth, endPos[2]*screenHeight, 0, animStep/100, animType) 
    
    dxDrawImage(x, y, 300, 50, 'images/dead_player.png', 0, 0, -120) 
    dxDrawLine(0, y+lOffsetY, screenWidth, y+lOffsetY, tocolor( 0, 255, 153, 255 ), 2 ) 
  
    animStep = animStep + speed --steping the animation 
end 
  
function hideDeadImage() 
    removeEventHandler("onClientRender", root, animateDeadImage) 
end 

And just call hideDeadImage when you need to.

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