java - Horizontal tiling background -


i'm coding game , want background repeat itself on , over.

this code i'm using

xoffset = (int) (camera.getx() % width);  g.drawimage(bginv, xoffset - width, 0, width, height, null); g.translate(xoffset, 0); g.drawimage(bg, 0, 0, width, height, null); g.translate(-xoffset, 0); g.drawimage(bginv, xoffset + width, 0, width, height, null); 

the first drawimage draws image when camera's x negative , third when camera's x positive.

  • bg normal background
  • bginv background inverted

the problem when i'm moving , xoffset goes width 0, gives effect of "teleportation".

click here see

the console outputting xoffset

i know because i'm using modulo xoffset didn't figure out better way...

thanks in advance

if understood correctly, want repeat 2 * width height image, left half background image , right half same image horizontally inverted.

so can following:

xoffset = (int) (camera.getx() % (2 * width));  // draw background image @ x = xoffset - 2 * width g.drawimage(bg, xoffset - 2 * width, 0, width, height, null); g.drawimage(bginv, xoffset - width, 0, width, height, null);  // draw background image @ x = xoffset g.drawimage(bg, xoffset, 0, width, height, null); g.drawimage(bginv, xoffset + width, 0, width, height, null); 

Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -