Page 1 of 1

Can't move my Sprite

Posted: Wed Dec 23, 2020 4:33 am
by Shadow3489
I've got my image uploaded into the game, and I've coded for it to move a specific direction when I press the 'w', 'a', 's' and 'd' buttons. But when I load the game, the image is not moving.

Here's my code:

Code: Select all

Player = Class{}

local MOVE_SPEED = 140

function Player:init(map)
    self.width = 16
    self.height = 20
    
    self.x = map.tileWidth * 10
    self.y = map.tileHeight * (map.mapHeight - 2) - self.height

    self.texture = love.graphics.newImage('images/Harry final.png')
    self.frames = generateQuads(self.texture, 32, 32)
end


function Player:update(dt)
    if love.keyboard.isDown('a') then
        self.x = self.x - MOVE_SPEED * dt
    elseif love.keyboard.isDown('d' ) then
        self.x = self.x + MOVE_SPEED * dt
    elseif love.keyboard.isDown('w') then
        self.y = self.y - MOVE_SPEED * dt
    elseif love.keyboard.isDown('s') then
        self.y = self.y + MOVE_SPEED * dt
    end
end

function Player:render()
    love.graphics.draw(self.texture, self.frames[2], self.x, self.y)
end
I'm sort of new to coding, so I don't know what I'm doing wrong.

Re: Can't move my Sprite

Posted: Wed Dec 23, 2020 4:39 am
by sphyrth
I'm only assuming that you didn't call Player:update(dt) inside love.update(dt)

Re: Can't move my Sprite

Posted: Wed Dec 23, 2020 5:11 am
by Shadow3489
sphyrth wrote: Wed Dec 23, 2020 4:39 am I'm only assuming that you didn't call Player:update(dt) inside love.update(dt)

Oh! Yep! That was it! Thank you!