1

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?

gnat
  • 6,213
  • 108
  • 53
  • 73
jkigel
  • 1,592
  • 6
  • 28
  • 49
  • If layerManager is already holding all the other Sprites you want to draw, then why aren't you also adding each gunshot to layerManager when the player fires? – mr_lou Mar 13 '13 at 06:19
  • So I have to hold a reference to GameCanvas in GameController, right? isn't it a little bit messy? – jkigel Mar 13 '13 at 06:25

1 Answers1

0

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();
}
mr_lou
  • 1,910
  • 2
  • 14
  • 26