Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411528 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 28, 2024, 10:59:06 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsJava scrolling
Pages: [1]
Print
Author Topic: Java scrolling  (Read 2477 times)
Wind_Waker
Level 0
*


View Profile
« on: July 30, 2011, 07:03:25 PM »

Anyone know a neat, ideal way to scroll the screen/map as your player continues to walk right?

in Java of course.

Also I have 1-2 years of programming in Java so please don't hold back! haha
Logged
Trevor Dunbar
Level 10
*****


Working on unannouned fighting game.


View Profile
« Reply #1 on: July 31, 2011, 01:48:50 AM »

You can just draw map tiles according to where a 2d camera object position is and cull map tiles that are not within a certain space off-screen.

Take the cameras position(x,y) divide it by the tile size, like let's say 32. This will tell you what tiles to start drawing from within the map array. Then you keep going in your For Loop until the map tiles reach somewhere off-screen or it reaches the end of the map array. If you wanted, you could even make the level wrap-around itself if you reach the end by using modulo operators when you iterate through the level array to draw tiles on-screen.

To make it appear like the tiles you are drawing are actually scrolling, take the cameras x and y position separately and modulate them by your chosen tile size and offset all the tiles you draw on-screen by that amount when you draw them.
« Last Edit: July 31, 2011, 01:56:31 AM by Trevor Dunbar » Logged

Toucantastic.
Wind_Waker
Level 0
*


View Profile
« Reply #2 on: July 31, 2011, 04:49:10 AM »

Thanks I'm going to get to this, also I'm going to try to implement a multi-dimensional arraylist for my tilemap! Wish me luck also this plan would work going vertically right? I mean just different math for the scrolling?
« Last Edit: July 31, 2011, 05:00:14 AM by Wind_Waker » Logged
Trevor Dunbar
Level 10
*****


Working on unannouned fighting game.


View Profile
« Reply #3 on: July 31, 2011, 05:52:50 AM »

Scrolling down is the same as scrolling left or right, just take your camera's y position component and do the math on the Y drawn tiles.


psuedo code:

Code:
int start_tiles_x =  Math.Clamp((int)(Camera.X / 32), 0, Map_width);
int end_tiles_x = Math.Clamp(start_tiles_x + 36, 0, Map_width);
int start_tiles_y =  Math.Clamp((int)(Camera.Y / 32), 0, Map_Height);
int end_tiles_y = Math.Clamp(start_tiles_y + 36, 0, Map_Height);
Vector2 draw_pos;

For (int X = start_tiles_x; X < end_tiles_x; ++X)
{
For (int Y = start_tiles_y; Y < end_tiles_y; ++Y)
{
   //position, sprite to draw
   draw_pos = new Vector2(Camera.X % 32 , Camera.Y % 32);
   int tileSprite_to_draw = ((X % 5) * Y);
   LevelObject.DrawCall( draw_pos, tileSprite_to_draw );
}
}
« Last Edit: July 31, 2011, 06:15:52 AM by Trevor Dunbar » Logged

Toucantastic.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic