Sprite grouping

Sprites are a very powerful tool for managing graphics in the Playdate. But they do not have a very important feature - grouping.
Yes, making an offset on the x-axis or y-axis of a set of sprites is easy, but making a change in the angle or scale is a big problem. Does anyone have any thoughts on how to resolve this issue?

2 Likes

For cases where the individual sprites don’t move a lot relative to each other I draw them to another sprite first and then move that around and only redraw when necessary, but I agree it would be nice to have some kind of container for more dynamic cases.

since there are no ready-made solutions, I decided to write it :blush:
coming soon

2 Likes

Sprite container (WIP)
Each rectangle is a sprite. The small black rectangle inside is the center point of the sprite. The small circle is the center point of the sprite group.

Rotation of the group around the control point, some sprites have their own rotation animation.
group-1
group-2

Rotation of the group plus movement.
group-3
group-4
group-5

Zoom. Still with the scale position bug. :laughing:
group-6

Without scale, everything is great (will be)! :blush:
group-7

to be continued!

4 Likes

Sprite Container Beta is here!
Sprite Container - 1.zip (31.7 KB)

Demo: 3 different sprites in a container
Moving
SpriteContainer-move-1
Rotation
SpriteContainer-rotation-1
Moving the center point
SpriteContainer-center-point-1

Getting started

import 'Lib/SpriteContainer'
local container = SpriteContainer(200, 120, sprite1, sprite2)

Supported:

container:moveTo(x, y)
container:moveBy(x, y)
container:getPosition()
container:getCenterPoint()
container:setRotation(angle)
container:getRotation()
container:getBounds()
container:getBoundsRect()
container:getSize()
container:setZIndex(index)
container:getZIndex()

Extra:

container:addSprite(...)
container:removeSprite(...)
container:checkSprite(sprite)
container:setCenterPoint(x, y)
container:getCenterPointPosition()

Not supported:
A scaling bug in the API does not allow you to implement these features. I hope this bug will be corrected.

container:setSize(width, height)
container:setCenter(x, y)
container:getCenter()
container:setScale(xScale, [yScale])
container:getScale()
8 Likes

Do you know if the scaling API bug has been fixed?

Sorry, I don't know. You can download the Sprite Container sources and test it yourself.

I wish I'd seen this earlier. I've been tackling this exact problem. I was surprised that sprites didn't have a translation context to add other sprites to.


:warning: Important Gotcha :playdate_agh:

Moving with collisions still needs to use the moveWithCollisions() function. Raw rotation can move the collideBox without this which can result in some insane behaviour. :playdate_goofy:


1 Like

@ivansergeev

Ivan, thank you so much for sharing this! This was the missing piece of the puzzle for me. I am coming to the Playdate from Solar2D/CoronaSDK and groups are such a fundamental part of how I work.

I was able to port my whole game “engine” over to the playdate in no time with the help of your Sprite Container code. Truly life changing. Honestly. Now I can move forward with making a game for the Playdate.

And I might have found an issue?

If I instantiate like this:
local container = SpriteContainer(0, 0, sprite1, sprite2)

And set the origin of the container to 0,0, - I cannot get moveBy(x,y) to work properly. It works just fine if those are not set to 0,0. Just letting you know.

Again,

Thank you so, so much.

Warm regards,

Joe

Fire Maple Games

1 Like

Thank you very much, I’m really glad that Sprite Container was useful to you.

I think you’ll be able to fix the zero-position bug quite easily.

Haha I am not smart enough! But I just use 200,120 and it works great. :slight_smile: