Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tile based scrolling advice  (Read 1924 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Duke





« Posted 2012-03-26 17:49:48 »

Hi everyone,
I've got a game I'm trying to make. What I have so far is a sort of 200 by 200 tile map inside my level class, set up like this:

1  
byte[] tiles = new byte[200 * 200];


This then populates the map with the grass tile:

1  
2  
3  
4  
5  
6  
7  
for(int y = 0; y < MAP_HEIGHT; y++)
{
   for(int x = 0; x < MAP_WIDTH; x++)
   {
      tiles[y * MAP_WIDTH + x] = Grass.id;
   }
}


Then the render method in my level class, which sets all the pixels to the screen is set out like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public void render(Screen screen)
   {
      int xx = player.getXCoord() - 5;
      int yy = player.getYCoord() + 5;
     
      for(int y = 0; y < 11; y++)
      {
         for(int x = 0; x < 11; x++)
         {
            int tileId = tiles[yy * MAP_WIDTH + xx];
           
            if(tileId == Grass.id)
            {
               s.drawTile(Grass.spriteSheetCoords(), x * 8, y * 8);
            }
           
            xx++;
         }
         yy--;
         xx -= 11;
      }
   }


Ignoring for the moment the blatant inefficiencies in the code above, I'm just wonder how I would go about creating a scrolling effect for walking?
What's confusing me at the moment is that the tiles array maps things out in tiles, which are 8 by 8 icon sized images extracted from a spritesheet, which is how I'm rendering them to the screen. Although the player's coordinates are in pixels, so when the player moves left, for example, their x coord is reduced by 1. They may still be on the same tile, though.

Anyone who can give me any advice on how to go about this would be doing me a big favour. Thanks!
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-26 21:10:58 »

What you need to do is not confuse logic and rendering. Logic-wise, your tiles should have absolute values. Render-wise, you want a variable to offset the X and Y to create the scrolling effect. Every time the player moves, you update the xOffset and yOffset variables with negative the player position minus half the player's width or height plus half the width or height of the window. Then, before the map is rendered, just simply make a call to Graphics2D.translate(offsetX,offsetY) to translate, or offset, the "world".

1  
2  
xOffset = -(player.getX()+player.getWidth()/2) + windowWidth/2;
yOffset = -(player.getY()+player.getHeight()/2) + windowHeight/2;

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (22 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!