luagame-developmentlove2d

Resizing makes sprite disappear love2d


So, I was working on my love2d project when I had this gun sprite, it was way too big so I though since love2d's love.graphics.draw() function had sx and sy as arguments I could just put it there. But when I did that the sprite disappeared. Here's my love.draw() function:

function love.draw()
    if playerRotation == 0 then
        love.graphics.draw(player.sprite, playerCollider:getX() - player.width + player.width / 3, playerCollider:getY() - player.height * 2 + player.height / 2)
    elseif playerRotation == 1 then
        love.graphics.draw(player.spriteLeft, playerCollider:getX() - player.width + player.width / 3, playerCollider:getY() - player.height * 2 + player.height / 2)
    end
    love.graphics.draw(ground.sprite, ground.x, ground.y, 0, love.graphics.getWidth(), love.graphics.getHeight())
    love.graphics.draw(gun.sprite, playerCollider:getX(), playerCollider:getY(), 0, gun.width, gun.height)
end

My player "class":

local class = require('libraries/middleclass')

local Player = class('Player')

function Player:init()
    self.x = 350
    self.y = 100
    self.width = 80
    self.height = 80
    self.sprite = love.graphics.newImage('sprites/player.png')
    self.spriteLeft = love.graphics.newImage('sprites/player-left.png')
    self.jumps = 0
    self.maxJumps = 1
end

return Player

Gun "class":

local class = require('libraries/middleclass')
local Player = require('player')
local Gun = Player:subclass('gun')

Gun:init()
Gun.sprite = love.graphics.newImage('sprites/gun.png')

return Gun

I tried to change the width to a single number, but that didn't work. It just kept disappearing for some reason.


Solution

  • The sx and sy arguments are scaling factors, 1.0 means that the sprite will be drawn with the original size in selected axis. By default sx = 1 and sy = sx. See love.graphics.draw for reference.

    The way that you define Gun it will have its width and height set to 80 due to Gun:init() call that calls Player.init(Gun). Middleclass usage aside, this is a ridiculously big factor and could be that a transparent section of gun is drawn to the screen creating the illusion of not drawing anything.

    You want to make the gun smaller - set the factors to something between 0 and 1.

    Regarding the middleclass, if you rename your init member functions to initialize then middleclass will call them on instance construction. Also, define initializer for Gun instead of operating on the class itself:

    local Player = class('Player')
    
    function Player:initialize()
      self.sprite = 'sprites/player.png'  -- just to illustrate; use newImage
    end
    
    local Gun = Player:subclass('Gun')
    
    function Gun:initialize()
      Player.initialize(self)
      self.sprite = 'sprites/gun.png'
    end
    
    player = Player()
    gun = Gun()
    print(player.sprite)  --> sprites/player.png
    print(gun.sprite)     --> sprites/gun.png
    

    I'd encourage you to think over Gun --> Player inheritance.