Drawing a line not working

Hey there!
I'm trying to simply draw a line as a sprite, but it isn't working.

local howHard = gfx.sprite.new()
local howBig = playdate.getCrankPosition
local function initialize()
howHard:setSize(howBig() / 4, howBig() / 4)
function howHard:draw()
gfx.drawLine(150, 100, 150, 20)
end
howHard:moveTo(150, 50)
howHard:add()
end
initialize()

But this never seems to output a line. I have in the playdate.update
gfx.playdate.update

Nothing appears when the code is run, any help is greatly appreciated.

Is it possible that you have the crank's position at zero? It would just not draw the line in that case since the sprite's size would be set to zero.

The other thing I can think of is that you seem to be using absolute coordinates in the draw function, which won't work since the sprite draw function acts like it's in a graphics context, with the sprite's size as the image dimensions.

getCrankPosition is a function. You're not invoking it.

They are though -- they're setting a local to playdate.getCrankPosition and invoking that.

Yes, you're correct. I don't think that this is the problem, but it's being invoked twice. It should probably invoke it once in case it returns a different value on the two calls (I doubt it this is true, but then it only needs invoking once).


I don't know if howHarddraw() can be defined that way (I'm grasping at straws), maybe try:

howHard.draw = function (self)
   // Draw stuff
end

Finally, could you be be creating another graphics context somewhere so that the draw is being send to that rather than where you think it is?

I think the problem here is the line coordinates being out of the sprite bounds.

From the Inside Playdate documentation for playdate.graphics.sprite:draw(x, y, width, height):

The rect passed in is the current dirty rect being updated by the display list. The rect coordinates passed in are relative to the sprite itself (i.e. x = 0, y = 0 refers to the top left corner of the sprite).

Using playdate.getCrankPosition() / 4 to set the sprite size you can at most get an 89.75x89.75 sprite (359 / 4), and coordinates used in the sprite draw function are relative to the sprite itself; so drawing a line from (150,100) to (150,20) in the sprite draw function would not change any pixels in the portion of the screen being updated via that call.

If you change howHard:draw() to:

    function howHard:draw(x, y, w, h)
        gfx.drawLine(x, y, x + w, y + h)
        gfx.drawRect(x, y, w, h)
    end

you can show the boundaries of the dirty rect being updated in the draw call (with a line across it for good measure):

That's basically where the sprite is, and to draw outside of it you'd have to call draw functions in other parts of your code (e.g., if gfx.drawLine(150, 100, 150, 20) is meant to refer to screen coordinates, calling it somewhere in playdate.update() will draw a line on that part of the screen).

Edit: sorry @scratchminer, I had missed that you had already said basically the same in shorter form...