# How to properly scroll a 2D tilemap?

Hello and I'm trying to make my own game engine in Java. I have completed all the necessary ones but I can't figure it out with the TileGame class. It just can't scroll. Also there are no exceptions. Here I'm listing the code.

TileGame.java

@Override
public void draw(Graphics2D g) {
if (back!=null){
back.render(g);
}
if (follower!=null){
follower.render(g);
follower.draw(g);
}
for (int i=0; i<actors.size(); i++){
Actor actor = actors.get(i);
if (actor!=follower&&getVisibleRect().intersects(actor.getBounds())){
g.drawImage(actor.getAnimation().getFrameImage(), actor.x - OffSetX, actor.y - OffSetY, null);
actor.draw(g);
}
}
}

/**
* This method returns the visible rectangle
* @return The visible rectangle
*/
public Rectangle getVisibleRect(){
return new Rectangle(OffSetX, OffSetY, global.WIDTH, global.HEIGHT);
}

@Override
public void update(){
if (follower!=null){
if (scrollHorizontally){
OffSetX = global.WIDTH/2 - Math.round((float)follower.x) - tileSize;
OffSetX = Math.min(OffSetX, 0);
OffSetX = Math.max(OffSetX, global.WIDTH - mapWidth);
}
if (scrollVertically){
OffSetY = global.HEIGHT/2 - Math.round((float)follower.y) - tileSize;
OffSetY = Math.min(OffSetY, 0);
OffSetY = Math.max(OffSetY, global.HEIGHT - mapHeight);
}
}
for (int i=0; i<actors.size(); i++){
Actor actor1 = actors.get(i);
if (getVisibleRect().contains(actor1.x, actor1.y)){
actor1.update();
for (int j=0; j<actors.size(); j++){
Actor actor2 = actors.get(j);
if (actor1.isCollidingWith(actor2)){
actor1.collision(actor2);
actor2.collision(actor1);
}
}
}
}
}


but the problem is that all the actors are working, but it just won't scroll. Help Please.. Thanks in Advance.

• Code dump and "find my problem" are bad questions. – MichaelHouse Mar 22 '12 at 1:47
• Show us only the relevant part of the code and tell us what you've tried already. Maybe even why you think it isn't working. – MichaelHouse Mar 22 '12 at 2:01
• The reason these details are important is so that we know you understand the code and can narrow down where the problem area resides. Otherwise, you might be someone who is using code they don't understand and asking someone else to fix it so you can pass it off as your own. Not to mention, that is a lot to look through... (Not saying you are doing this, just mentioning why it isn't good practice to approach these questions this way.) – AndrewAchilles Mar 22 '12 at 3:01
• You don't seem to be changing the Offset values at all... – thedaian Mar 22 '12 at 3:07
• I can see the problem but as others mentioned you likely don't understand the code so just a hint: go through your scroll offset calculation line by line and figure out exactly what each does and why is it there. That's a basic debugging skill which you have to master if you're going to become a programmer. Figuring out exactly what each part of your code does is also a good idea. – Gilead Mar 22 '12 at 3:13

Hopefully you have solved this yourself by now, if not here is some help to get you there.

# Debugger

That program you're using to type all your code in to? It's not just for typing code in and pressing "play". It's an IDE (Integrated Development Environment). This means it contains many tools to help you develop, one of those tools is the debugger. I suggest you read more about debuggers here.

## How do you use the debugger?

It's going to depend on your IDE of choice (they likely contain very similar debuggers, but just go with it). For Java there are a few major ones: Eclipse, Netbeans and more. Regardless they all have some similar features. One of the most powerful features is breaking, breaking can happen automatically from an exception (as I'm sure you've seen), or it can happen manually via the use of breakpoints. For situations like yours, breakpoints are your best friend. They will allow you to stop execution of your code at a specific point, and examine the state of your program at that time.

## How do you use breakpoints?

Again, this is IDE specific. Commonly it's a right click off to the left side column of the code window and selecting "Enable breakpoint". Yes, it's that easy. After setting a breakpoint, you'll start your program in debug mode. This mode lets the IDE know that it should pay attention to breakpoints and other debug features. Now, when the code execution gets to the point in your code where you set your breakpoint, it stops, and turns control over to you. This is awesome. You can now view what variable values are, many IDEs let you execute code snippets to see their result against the current state of your program, change values and more! This is SO powerful. Breakpoints rock.

Now that your program execution has stopped, it's not dead. It's waiting for you to tell it what to do next. Again, you have some powerful options. You can continue execution as normal, you can step into functions, step out of functions or step over line by line. Amazing. It's often useful to set a breakpoint at the beginning of a function, then step through the code line by line, seeing the variables change and following the execution of the code. The line by line stepping is by far the coolest way to follow the execution of your program.

Now, I hope you've read the above, followed some of the links, and looked up how to perform the actions described using your IDE of choice. If you've gotten this far, I'll offer you a further hint. Set a breakpoint on this line: if (follower!=null){ check your values. Is follower null? How about scrollHorizontally and scrollVertically, are they both true? Now, step line by line, watch how your OffsetX and OffsetY values change as you change them around, around in a full circle ;)

• thanks, it worked. I've changed the Math.min to Math.max and vice versa – Sri Harsha Chilakapati Mar 23 '12 at 0:24