Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Newbie & Debugging Questions / Re: LibGDX box2D Moving Platfromer and Player on: 2019-06-23 23:02:40
If I may, I would recommend use State Pattern for your player states, otherwise you gonna have a bunch of flags around your inputs and that's really error prone.

something like:
1  
2  
3  
JumpingState
onGroundState
onPlatformState


The State class can be a nested class, for example.

Then you just use inputs for each state and don't need to worry about flags all around the house.

Good lecture: http://gameprogrammingpatterns.com/state.html

Regards!
2  Java Game APIs & Engines / Java 2D / Re: Drawing a Tech Tree on: 2019-04-16 01:27:11
Hi, Longarmx,

thanks for your answer. It never occourred to me to simply draw it beforhand, lol.

Actually, I still don't know how it will be. It will not exactly dynamic, but if it's too big, may be I could use just some "cuts", but I think it will be less work with the image.

These algorithms are really annoying.

Thanks again!

3  Java Game APIs & Engines / Java 2D / Drawing a Tech Tree on: 2019-04-11 18:57:56
Hi, guys!

I'm developing a game that is going to have a tech tree (like Civilization, for example);

So, I began to search how to draw and discovered that this is quite tricky. A tech tree is actually a directed acyclic graph. So, we have to draw a graph. I found tons of academic material, but most of them grows in complexity, since they try to optimize the code (in terms of Big O).

So, I would like to know if anyone knows a working algorithm that is not tooooooooo complex to implement. For now, I don't need to be optimized.

Thanks!
4  Game Development / Newbie & Debugging Questions / Re: Inventory System? on: 2019-01-02 15:56:40
With using an Enum, I would need to have a switch statement to decide how to render the item, wouldn't this slow performance the more items I add?


If you just want to render(), then you need to use polymorphism, where each class should render their own way. So, yes, you'll probably gonna haver a LOT of classes.

But things can be smarter: you can, for example, name your files the same name as the Enums. This way, you just need to call something like this:

In the constructor:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class Item {

      private itemTypeEnum type;
      private Texture texture;

      public Item(itemType type){

             this.type = type;
             //now the smart thing:
             String myFileTexture = type.toString();
             texture = SomeLoadMethod(myFileTexture);
             //other stuff....

      }

      //other methods
}


And the render will be the same to everyone:

1  
2  
3  
4  
5  
public void render() {

        graphicsEngine.render(texture);

}


That is just an idea, but you can do in many ways.
5  Discussions / General Discussions / Re: Where to get a spot for game developing? on: 2018-12-26 04:00:57



If your laptop's keyboard doesn't sound like rock drums, then you'll cause no problem to the others.

When I was in college, everytime that I really needed to focus, I always went to the library. It really works.
6  Game Development / Newbie & Debugging Questions / Re: Persistent Data in a fat jar on: 2018-12-25 23:03:45
Oh, not only the json files, it's all the data. It's a really fat jar, lol
7  Game Development / Newbie & Debugging Questions / Re: Persistent Data in a fat jar on: 2018-12-24 18:30:03
I read some about and actually it's not well advised to use a Fat Jar. For now, I'm using, but in the future I most likely will not use a fat jar.
8  Game Development / Newbie & Debugging Questions / Re: Persistent Data in a fat jar on: 2018-12-24 13:24:48
Hi, guys! Thanks for all!

Another option I'm thinking: if I unzip the jar and create a *.bat file, it could work?
9  Game Development / Newbie & Debugging Questions / Re: Persistent Data in a fat jar on: 2018-12-23 00:32:02
Hello, VaTTeRGeR!

I just extracted the data folder from the jar and let it at the same folder, like:
/mygame.jar
/data/config.json

And now it works fine! Thanks!
10  Game Development / Newbie & Debugging Questions / Re: Shipping a Gradle project on: 2018-12-22 17:21:04
Hi, sugarblood! =]

I solved using the gradlew. Only need to export this way

1  
gradlew desktop::dist


