com.golden.gamedev.object
Class SpriteGroup

java.lang.Object
  |
  +--com.golden.gamedev.object.SpriteGroup
Direct Known Subclasses:
PlatformGroup

public class SpriteGroup
extends Object

SpriteGroup is a group of sprites with common behaviour. Use SpriteGroup to manage sprites easily (updating and rendering many sprites at once).

For example:

    ////////// Group for Enemy //////////

    // creates the enemy sprites
    Sprite asteroid = new Sprite(),
           asteroid2 = new Sprite();

    // the enemy SpriteGroup
    SpriteGroup ENEMY = new SpriteGroup("Enemy");
    ENEMY.add(asteroid);
    ENEMY.add(asteroid2);

    // updating and rendering all sprites at once
    ENEMY.update();
    ENEMY.render(Graphics2D);
 
SpriteGroup is also be used by CollisionGroup.


Constructor Summary
SpriteGroup(String name)
          Creates a new sprite group, with specified name.
 
Method Summary
 void add(int index, Sprite member)
          Inserts specified sprite into this group at specified index, the first sprite is the first to render.
 void add(Sprite member)
          Inserts specified sprite into this group.
 void clear()
          Removes all group members.
 Sprite getActiveMember()
          Returns the first found active sprite or null if there is no active sprite.
 Background getBackground()
          Background of this group.
 Comparator getComparator()
           
 int getExpandFactor()
           
 String getName()
          Returns the name of this group.
 int getScanFrequence()
           
 int getShrinkFactor()
           
 int getSize()
          Returns the size of this group.
 Sprite[] getSprites()
          Returns all sprites (active, inactive, and also null sprite) in this group.
 Sprite remove(int index)
          Removes a sprite at specified index from this group.
 boolean remove(Sprite s)
          Removes specified sprite from this group.
 void render(Graphics2D g)
          Renders all sprites in this group.
 void reset()
          Removes all group members (same with clear() except this method also removes sprite memory reference).
 void scanSprites()
          Scans for inactive sprites to be thrown from this group.
protected  void scheduleToScan()
          Schedule to scan inactive sprites based on scan frequence time.
 void setBackground(Background backgr)
           
 void setComparator(Comparator c)
           
 void setExpandFactor(int factor)
           
 void setName(String name)
           
 void setScanFrequence(int i)
           
 void setShrinkFactor(int factor)
           
 void sort(Comparator c)
           
 String toString()
           
 void update()
          Updates all sprites in this group.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SpriteGroup

public SpriteGroup(String name)
Creates a new sprite group, with specified name.
Name is used only for identifier.

Method Detail

add

public void add(Sprite member)
Inserts specified sprite into this group.


add

public void add(int index,
                Sprite member)
Inserts specified sprite into this group at specified index, the first sprite is the first to render.


remove

public Sprite remove(int index)
Removes a sprite at specified index from this group.


remove

public boolean remove(Sprite s)
Removes specified sprite from this group.


reset

public void reset()
Removes all group members (same with clear() except this method also removes sprite memory reference).
Use this method whenever the size of the removed members is taking too large memory.

See Also:
clear()

clear

public void clear()
Removes all group members.
This method simply set group size to nil.
The sprites reference is actually removed when shrink factor is exceed.
To remove sprites and its reference, use reset() instead.

For example:
Destroying all enemies when player got a bomb.

     ENEMY_GROUP.clear();
 

See Also:
reset()

update

public void update()
Updates all sprites in this group.


sort

public void sort(Comparator c)

render

public void render(Graphics2D g)
Renders all sprites in this group.


scheduleToScan

protected void scheduleToScan()
Schedule to scan inactive sprites based on scan frequence time.

See Also:
setScanFrequence(int), scanSprites()

scanSprites

public void scanSprites()
Scans for inactive sprites to be thrown from this group.
All inactive sprites will be thrown from the group if inactive sprites exceed this group's shrink factor.

See Also:
setShrinkFactor(int)

getActiveMember

public Sprite getActiveMember()
Returns the first found active sprite or null if there is no active sprite. Usually used to check is this group still have an active sprite or not.

For example:

     if (ENEMY.getActiveMember() == null) {
         // no active enemy, go to next level
         gameState = WIN;
     }
 

Returns:
the first found active sprite or null if no active sprite

getScanFrequence

public int getScanFrequence()

setScanFrequence

public void setScanFrequence(int i)

getComparator

public Comparator getComparator()

setComparator

public void setComparator(Comparator c)

getName

public String getName()
Returns the name of this group.


setName

public void setName(String name)

getSprites

public Sprite[] getSprites()
Returns all sprites (active, inactive, and also null sprite) in this group.

See Also:
getSize()

getSize

public int getSize()
Returns the size of this group.


getExpandFactor

public int getExpandFactor()

setExpandFactor

public void setExpandFactor(int factor)

getShrinkFactor

public int getShrinkFactor()

setShrinkFactor

public void setShrinkFactor(int factor)

getBackground

public Background getBackground()
Background of this group.

See Also:
com.golden.gamedev.object.background

setBackground

public void setBackground(Background backgr)

toString

public String toString()
Overrides:
toString in class Object