Need help to animation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
HiddenCookie4
Prole
Posts: 1
Joined: Thu Nov 21, 2024 3:49 pm

Need help to animation

Post by HiddenCookie4 »

Hello, I'm learning developement with Love2D and i've a problem with animation. (I'm french then i'm sorry for my English)
i want lunch an animation when i pressed a button of my mouse ("1") but i need stay pressed to view completely my animation. I expect is simple problem but i've already try 2 ou 3 tutorial to understand but... I don't.

I'm trying do a simple TDS, i've programme all function i want but i'm trying implent animation to have a better game !

I send you my code !

Thank's to you ! (I'm here if u need more information !)

Code: Select all

--! file: player.lua

Player = Object:extend()

function Player:new()
    self.x = 300
    self.y = 100
    self.speed = 500
    self.width = 149
    self.height = 182

    self.state = "idle"
    self.move = false
    self.shot = false

    self:loadAssets()
end

function Player:loadAssets()
    self.animation = {timer = 0, rate = 0.1}
 
    self.animation.run = {total = 6, current = 1, img = {}}
    for i=1, self.animation.run.total do
       self.animation.run.img[i] = love.graphics.newImage("assets/Walk_gun/Walk_gun_00"..i..".png")
    end

    self.animation.idle = {total = 1, current = 1, img = {}}
    for i=1, self.animation.idle.total do
       self.animation.idle.img[i] = love.graphics.newImage("assets/Walk_gun/Walk_gun_00"..i..".png")
    end

    self.animation.shot = {total = 5, current = 1, img = {}}
    for i=1, self.animation.shot.total do
       self.animation.shot.img[i] = love.graphics.newImage("assets/Gun_shot/Gun_shot00"..i..".png")
    end
 
    self.animation.draw = self.animation.run.img[1]
    self.animation.width = self.animation.draw:getWidth()
    self.animation.height = self.animation.draw:getHeight()
 end

function Player:update(dt)
    self:animate(dt)
    self:setState()


    -- Gestion des déplacements
    if love.keyboard.isDown("left") or love.keyboard.isDown("q") then
        self.x = self.x - self.speed * dt
        self.move = true
    elseif love.keyboard.isDown("right") or love.keyboard.isDown("d") then
        self.x = self.x + self.speed * dt
        self.move = true
    elseif love.keyboard.isDown("up") or love.keyboard.isDown("z") then
        self.y = self.y - self.speed * dt
        self.move = true
    elseif love.keyboard.isDown("down") or love.keyboard.isDown("s") then
        self.y = self.y + self.speed * dt
        self.move = true
    elseif love.mouse.isDown("1") then
        self.shot = true
    else
        self.shot = false
        self.move = false
    end

    -- Gestion angle de la souris
    -- position de la souris
    mouse_x, mouse_y = love.mouse.getPosition()
    dx, dy = mouse_x - self.x, mouse_y - self.y
    -- calcul de l'angle
    angle = math.atan2(dy, dx) - 1.57
    
    
end

function Player:setState()
    if self.move then
       self.state = "run"
    elseif self.shot then
       self.state = "shot"
    else
       self.state = "idle"
    end
 end

function Player:animate(dt)

    self.animation.timer = self.animation.timer + dt
    if self.animation.timer > self.animation.rate then
       self.animation.timer = 0
       self:setNewFrame()
    end
end

function Player:setNewFrame()
    local anim = self.animation[self.state]
    if anim.current < anim.total then
       anim.current = anim.current + 1
    else
       anim.current = 1
    end
    self.animation.draw = anim.img[anim.current]
end

function Player:draw()

    love.graphics.draw(self.animation.draw, self.x, self.y, angle, 0.5, 0.5, self.animation.width / 2, self.animation.height / 2)
    love.graphics.print(self.width, 10, 10)
    love.graphics.print(self.height, 10, 30)
    love.graphics.print(angle, 10, 50)

end
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Need help to animation

Post by pgimeno »

The problem might be the quotes around "1". Use love.mouse.isDown(1) instead of love.mouse.isDown("1").

Also, if you want people with other keyboards to be able to use your game, use love.keyboard.isScancodeDown instead of love.keyboard.isDown, specifying WASD instead of ZQSD. That way it will work the same no matter the language of the keyboard.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 10 guests