I’m not going into proper Object Oriented Programming but some of you may be interested in using or modifying a custom class I created that allows you to “attach” an animation to a fisica body (it’s still experimental):
The class is called Sprite is a custom class with an FBox (only a box for now) and an Animation (also a custom class). By custom I mean it’s not part of the Processing language or any library, it shows up as a tab in Processing and you need to have it in the same folder as the main sketch along with animation.
You declare a Sprite this way:
Creating a sprite
This is how you create a new sprite. The needed parameters are: width, height, animation frames prefix, number of frames
player = new Sprite(100, 100, "triangle_animated_", 9);
Assets: at this point Processing will try to load 9 images named “triangle_animated_0000.png” to “triangle_animated_0008.png” make sure you prepare the assets and name them properly.
– the numeration always has to start from 0000
– they must have have 4 digits in their number 0001 0002 etc
– they must be .png files so you can have transparency
Proper game frameworks use spritesheets usually created with specialized applications or plugins. I’ll avoid them for now, I figure it’s easier to produce a series of images.
The Sprite class extends FBox so it inherits all the properties and methods. You can still do things like:
player.setPosition(400, 500); player.setName("player"); world.add(player);
Full example here: example13
Animation without fisica
You can use the Animation class independently from Sprite and Fisica and/or control it independently within the sprite (access it by yourSpriteName.animation).
Here’s an example