Module: Rubygame::Sprites::DepthSortGroup
- Defined in:
- lib/rubygame/sprite.rb
Overview
DepthSortGroup is a mix-in module that extends Group to sort its sprites by their @depth attribute, so that sprites with low depths will appear on top of sprites with higher depths. A sprite’s depth can be any Numeric, or nil (which will be counted as 0).
If two sprites have exactly the same depth, there is no guarantee about which one will be drawn on top of the other. (But, whichever one is on top will stay on top, at least until the group is re-sorted.)
If a sprite’s depth changes after it has been added to the group, you must use the #sort_sprites method for the change to have any effect.
Instance Method Summary collapse
-
#<<(sprite) ⇒ Object
Add a single sprite to the group.
-
#push(*sprites) ⇒ Object
Add multiple sprites to the group, then re-sort all sprites.
-
#sort_sprites ⇒ Object
Sort sprites by depth, in descending order, so that sprites with low depths will be drawn on top of sprites with high depths.
Instance Method Details
#<<(sprite) ⇒ Object
Add a single sprite to the group. For efficiency reasons, this method doesn’t re-sort sprites afterwards. You should use #sort_sprites after you’re done adding sprites. Or, better yet, just use #push.
488 489 490 |
# File 'lib/rubygame/sprite.rb', line 488 def <<(sprite) super end |
#push(*sprites) ⇒ Object
Add multiple sprites to the group, then re-sort all sprites.
493 494 495 496 497 498 499 |
# File 'lib/rubygame/sprite.rb', line 493 def push(*sprites) sprites.each { |sprite| self << sprite } sort_sprites() return self end |
#sort_sprites ⇒ Object
Sort sprites by depth, in descending order, so that sprites with low depths will be drawn on top of sprites with high depths.
If a sprite has a depth of nil, it is sorted as if its depth were 0 (zero).
505 506 507 |
# File 'lib/rubygame/sprite.rb', line 505 def sort_sprites self.sort! { |a,b| (b.depth or 0) <=> (a.depth or 0) } end |