Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  [Solved] Question with map handling & help w/ camera sliding in a 2d tile engine  (Read 329 times)
0 Members and 1 Guest are viewing this topic.
Offline crumpetxxix

Junior Newbie





« Posted 2013-04-13 21:53:22 »

Hi guys, first post here.  Ok so im creating a 2d tile based game which uses a camera to slide the map the opposite direction the player is trying to walk in order to keep the character centered in the screen.  Sounds simple right? It is, this isnt where im having any issues.

My first question is kind of a general question about how i should impliment maps since mine seems to not be working so well and ive noticed some random pauses in my game (not sure if that has to do with the key input handling though)..

Would it be better to have a map load from a text file or to have a pre defined map within the code.  I feel like having the map as a saved text file would be nice because if i feel like making any event within the game that happens (perhaps an explosion in a mountain wall which reveals a cave enterance) i can perminantly change the environment, but having the map defined within the code seems like it would allow the game to run smoother.

Anyways for my question. How i was currently doing my maps was that the map and all the map data was placed in a text file with region ID, X, and Y, and the Tile Type.  Then the game would load the camera cells tile by tile, loading just enough of the tiles from the DB to fill the viewable screen, with a little bit of a buffer off to the side in order for tiles sliding on / off the screen.
So the player loads, then the tiles for the active screen loads, i can walk camera slides and the camera slides like it should, now here comes the problem.  When the camera slides, one row or column of tiles gets knocked out of the active camera cell array when they get pushed off the side of the screen and all the tiles within the camera cells are supposed to slide over by one, depending on the direction traveled, and then the empty row or column of cells gets a new tile loaded


Heres an example of the player moving up (camera slides down) of me trying to swap the tiles within the camera cells and then loading a new set of tiles after moving, im just showing the cameraUpdate because all the other code works fine and ive narrowed it down to this select piece of code

So the player moves up 1 and the camera gets moved down, activeCells
  • [y] is an array of camera cells, camXSize and camYSize are just static ints to show how many cells are in the arrays.  0, 0 in the array is in the top left corner of the screen, x increases every cell to the right, y increases every cell down
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  
    public void updateCamera(int dir)
    {
        //Just showing the example of a player walking up because its the same code for every direction
        switch (dir)
        {
            case 0:
            {
                for (int x = 0; x < camXSize; x++)
                {
                    // This would be the bottom row of the camera cells, these will be "knocked-off" since there is no cell below it to hold this tile
                    // Instead, just give it the tile before it.
                    activeCells[x][camYSize - 1].currentTile = activeCells[x][camYSize - 2].currentTile;
                    activeCells[x][camYSize - 1].currentTile .cameraX = activeCells[x][camYSize - 1].cellXPos;
                    activeCells[x][camYSize - 1].currentTile .cameraY = activeCells[x][camYSize - 1].cellYPos;
                    for (int y = camYSize - 2; y > 0; y--)
                    {
                        // Already took care of the bottom row of cells so it starts at the 2nd last row, giving it the previous row
                        activeCells[x][y].currentTile = activeCells[x][y - 1].currentTile;
                        activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
                        activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
                    }
                    // Finally we get to the top row, since there was nothing above this row to have sliden into these cells, we gotta load a new cell instead
                    activeCells[x][0].currentTile = dbMng.loadTile(activeCells[12][10].currentLiving.regID, activeCells[x][1].currentTile.xLoc, activeCells[x][1].currentTile.yLoc - 1);
                    activeCells[x][0].currentTile.cameraX = activeCells[x][0].cellXPos;
                    activeCells[x][0].currentTile.cameraY = activeCells[x][0].cellYPos;
                }
                break;
            }
            default:
                break;
        }
    }


What is supposed to happen is player clicks a directional key to move -> camera slides + game animates -> after the last step of the animation / camera slide the camera updates which tiles are in which cells, then the game checks for any events at that coordinate.. what actually happens is because of the camera update tiles start disappearing and occasionally some of the default grey tiles i made appear in random areas.. not quite sure what i did wrong in this code. any info will help. if i need to post more of my code let me know

edit:  Well i scrapped trying to switch the cells based off of the cells x and y coordinates and updated the new tiles based on the character position

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  
    public void updateCamera(int dir) throws IOException
    {  
        int xMin = activeCells[12][10].currentLiving.xLoc - 12,
                xMax = activeCells[12][10].currentLiving.xLoc + 12,
                yMax = activeCells[12][10].currentLiving.yLoc + 10,
                yMin = activeCells[12][10].currentLiving.yLoc - 10;
        //Just showing the example of a player walking up because its the same code for every direction
        switch (dir)
        {
            case 0:
            {
                for (int y = camYSize - 1; y >= 0; y--)
                {
                    for (int x = 0; x < camXSize; x++)
                    {
                        if (y == 0)
                        {
                            activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, (xMin + x), yMin);
                        }
                        else
                        {
                            activeCells[x][y].currentTile = activeCells[x][y - 1].currentTile;
                        }
                        activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
                        activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
                    }
                }
                break;              
            }
            case 1:
            {    
                for (int x = 0; x < camXSize; x++)
                {
                    for (int y = 0; y < camYSize; y++)
                    {
                        if (x == camXSize - 1)
                        {
                            activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, xMax, (yMin + y));
                        }
                        else
                        {
                            activeCells[x][y].currentTile = activeCells[x + 1][y].currentTile;
                        }
                        activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
                        activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
                    }
                }        
                break;                          
            }
            case 2:
            {    
                for (int y = 0; y < camYSize; y++)
                {
                    for (int x = 0; x < camXSize; x++)
                    {
                        if (y == camYSize - 1)
                        {
                            activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, (xMin + x), yMax);
                        }
                        else
                        {
                            activeCells[x][y].currentTile = activeCells[x][y + 1].currentTile;
                        }
                        activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
                        activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
                    }
                }        
                break;                          
            }
            case 3:
            {
                for (int x = camXSize - 1; x >= 0; x--)
                {
                    for (int y = 0; y < camYSize; y++)
                    {
                        if (x == 0)
                        {
                            activeCells[x][y].currentTile = testGame.dbMng.loadTile(activeCells[12][10].currentLiving.regID, xMin, (yMin + y));
                        }
                        else
                        {
                            activeCells[x][y].currentTile = activeCells[x - 1][y].currentTile;
                        }
                        activeCells[x][y].currentTile.cameraX = activeCells[x][y].cellXPos;
                        activeCells[x][y].currentTile.cameraY = activeCells[x][y].cellYPos;
                    }
                }
                break;              
            }
            default:        
                break;    
        }  
    }


Still cant figure out quite why the other method didnt work but everything is running smoothly now
Offline crumpetxxix

Junior Newbie





« Reply #1 - Posted 2013-04-13 22:18:23 »

Interesting, this code works I think..  I wrote this from memory instead of copying and pasting and I think I may have made a copy + paste error in my code because this one seems to work,  one key thing from before is i forgot to set the camera pixel coordinates so i believe they were defaulting to 0.f, 0.f on the screen, which is why i would get random gray boxes at that coordinate all the time

Edit: ok i should have tested it first, doesnt quite work but at least im not getting all blackness any more and my default gray tile is loading
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.

toopeicgaming1999 (16 views)
2014-11-26 15:22:04

toopeicgaming1999 (15 views)
2014-11-26 15:20:36

toopeicgaming1999 (7 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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!