×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    43

    Smooth Horizontal Canvas Scrolling

    Hi,

    Is there a simple technique for continous horizontal scrolling in a game canvas.

    I have a simple image which i'd like to scroll as a game background. It needs to wrap around and continue scrolling until the user stops the game.

    I tried it with TiledLayer and using tiledlayer.setPosition(0, 0) each time the (tiledlayer.getX() > (TILE_WIDTH - getWidth()), but this produced a jerky effect as the TiledLayer was positioned back to the start of the image.


    Many thanks for any help.
    Kind Regards
    Rav

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

    Re: Smooth Horizontal Canvas Scrolling

    Code:
    Image background;
    int xWorld;
    int screenWidth;
    
    void paint(Graphics g) {
      g.drawImage(background,-xWorld,0,Graphics.TOP|Graphics.LEFT);
      g.drawImage(background,screenWidth-xWorld,0,Graphics.TOP|Graphics.LEFT);
    }
    
    void run() {
      // bla bla bla
      if (xWorld <= 0) {
        xWorld += screenWidth;
      }
      if (xWorld >= screenWidth) {
        xWorld -= screenWidth;
      }
      // more bla bla bla
    }
    That's off the top of my head without using the game API. It might need some corrections.

    shmoove

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    43

    Re: Smooth Horizontal Canvas Scrolling

    Thanks for the info, seemed to do the trick.

    Kind Regards
    Rav

  4. #4
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249

    Re: Smooth Horizontal Canvas Scrolling

    Sweet. Works like a charm
    Just one tiny adjustment... it doesn't work perfect when the background image has a longer width than the screensize. It wraps at the width of the screensize so part of the image is never used. A tiny readjustment to the code compares the values to the image width as opposed to the screenwidth.

    Code:
    Image background = ...;
    int xWorld = ...;
    int iWidth = background.getWidth();
    
    void paint(Graphics g) {
      g.drawImage(background,-xWorld,0,Graphics.TOP|Graphics.LEFT);
      g.drawImage(background,iWidth-xWorld,0,Graphics.TOP|Graphics.LEFT);
    }
    
    void run() {
      // bla bla bla
      if (xWorld <= 0) {
        xWorld += iWidth;
      }
      if (xWorld >= iWidth) {
        xWorld -= iWidth;
      }
      // more bla bla bla
    }

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

    Re: Smooth Horizontal Canvas Scrolling

    True. I didn't test any of that and I guess I assumed the background was screen sized.

    shmoove

Similar Threads

  1. Scrolling canvas
    By roopa_ckr in forum Mobile Java General
    Replies: 5
    Last Post: 2005-10-17, 11:10
  2. Horizontal scrolling in List Boxes
    By urrg in forum Symbian User Interface
    Replies: 3
    Last Post: 2004-11-23, 09:30
  3. problem scrolling canvas
    By diplm in forum Mobile Java General
    Replies: 9
    Last Post: 2004-05-26, 21:19
  4. Canvas Scrolling
    By ramka001 in forum Mobile Java General
    Replies: 1
    Last Post: 2004-01-19, 03:37
  5. Major problem with delegation to Canvas
    By bartekn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2003-10-08, 17:46

Posting Permissions

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