Hi, I’ve bumped on some problems since i got movement on my frame.
I’m trying to move the cursor around within my tile map and to scroll when it is 2 tiles from the edge from the screen.
Problems that happen are:
When i scroll around somethimes it scrolls wrong, leaving a part of the map or the cursor out of the window…
you can see the full code over here http://sourceforge.net/projects/jadvancedwars
private void updateWorld(){
t++;
// Wait some time because else the cursor moves 3 tiles with one key press.
if (t % 3 == 0){
t=0;
// Move the Cursor and Scroll the map if needed
moveCursor();
}
}
private void moveCursor() {
boolean canMoveLeft=false; // Check if we can move to the left
boolean canMoveRight=false; // Check if we can move to the right
boolean canMoveUp=false; // Check if we can move up
boolean canMoveDown=false; // Check if we can move down
// Cursor position in pixels
int row_Pixel= cursor.getCurrentRowInPixels();
int col_Pixel= cursor.getCurrentColInPixels();
// Dimentions in Pixels
int width_Pixel= map.getWidth()-30;
int height_Pixel= map.getHeight()-TOP_OFFSET;
// Set all directions in wich we can move.
if( col_Pixel > 0 ) canMoveLeft=true;
if( col_Pixel < width_Pixel) canMoveRight=true;
if( row_Pixel > 0 ) canMoveUp=true;
if( row_Pixel <= height_Pixel) canMoveDown=true;
// Move the cursor to the given direction if it is possible.
// Alsoo check if we have to scroll the map.
if(cursor.isLeft() && canMoveLeft && !cursor.isRight()) {
cursor.moveLeft(); scroll("Left"); }
if(cursor.isRight() && canMoveRight && !cursor.isLeft()) {
cursor.moveRight(); scroll("Right"); }
if(cursor.isUp() && canMoveUp && !cursor.isDown()) {
cursor.moveUp(); scroll("Up"); }
if(cursor.isDown() && canMoveDown && !cursor.isUp()) {
cursor.moveDown(); scroll("Down"); }
}
// Problems:
// TODO: Cursor sometimes get's off the screen when we move
// TODO: Part of the map get's off the screen when we move
private void scroll(String direction) {
boolean canScroll_Left=false; // Check if we can scroll Left
boolean canScroll_Right=false; // Check if we can scroll Right
boolean canScroll_Up=false; // Check if we can scroll Up
boolean canScroll_Down=false; // Check if we can Scroll Down
if ( direction.equals("") ) return;
// Cursor position in tiles
int row_Tile= cursor.getCurrentRowInTiles();
int col_Tile= cursor.getCurrentColInTiles();
// Dimentions in Tiles
int col_tiles= map.getCols();
int row_tiles= map.getRows();
//views
int left_view=col_tiles-12;
int up_view=row_tiles-12;
// Set all directions in wich we can scroll.
if( col_Tile >= 2 && col_Tile <= left_view ) canScroll_Left=true;
if( col_Tile >= 13 && col_Tile <= map.getWidth()-EDGE ) canScroll_Right=true;
if( row_Tile >= 2 && row_Tile <= up_view) canScroll_Up=true;
if( row_Tile >= 13 && row_Tile <= map.getHeight()-EDGE )canScroll_Down=true;
if (direction.equals("Left") && canScroll_Left )
scrollMap("Left");
if (direction.equals("Right") && canScroll_Right )
scrollMap("Right");
if (direction.equals("Up") && canScroll_Up )
scrollMap("Up");
if (direction.equals("Down") && canScroll_Down )
scrollMap("Down");
}
/**
* Move the map by adjusting the offset.
* We start moving when we are 2 tiles from the edge.
* @param direction
*/
public void scrollMap(String direction) {
if (direction.equals("Left")) {
i--;
left_Offset+=map.getTile_Size();
System.out.println("Scrolled-Left"+i);
}
if (direction.equals("Right")) {
i++;
left_Offset-=map.getTile_Size();
System.out.println("Scrolled-Right"+i);
}
if (direction.equals("Up")) {
j--;
top_Offset+=map.getTile_Size();
System.out.println("Scrolled-Up"+j);
}
if (direction.equals("Down")) {
j++;
top_Offset-=map.getTile_Size();
System.out.println("Scrolled-Down"+j);
}
}