×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    16

    Is switching to another Canvas inside a paint() method OK?

    Please explain me where the error is:

    I have a main game loop in a run() method of a Thread Object.
    It's contents is simple:

    Code:
    1 switch (gameUtil.getCurrentDisplayNumber())
    2   case GameUtil.CANVAS_GAME:
    3   {
    4      ((Canvas)gameUtil.getCurrentCanvas(GameUtil.CANVAS_GAME)).repaint();
    5      ((Canvas)gameUtil.getCurrentCanvas(GameUtil.CANVAS_GAME)).serviceRepaints();
    6      break;
    7   }
    8   case GameUtil.CANVAS_GAME_OVER:
    9   {
    10      ((Canvas)gameUtil.getCurrentCanvas(GameUtil.CANVAS_GAME_OVER)).repaint();
    11      ((Canvas)gameUtil.getCurrentCanvas(GameUtil.CANVAS_GAME_OVER)).serviceRepaints();
    12      break;
    13  }
    The main game logic is inside the paint method of the game canvas.
    I utilize the fact that paint() is called all the time and I do all movement calculations
    inside this method.

    Now, when somewhere inside this paint() method I detect that the player has lost all lives
    I want to switch to a Game Over screen. So I call:

    Code:
    gameUtil.setCurrentDisplayNumber(GameUtil.CANVAS_GAME_OVER);
    Display.getDisplay(gameUtil.getGameMIDlet()).setCurrent(gameUtil.getCurrentCanvas(gameUtil.getDisplayNumber()));
    This call is made somewhere inside the paint() method of the game canvas.

    And the problem is that on some phones the game over canvas shows up but is deadlocked.
    I mean the paint() method of the game over canvas is called only once and never again.

    I noticed that removing the serviceRepaints() call from the 5th line solves the problem but
    it result in animation frames being dropped (at least it looks like this).

    So I want to find out what is causing the problem - what I'm doing wrong and how to correct it.
    By the way: I'm not explicitly using any other threads than the main one.

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

    Re: Is switching to another Canvas inside a paint() method OK?

    Putting all the logic in the paint() method is not healthy. Remember that calling repaint() only asks the main event thread to perform paint() so your logic is basically blocking the main event thread. That will, in the best case, make your app less responsive. In the worst case you get weird behaviour like you described.

    I'd say move the logic out of paint() into a method called from the game loop, that should fix it.

    shmoove

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    16

    Re: Is switching to another Canvas inside a paint() method OK?

    Thank you. It helped.

Similar Threads

  1. How to paint Full screen using Canvas?
    By ongchiboon in forum Mobile Java General
    Replies: 8
    Last Post: 2011-07-09, 10:18
  2. Replies: 0
    Last Post: 2005-02-03, 09:44
  3. Major problem with delegation to Canvas
    By bartekn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2003-10-08, 17:46
  4. Repaint calls ignored in paint method on S40
    By sam.hendley in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2003-07-26, 06:45
  5. 7650 and 3650 Canvas and FullCanvas memory
    By marcilgen in forum Mobile Java General
    Replies: 5
    Last Post: 2003-07-08, 12:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×