Just type this in the command line and it`s solved! =]
11  Game Development / Newbie & Debugging Questions / Persistent Data in a fat jar on: 2018-12-22 17:18:34
Hi, guys!

I created a fat jar for my game and I use JSON to save and load data.

When the app runs from Netbeans, everthing works fine.

But when I run the app from the fat jar generated, I can't save data anymore. I still can read and load, but can't save.

The code is inside a try/catch and I got this exception:

java.io.FileNotFoundException: data\savegame.json (The system cannot find the path specified)

The "funny" thing is that actually it can find the path, since it loads data from the same file. I've checked already that the files are not "Read only".

Someone knows what is happening?
12  Discussions / General Discussions / Re: Where to get a spot for game developing? on: 2018-12-21 16:21:15
Library
13  Game Development / Newbie & Debugging Questions / Re: Shipping a Gradle project on: 2018-12-19 01:50:15
Hi,  VaTTeRGeR,

me again! I don't know what`s happening, but from the desktop, the fat jar don't runs.

But if I create a simple folder in the desktop and move the fat jar to there, THEN, the game runs.

Am I missing something about Java and Windows Desktop (win 10)?
14  Games Center / Showcase / Re: Android Game Released - Leap on: 2018-12-17 19:32:50
Nice work! I found it quite hard, lol.

Tip: the "click" sound (when press buttons) is kinda annoying; I think is too sharp; try one "smoother" ;-)
15  Game Development / Game Mechanics / Re: Best way to make 2D Platformer collision? on: 2018-12-14 22:38:44
There are a lot of ways how you can do it. Since is for learning, you can check links below, but to use in a real world, I think Box2D is the answer.

http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

https://katyscode.wordpress.com/2013/01/18/2d-platform-games-collision-detection-for-dummies/

That`s a nice intro.

Regards
16  Game Development / Game Mechanics / Re: Continuous 2D Worlds using chunks and rendering. on: 2018-12-14 22:34:56
Hello, unenergizer,

About your link, it's about TWO-D graphics using a map format made with the software Tiled. https://www.mapeditor.org/

Regarding you doubt, render a map is quite easy, you just need to walk through the 2d array rendering each element. This can help: https://gamedevelopment.tutsplus.com/tutorials/an-introduction-to-creating-a-tile-map-engine--gamedev-10900

And to make the map move with the player, you will need a camera. Just need to move the camera as the player moves.

Regards

17  Game Development / Newbie & Debugging Questions / Re: Bug with states in LIBGDX on: 2018-12-08 03:17:40
So, just going back, because i found the real problem; it was in the Gdx.input.setProcessor();

Each time I change a state, I must call this method again to the new Stage;

My game is now much more advanced and I`m actually using the Multiplex, but that`s the same idea.

18  Game Development / Newbie & Debugging Questions / Re: Shipping a Gradle project on: 2018-12-08 03:08:01
Thank you, man!

But the word that I was searching is actually "deploy" lol

For record: need the gradlew to work. The gradle only was not working!

Thanks again!

19  Game Development / Game Play & Game Design / Re: Placing houses on random generated map? on: 2018-12-07 21:19:15
Hi, GustavXIII!

The way I usually do is for every object I've created in the map, it will hold his position AND size, like (x, y, width, height). And all of them will be added to a List (Array, LinkedList, etc)

So, any time you will try to create an object, you check before, using simple rectangle collision between the new one and all of the array, like so:

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  
class ObjectCreator(){
   
   public static void createObject(){

      MyObject newObject = new MyObject(random_x, random_y, type_of_object_width, type_of_object_height);

      boolean collide = false;
     
      for(MyObject myobj : arrayOfMyObjects){
         if(collides(myobj, newObject)){
            //OBJECTS ARE COLLIDING, SO YOU DON'T ADD            
            collide = true;
            return; //just one collide is enough, don't need to continue walkin the array
         }
      }
     
      if(!collide){
         arrayOfMyObjects.add(newObject);
      }

   }
   public boolean collides(MyObject A, MyObject B){

      Rectangle rA = new Rectangle(A.x, A.y, A.width, A.height);
      Rectangle rB = new Rectangle(B.x, B.y, B.width, B.height);

      return rA.intersects(rB);
   }
   
}


This way you can have multiple objects with different sizes.

Hope it can help!
20  Game Development / Newbie & Debugging Questions / Re: Spritesheet with animations of differing number of frames on: 2018-12-07 20:56:11
Hey, Sean!

If you're hard coding (and not using a framework), the best way is to create a class for your 2D animations. The main variables should be:

- The spritesheet itself

It can be a simple array of images. And just it. You can use one spritesheet to each animation or a game spritesheet. For individual files is actually easy to work, but not much efficient.
A huge spritesheet that will hold all of the game images is the usual approach. You just need to have a logic to point to each animation. In that case, you can have some variable like "INICIAL_FRAME".

- The number of frames

If you're using the big spritesheet, so you will need the number of frames. Could be the array size, for exemple.

- The animation's speed

That's an important one. Some times you want to change some of the animation speed, so could be useful.

- The kind of animation

I don't know if "kind" is the right word, but I mean how the frames go. If you have, for example, 4 frames (1, 2, 3 and 4), the usual animation will play in that order:

1, 2, 3, 4 - repeat. This is "LOOP" kind

But you can have some other cool types, like "PING_PONG", like this:

1, 2, 3, 4, 4, 3, 2, 1 - repeat

You can also use "REVERSED_LOOP",like:

4, 3, 2, 1 - repeat

If you are creating your own class, keep this in mind. All of this will be useful in the future.

Here is an example of the LIBGDX class Animation; you can take some ideas from it;
https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Animation.html

Hope this could help!
21  Game Development / Newbie & Debugging Questions / Shipping a Gradle project on: 2018-12-07 19:29:35
Hi, all,

it's a very simple question, but I really can't finding the answer.

How can I "export" my game to send to other people to play? In other words, create an executable game.

I'm using LIGBDX and Gradle.

ps - the people shall not have Gradle installed.
22  Java Game APIs & Engines / OpenGL Development / Re: [LibGDX] Textures and Zoom Out on: 2018-05-18 00:06:27
Hi VaTTeRGeR,

I enabled mipmap generation and now it works perfect! Actually, is better than Paint using MipMap/MipMapLinearNearest.

Thank you very much!
23  Java Game APIs & Engines / OpenGL Development / Re: [LibGDX] Textures and Zoom Out on: 2018-05-16 13:53:00
Have you tried enabling mipmap generation upon texture load and then using one of the the mipmap filter options?

It could also be possible that paint does the filtering in software, so you can't just use one of the default filters to get the same effect.

A last ditch effort would be to generate scaled down versions offline and load them as mipmaps or just load them as normal textures and do the swapping yourself depending on the zoom level.

I was thinking that mipmap only works when you already have the images scaled (I'm new at openGL, sorry). I will try that later.

Thanks!
24  Java Game APIs & Engines / OpenGL Development / [LibGDX] Textures and Zoom Out on: 2018-05-16 01:14:31
Hi, guys!

I'm using LibGDX to render my game and at some point I call the following code to render:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
render(){

        Gdx.gl.glClearColor(0, 0.1f, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         
        camera.update();
        mapRenderer.setView(camera);
        mapRenderer.render();
       
        batch.setProjectionMatrix(camera.combined);
       
        batch.begin();

            for(GameObject o : tiledWorld.objects){                
                o.getGraphics().render(o, batch);
            }    
       
        batch.end();
       
        stageGame.act();
        stageGame.draw();
}


The render(o, batch) just renders TextureRegions from the GraphicComponent. I'm getting my textures from there.

When the camera zoom out, it seems very pixelated. I tried different setFilter() options (TextureFilter.Linear/TextureFilter.Nearest), as you can see in that follow image:

https://imgur.com/a/LLx6zno



I would like that textures look like when I have the 12.5% zoom out in the Paint (as in the example, it's much better).
If you zoom in, it's possible to see that there are differences between the different combinations at setFilter(). It seems to me that Linear/Nearest is the best, but it is far from the best result like in Paint.

Someone could help or have any ideas?
25  Game Development / Newbie & Debugging Questions / Re: Could someone explain to me the update method? on: 2018-04-17 00:51:50
I recommend these two links to a better understand for games in general:

http://gameprogrammingpatterns.com/game-loop.html
http://gameprogrammingpatterns.com/update-method.html

And also recommend this whole book, lol

Regards
26  Game Development / Newbie & Debugging Questions / Re: Best LibGDX course / book ? on: 2018-04-17 00:49:35
There are two books that can help you, but I think there are a lot of videos and etc in Youtube.

Learning LibGDX Game Development (it uses a cross-platform game all the book)
Mastering LibGDX Game Development (it uses a rpg game)

You can also look that series, I will certainly help:
http://www.gamefromscratch.com/page/LibGDX-Tutorial-series.aspx

Regards,
27  Game Development / Newbie & Debugging Questions / Re: [LibGDX] TiledMap, Textures and Zoom on: 2018-04-16 19:52:44
So, I finally figure out a solution. First, not using a Texture. Now I'm using a TextureMapObject. That way, is a must to create a MapObjectLayer also in Tiled.

Include the batch.setProjectionMatrix(camera.combined) before batch.begin().

And also draw manually using batch.draw(). It follows the final code for anyone struggling:

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  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
public class TiledTest extends ApplicationAdapter {
   
    private static final String TAG = TiledTest.class.getName();
   
    TextureMapObject tmo;
    TiledMap tiledMap;    
    OrthographicCamera camera;    
    TiledMapRenderer tiledMapRenderer;
    SpriteBatch batch;    
    Texture texture;
    Actor actor;
    Stage stage;

   
   
    @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
       
        batch = new SpriteBatch();
        camera = new OrthographicCamera();
       
        camera.setToOrtho(false,w,h);
        camera.update();        
       
       
        InternalFileHandleResolver handle = new InternalFileHandleResolver();
        handle.resolve("mapa_teste.tmx");
        TmxMapLoader loader = new TmxMapLoader(handle);        
        tiledMap = loader.load("img/mapas/mapa_teste.tmx");
       
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
       
        //TextureMapObject
        texture = new Texture(Gdx.files.internal("img/tanque_192x192.png"));
        tmo = new TextureMapObject(new TextureRegion(texture,192,192));
        tmo.setX(128);
        tmo.setY(128);
        tiledMap.getLayers().get("Objects").getObjects().add(tmo);
        stage = new Stage();
        actor = new Actor();
        actor.setBounds(128,128, tmo.getTextureRegion().getRegionWidth(), tmo.getTextureRegion().getRegionHeight());
        actor.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                Gdx.app.log(TAG, "Tanque clicado!");
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, x, y, pointer, toActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse saindo do actor!");                
            }

            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse entrando no actor!");                
            }
                     
        });
        stage.addActor(actor);                
        Gdx.input.setInputProcessor(stage);
        stage.getViewport().setCamera(camera);
        stage.setDebugAll(true);        

       
    }

    @Override
    public void render () {

        input();
       
        Gdx.gl.glClearColor(0, 0.5f, 0.5f, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();
       
        batch.setProjectionMatrix(camera.combined);
        batch.begin();        
       
        batch.draw(tmo.getTextureRegion(), tmo.getX(), tmo.getY(),
                tmo.getOriginX(), tmo.getOriginY(), tmo.getTextureRegion().getRegionWidth(), tmo.getTextureRegion().getRegionHeight(),
                tmo.getScaleX(), tmo.getScaleY(), tmo.getRotation());      
       
        batch.end();
       
       
        stage.act();
        stage.draw();        


    }

   
    public void input(){
       
        float cameraSpeed = 7f;
       
        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            camera.translate(0f, +cameraSpeed);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            camera.translate(0f, -cameraSpeed);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            camera.translate(-cameraSpeed, 0f);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            camera.translate(+cameraSpeed, 0f);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.F2)){
            camera.zoom += 0.01f;
        }if(Gdx.input.isKeyPressed(Input.Keys.F3)){
            camera.zoom -= 0.01f;
        }
        if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)){
            stage.dispose();
            batch.dispose();
            tiledMap.dispose();            
            Gdx.app.exit();
        }
       
    }
   
}
28  Game Development / Newbie & Debugging Questions / [LibGDX] TiledMap, Textures and Zoom on: 2018-04-15 18:30:35
Hi guys!

I'm creating a builder style game and using TiledMap and LIBGDX. So, I'm just testing the camera and translation and having some issues with zoom. All works perfect without changing the zoom. But when I change it, the Texture/Sprite becomes misaligned with the Tiled. The actors continue just fine, but the textures/sprites not. I don't know how to correct the scale in the update.

So, how could I correct the zoom for the TextureRegion (and Sprites)? My code is above:

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  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
public class TiledTest extends ApplicationAdapter {
   
    private static final String TAG = TiledTest.class.getName();
   
    Texture img;
    TiledMap tiledMap;
    OrthographicCamera camera;    
    TiledMapRenderer tiledMapRenderer;
    SpriteBatch batch;
    Sprite sprite;
    Actor actor;
    Stage stage;
    boolean init = false;
   
    @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        batch = new SpriteBatch();
        camera = new OrthographicCamera();

       
        camera.setToOrtho(false,w,h);
        camera.update();
       
       
        InternalFileHandleResolver handle = new InternalFileHandleResolver();
        handle.resolve("mapa_teste.tmx");
        TmxMapLoader loader = new TmxMapLoader(handle);        
        tiledMap = loader.load("img/mapas/mapa_teste.tmx");
       
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);        
       
        sprite = new Sprite(new Texture(Gdx.files.internal("img/tanque_192x192.png")));
        sprite.setBounds(128, 128, sprite.getTexture().getWidth(), sprite.getTexture().getHeight());
        stage = new Stage();
        actor = new Actor();
        actor.setBounds(128,128, 192, 192);
        actor.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                Gdx.app.log(TAG, "Tanque clicado!");
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, x, y, pointer, toActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse saindo do actor!");                
            }

            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse entrando no actor!");                
            }
                     
        });
        stage.addActor(actor);                
        Gdx.input.setInputProcessor(stage);
        stage.getViewport().setCamera(camera);
        stage.setDebugAll(true);
       
       
    }

    @Override
    public void render () {

        input();
       
        Gdx.gl.glClearColor(0, 0.5f, 0.5f, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();
       
        batch.begin();
        sprite.draw(batch);
        batch.end();
       
        stage.act();
        stage.draw();        

    }

   
    public void input(){
       
        float cameraSpeed = 7f;
       
        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            camera.translate(0f, +cameraSpeed);
            sprite.translate(0f, -cameraSpeed);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            camera.translate(0f, -cameraSpeed);            
            sprite.translate(0f, +cameraSpeed);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            camera.translate(-cameraSpeed, 0f);            
            sprite.translate(+cameraSpeed, 0f);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            camera.translate(+cameraSpeed, 0f);            
            sprite.translate(-cameraSpeed, 0f);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.F2)){
            camera.zoom += 0.01f;
            //sprite.scale(camera.zoom);
        }if(Gdx.input.isKeyPressed(Input.Keys.F3)){
            camera.zoom -= 0.01f;            
            //sprite.scale(camera.zoom);
        }
        if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)){
            stage.dispose();
            batch.dispose();
            tiledMap.dispose();
            sprite.getTexture().dispose();
            Gdx.app.exit();
        }
       
    }
   
}
29  Game Development / Newbie & Debugging Questions / Re: Bug with states in LIBGDX on: 2017-11-25 05:22:38
Ok, I just found the problem, lol.

I need to call dispose() before change to the new State.

OK to that, but still crazy for me, the LevelState does not has any information about the MainMenuState. Whatever, I'm studying this libgdx for about a month or two...
30  Game Development / Newbie & Debugging Questions / Bug with states in LIBGDX on: 2017-11-25 05:03:13
Hi, people!

I'm new at game development. I just wrote some games to learn the basic structure and now I'm trying to go with a real game.

Well, I'm using libgdx. I created my own Game State Manager instead of using the Game/Screen, because they don't save state. That way, I'm using an abstract class called GameState:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public abstract class GameState {
   
    protected GameStateManager gsm;
   
    public abstract void update();
    public abstract void render();
    public abstract void dispose();
   
}


The GameStateManager class is just a Stack:

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  
public class GameStateManager {
   
    public Stack<GameState> states;
   
    public GameStateManager(){
        System.out.println("Constructor do "+this.toString()+" criado");
        states = new Stack<GameState>();
       
    }
   
    public void addState(GameState state){
        states.push(state);
    }
   
    public void setState(GameState state){        
       
        if(!states.empty()){            
            states.pop();
            states.push(state);
        } else {
            states.push(state);
        }
       
    }
   
    public void removeState(){
        if(!states.empty()) {states.pop();}
    }
   
    public GameState getState(){
        return states.peek();
    }
   
    public void update(){
        states.peek().update();
    }
   
    public void render(){
        states.peek().render();
    }
   
    public void dispose(){
        states.peek().dispose();
    }
   
}



And the main game class is pretty simple:

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  
public class MainGame extends ApplicationAdapter {
   
        private static final String TAG = Chemeng.class.getName();
       
        private GameStateManager gsm;
   
   @Override
   public void create () {
           
            gsm = new GameStateManager();
            gsm.setState(new SplashState(gsm));            
           
   }

   @Override
   public void render () {
           
            input();
           
            gsm.update();
            gsm.render();
           
            System.out.printf(" GSM %s: %d%n", gsm.states.peek().toString(), this.gsm.states.size());
   }
   
   @Override
   public void dispose () {
            gsm.dispose();
   }
       
        private void input(){
            if(Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
                Gdx.app.exit();
            }
        }
       
}


It's working fine, besides a crazy loop for me. I have the MainMenuState where the player will choose their options (play campaing, tutorials, load, save, settings, etc, etc). One of them take to the LevelState, what also works fine.

The problem is at the LevelState. Somehow, if we click at the same area from the button in the MainMenuState it works! But the LevelState there is no Stage and no listeners! I checked the GameStateManager and etc and nothing makes sense to me. Anyone knows how it's happening?

MainMenuState:

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  
public class MainMenuState extends GameState {
   
    private Stage stage;
   
    private Skin skin;
    private TextureAtlas uiAtlas;
   
    private Image imgBackground, imgCampanha, imgTutorial, imgCenario;
    private Image imgSandbox, imgConfig, imgCreditos;
       
   
    public MainMenuState(GameStateManager gsm){
        System.out.println("Constructor do "+this.toString()+" criado");
       
        this.gsm = gsm;
       
        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);
       
        uiAtlas = new TextureAtlas(
                Gdx.files.internal("skin/uiskin.atlas"));
       
        skin = new Skin(
                Gdx.files.internal("skin/uiskin.json"),
                uiAtlas);
       
        Texture background = new Texture("img/menu/chemical_plant_menu_background.jpg");
        imgBackground = new Image(background);
        imgBackground.addAction(Actions.alpha(0.15f));        
       
        imgCampanha = new Image(new Texture("img/menu/executive_menu.png"));
        imgCampanha.setPosition(750, 500);
        imgCampanha.addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
                System.out.println("Imagem da campanha clicada...");                
                teste();
                return true;
            }
        });
               
        imgTutorial = new Image(new Texture("img/menu/estudante_menu.png"));
        imgTutorial.setPosition(710, 250);
       
        imgCenario = new Image(new Texture("img/menu/engineer_menu.png"));
        imgCenario.setPosition(480, 230, 10);
       
        imgSandbox = new Image(new Texture("img/menu/crazy_scientist_menu.png"));
        imgSandbox.setPosition(50, 520);
       
        imgConfig = new Image(new Texture("img/menu/quimica_menu.png"));
        imgConfig.setPosition(50, 50);
       

        stage.addActor(imgBackground);
        stage.addActor(imgCampanha);
        stage.addActor(imgTutorial);
        stage.addActor(imgCenario);
        stage.addActor(imgSandbox);
        stage.addActor(imgConfig);
       
        stage.addAction(Actions.alpha(0));
        stage.addAction(Actions.fadeIn(2));
       
       
    }
   
    private void teste(){
        System.out.println("M├ętodo teste() chamado;");
        System.out.println("Removendo Actor");
        stage.addAction(Actions.removeActor(imgCampanha));
        imgCampanha = null;
        System.out.println("Atualizando GSM para o novo LevelState");
        gsm.setState(new LevelState(gsm));
    }

    @Override
    public void update() {

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0.10f, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

    @Override
    public void dispose() {
        stage.dispose();        
    }
   
}


LevelState:

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  
public class LevelState extends GameState {

    private OrthographicCamera camera;
    private SpriteBatch batch;
   
    private ShapeRenderer shaperenderer;
   
    private Texture map;
   
    public LevelState(GameStateManager gsm){
        System.out.println("Constructor do "+this.toString()+" criado");
        this.gsm = gsm;

        batch = new SpriteBatch();
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
       
        camera.translate(camera.viewportWidth/2, camera.viewportHeight/2);
        camera.update();
       
        map = new Texture(Gdx.files.internal("img/map_test_camera.png"));        
       
        shaperenderer = new ShapeRenderer();
       
    }
   
    private void drawUI(){
       
        shaperenderer.begin(ShapeRenderer.ShapeType.Filled);
       
        shaperenderer.setColor(Color.FIREBRICK);
       
        shaperenderer.rect(Gdx.graphics.getWidth()-120, 0, 120, Gdx.graphics.getWidth());        
        shaperenderer.rect(0, 0, Gdx.graphics.getWidth(), 100);
       
        shaperenderer.end();        
       
    }
   
    @Override
    public void update() {
       
    }

    @Override
    public void render() {
       
        Gdx.gl.glClearColor(0, 0.1f, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(map, 0, 0);
        batch.end();
       
        drawUI();
       
        camera.translate(1f, 1f);
       
    }

    @Override
    public void dispose() {
        batch.dispose();
        map.dispose();
        shaperenderer.dispose();
    }
   
}


I even tried to remove the Actor that calls the listener as you can see at teste() method, but even this doesn't work.

I'm completly lost =[
Pages: [1] 2
 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5545 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4585 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4017 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!