Weird animation while loading 2 images

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
LemonSlice
Prole
Posts: 3
Joined: Fri Apr 06, 2018 5:51 pm

Weird animation while loading 2 images

Post by LemonSlice »

Hi
Im new to Love2D and to programming in general.
I tried doing a simple running animation to a character but instead of doing it I get a weird glitch animation...

I saw that glitch happens to a different animation depending on which image is loaded first.

here is the code (hope you won't laugh at it):

Code: Select all

local imageFileRun
local imageFile
local frames = {}
local activeFrame
local currentFrame = 1
local imageFileX, imageFileY

function love.load()
    imageFileRun = love.graphics.newImage ("axe bandit run.png")
    frames[1] = love.graphics.newQuad(0,0,80,80, imageFileRun:getDimensions())
    frames[2] = love.graphics.newQuad(80,0,80,80, imageFileRun:getDimensions())
    frames[3] = love.graphics.newQuad(160,0,80,80, imageFileRun:getDimensions())
    frames[4] = love.graphics.newQuad(240,0,80,80, imageFileRun:getDimensions())
    frames[5] = love.graphics.newQuad(320,0,80,80, imageFileRun:getDimensions())
    frames[6] = love.graphics.newQuad(400,0,80,80, imageFileRun:getDimensions())
    frames[7] = love.graphics.newQuad(480,0,80,80, imageFileRun:getDimensions())
    frames[8] = love.graphics.newQuad(560,0,80,80, imageFileRun:getDimensions())

    imageFile = love.graphics.newImage ("Axe Bandit.png")
    frames[1] = love.graphics.newQuad(0,0,80,80, imageFile:getDimensions())
    frames[2] = love.graphics.newQuad(80,0,80,80, imageFile:getDimensions())
    frames[3] = love.graphics.newQuad(160,0,80,80, imageFile:getDimensions())
    frames[4] = love.graphics.newQuad(240,0,80,80, imageFile:getDimensions())
    frames[5] = love.graphics.newQuad(320,0,80,80, imageFile:getDimensions())
    frames[6] = love.graphics.newQuad(400,0,80,80, imageFile:getDimensions())

    imageFileX = love.graphics.getWidth()/2-160
    imageFileY = love.graphics.getHeight()/2-160

    activeFrame = frames[currentFrame]
end

function love.draw()
    if(love.keyboard.isDown('d'))
    then love.graphics.draw(imageFileRun,activeFrame,imageFileX,imageFileY,0,4,4)
        imageFileX = imageFileX + 1
    else love.graphics.draw(imageFile,activeFrame,imageFileX,imageFileY,0,4,4)
    end
end

local elapsedTime = 0
function love.update(dt)
        elapsedTime = elapsedTime + dt
    if love.graphics.draw(imageFileRun,activeFrame,imageFileX,imageFileY,0,4,4) then
        if (elapsedTime > 0.15) then
            if(currentFrame < 8) then
                currentFrame = currentFrame + 1
            else
                currentFrame = 1
            end
                activeFrame = frames[currentFrame]
                elapsedTime = 0
        end
    else
        if (elapsedTime > 0.15) then
            if(currentFrame < 6) then
                 currentFrame = currentFrame + 1
            else
                 currentFrame = 1
            end
            activeFrame = frames[currentFrame]
            elapsedTime = 0
        end
    end
end
someone knows what's wrong?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Weird animation while loading 2 images

Post by zorg »

Hi and welcome to the forums!

From a first glance, i do see one potential issue; you're overwriting the first 6 quads in the frames table with those of the second image.
I'd suggest you either create a framesRun table, or have it be 2 dimensional, and frames[1][j] would refer to the running quads, and frames[2][j] to the other ones.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
LemonSlice
Prole
Posts: 3
Joined: Fri Apr 06, 2018 5:51 pm

Re: Weird animation while loading 2 images

Post by LemonSlice »

Hey thanks
Could you try and show it to me as code?
I dont really understand it like that
As I said im really new to this

Edit: I managed to do it Thank You!
Last edited by LemonSlice on Fri Apr 06, 2018 7:44 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Weird animation while loading 2 images

Post by zorg »

You defined frames as such:

Code: Select all

local frames = {}
then you wrote this:

Code: Select all

    imageFileRun = love.graphics.newImage ("axe bandit run.png")
    frames[1] = love.graphics.newQuad(0,0,80,80, imageFileRun:getDimensions())
    frames[2] = love.graphics.newQuad(80,0,80,80, imageFileRun:getDimensions())
    frames[3] = love.graphics.newQuad(160,0,80,80, imageFileRun:getDimensions())
    frames[4] = love.graphics.newQuad(240,0,80,80, imageFileRun:getDimensions())
    frames[5] = love.graphics.newQuad(320,0,80,80, imageFileRun:getDimensions())
    frames[6] = love.graphics.newQuad(400,0,80,80, imageFileRun:getDimensions())
    frames[7] = love.graphics.newQuad(480,0,80,80, imageFileRun:getDimensions())
    frames[8] = love.graphics.newQuad(560,0,80,80, imageFileRun:getDimensions())

    imageFile = love.graphics.newImage ("Axe Bandit.png")
    frames[1] = love.graphics.newQuad(0,0,80,80, imageFile:getDimensions())
    frames[2] = love.graphics.newQuad(80,0,80,80, imageFile:getDimensions())
    frames[3] = love.graphics.newQuad(160,0,80,80, imageFile:getDimensions())
    frames[4] = love.graphics.newQuad(240,0,80,80, imageFile:getDimensions())
    frames[5] = love.graphics.newQuad(320,0,80,80, imageFile:getDimensions())
    frames[6] = love.graphics.newQuad(400,0,80,80, imageFile:getDimensions())
All those frames assignments use the same table, so you're overwriting the first six with the second six.
You need to use a separate place to store that second set of quads.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
LemonSlice
Prole
Posts: 3
Joined: Fri Apr 06, 2018 5:51 pm

Re: Weird animation while loading 2 images

Post by LemonSlice »

Thank you guys
Its working now
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests