Search code examples
graphicsjava-memidplcduigamecanvas

Java ME Drawing multiple dynamic Sprites on GameCanvas


I want to draw on GameCanvas multiple dynamic Sprites such as gun shots.

I have 2 main classes: GameCanvas and GameController

GameController holds a Vector of my gun shots.

GameCanvas has an access to GameController's Vector of Sprite and it also has a render() method which draws Sprites on screen.

private void render() {
            Graphics g = getGraphics();

            layerManager.setViewWindow(0, 0, getWidth(), getHeight());
            layerManager.paint(g, 0, 0);

            flushGraphics();
}

LayerManager holds all the Sprites I want to draw.

How can I draw all objects in GameController's Vector on screen?


Solution

  • I'd think it could be done like this:

    Graphics g = getGraphics(); // No need to get this each time you render. Get it once outside the render function
    
    private void render() {
    
      layerManager.setViewWindow(0, 0, getWidth(), getHeight());
      layerManager.paint(g, 0, 0);
    
      // Loop through the vector
      for (Enumeration en = gunshotVector.elements(); en.hasMoreElements();) {
        ((Sprite)en).paint(g);
      }
    
      flushGraphics();
    }