The Sprite Batch class enables the batch drawing of all the items added as children. This will increase the performance and the memory utilization of the game. In order to accomplish this, the Sprite Batch needs to be loaded with a sprite sheet image.
Another thing that the SpriteBatch needs to know is where in the sprite sheet a particular sprite is placed (known as frame). For this another class comes into play: the SpriteFrameCache class contains all the necessary information about every frame in the sprite sheet.
For more information: see: Sprite Frame Cache documentation page.
Every time a new single sprite needs to be drawn on the screen a binding between the texture (the uncompressed raw image) and the openGL context occur, which is very expensive. But when a Sprite Batch is used, only the sprite sheet is bound because all of the SpriteBatch children are included in it.
In order to work the Sprite Batch should be added as a child of a Scene or Layer, then the Sprites must be added to the SpriteBatch. For more information, see: the Sprite documentation page.
Creates a new instance of the Sprite Batch:
sprite_batch = SpriteBatch.new file_name: 'sprite_batch.png' sprite_batch = SpriteBatch.new texture: sprite_batch_texture
capacity: Number - Default:29
The SpriteBatch maximum number of children.
Adds a new child to the SpriteBatch instance:
The Sprites are the only classes can be children of a SpriteBatch.
Push a new child into a SpriteBatch instance:
sprite_batch << sprite
The << and add_child methods are equivalent.
Adds multiple children to the SpriteBatch instance:
children = [asteroid_sprite, mother_ship_sprite] sprite_batch.add_children children