Necessary to setImage() again after drawing into a sprite's image?

I have two projects. In one of them I draw into the image associated with a sprite upon its creation with sprite.new—and the sprite simply updates immediately. I never have to setImage().

In the other, much simpler project, nothing changes in the sprite... until I do setImage() again after the drawing is done.

Both are working fine! But I still want to understand how it all works... are you supposed to have to do setImage() after drawing into a sprite's image, or not?

Are you marking the sprite as dirty via sprite:markDirty()? Otherwise it has no way of knowing that anything has changed.

You can also use sprite:addDirtyRect() if you know only a portion needs to be redrawn, or sprite:setAlwaysRedraw(true) if the whole image is changing every frame.

1 Like

No, neither of the projects uses markDirty()

(That may be happening as a side effect of something else though)

Right, setImage() redraws by default (you can disable this via sprite: setRedrawsOnImageChange()).

If you want the sprite to redraw after that but not as a result of movement or setImage(), you'll have to mark some portion of its bounds as dirty ("needs redraw").

1 Like

Thanks! With the one project, I'm NOT using setImage(), nor markDirty(), yet the sprite stays updated just fine as its assigned image receives drawing commands. But I think what I understand is that I SHOULD expect to be doing one or the other—which mostly satisfies my curiosity. (Some other factor may be marking the sprite as dirty, allowing me to "skip that step.")

1 Like

In 1.7 I added

  • image-based sprites are now automatically marked for redraw if their image changes behind the scenes

It's just a dirty flag in the image that we set when you draw into an image, then the sprite system sees that the image was updated and automatically marks the sprite dirty. Looking at the code I noticed that it doesn't respect the setRedrawsOnImageChange() setting (only sprite:setImage() does), and that's a bug. That I just filed. And will fix some day.

3 Likes