Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Scrolling TileMap  (Read 1439 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Duke





« Posted 2007-07-03 11:24:43 »

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

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  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
    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);
        }
    }
Offline Juriy

Junior Duke





« Reply #1 - Posted 2007-07-04 06:35:44 »

Sorry for not helping you directly. Your code is little hard to read. Maybe you should try placing some breakpoints and watches in moveCursor() and figure out why your code goes wrong.

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline keldon85

Senior Duke


Medals: 1



« Reply #2 - Posted 2007-07-04 08:13:48 »

You're not storing the states properly; sometimes it helps to be able to just say "what will the screen look like in this particular state". If you break that rule then simply enforce it, output a debug message or even enter a breakpoint.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline stef569

Junior Duke





« Reply #3 - Posted 2007-07-07 11:31:51 »

When i set a breakpoint my frame goes white and i cannot move or see my cursor.

So i used system output messages but they don't give me the information about where it went wrong Huh

I have put a output message at key pushed and key released and when the cursor moved and when the map scrolled.

and it sais scrolled-left -1 that shouldn't be possible...
Offline 2playgames

Junior Duke





« Reply #4 - Posted 2007-07-07 13:36:11 »

Quote
When i set a breakpoint my frame goes white and i cannot move or see my cursor.
that's the purpose. a breakpoint pauses your app so you can go in the debugger and examine the values of some variables

Offline keldon85

Senior Duke


Medals: 1



« Reply #5 - Posted 2007-07-07 16:47:42 »

Tip: you often find that rewriting from scratch takes less time!

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.

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

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!