Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Can't seem to figure out how to make my map view scroll...  (Read 294 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-10-11 18:50:19 »

Hey,

I've just about completed the code that renders a map to the JFrame but I can't seem to figure out how to make the map scroll as the player moves along it. I've got everything else working as far as I know (not rendering what's outside the view, rendering tiles, etc...).

Could someone take a look at the code below and see if you can see what I might need to do to get the map to move with the player? I'm taking out the huge switch statement but the main code will be below.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
package valkryst.area.map;

import java.awt.Graphics;

import valkryst.area.Area;
import valkryst.area.AreaLoader;
import valkryst.area.map.tile.GrassTile;
import valkryst.area.map.tile.VoidTile;
import valkryst.core.graphics.Frame;
import valkryst.entity.Player;

/**
 * This class adds the map to the pixel array that will be rendered onto the screen.
 *
 * @author Valkryst
 * --- Last Edit 11-Oct-2013
 */

public class RenderMap {
   private static VoidTile voidTile = new VoidTile();
   private static GrassTile grassTile = new GrassTile();
   
     /**
    * Renders the map to the canvas.
    * @param g The graphics object to use to render things to the canvas with.
    * @param area The area that the player is currently in.
    * @param p The player.
    * @param width The width of the canvas.
    * @param height The height of the canvas.
    */

   public static void render(final Graphics g, final Area area, final Player p, final int width, final int height) {
      if(width > 0 && height > 0) { // If there is no room on the screen to render the map then don't bother rendering it.
        for(int y = 0 + (int)p.getTileY() - 1; y < (int)p.getTileY() + (height >> 5) + 2; y++) {
            for(int x = 0 + (int)p.getTileX() - 1; x < (int)p.getTileX() + (width >> 5) + 2; x++) {
               try {
                  switch(AreaLoader.toArray()[y].substring(x, x+1)) {
                     case "A": {
                        g.drawImage(grassTile.getTileImage(), x * 32 * Frame.SCALE, y * 32 * Frame.SCALE, null);
                        break;
                     }
                                                        // Cut out about a hundred lines of switch statement.
                    default: {
                        g.drawImage(voidTile.getTileImage(), x * 32 * Frame.SCALE, y * 32 * Frame.SCALE, null);
                     }
                  }
               } catch(Exception e) {
                  continue;
               }
            }
         }
      }
   }
}


The player moves in pixels and every time the player moves 32 pixels the tileX and tileY change.
tileX and tileY represent where the player is on the map.
height and width are the height and width of the canvas.

Thanks for any replies.
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-10-11 19:54:13 »

You need a starting coordinate to start drawing your tiles. Then you can move that point around, and reference it when drawing your tiles.

So, When you move the player by X, you move that point by -X. Of course, all your tiles need to be drawn relative to that point.
Offline tyeeeee1
« Reply #2 - Posted 2013-10-11 20:14:46 »

You need a starting coordinate to start drawing your tiles. Then you can move that point around, and reference it when drawing your tiles.

So, When you move the player by X, you move that point by -X. Of course, all your tiles need to be drawn relative to that point.

Alright, so let's assume I set the starting coords to the tile at (0,0) and that the player is currently standing at (5,5). Now, the player moves to the tile to the right which puts him at (6,5). This is what I assume you're saying so-far and it makes sense.

Now, to move the entire map +1 to the right where on my for loops would I place the "xOffset" which will be assumed to be +1 to the xOffset to make it move like this? I'm thinking it would go something like this...

1  
2  
for(int y = yOffset + (int)p.getTileY() - 1; y < (int)p.getTileY() + (height >> 5) + 2; y++) {
            for(int x = xOffset + (int)p.getTileX() - 1; x < (int)p.getTileX() + (width >> 5) + 2; x++) {


But I can't test that until I alter my entire program to work with the offsets.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troncoso

JGO Coder


Medals: 20



« Reply #3 - Posted 2013-10-11 20:18:47 »

The offset is applied to the final position that you draw each tile.

1  
g.drawImage(grassTile.getTileImage(), x * 32 * Frame.SCALE, y * 32 * Frame.SCALE, null);


I guess that's where it is. If you are scrolling the map by tile size, then you'd scale the offset by 32.
Offline tyeeeee1
« Reply #4 - Posted 2013-10-11 20:28:24 »

That seemed to do the trick. Thanks for the help!
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.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!