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  
  [LibGDX] Camera movement and input processing  (Read 1059 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk
« Posted 2013-04-18 02:57:31 »

I put a big picture in the background to learn the camera movements. It works good when I handle key input from within the class, but when I try to handle input using another class, it partially works. To describe the 'partially working' thing, I have to post the code. So here they are:
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  
public class World implements Screen {

   OrthographicCamera camera;
   Texture texture;
   SpriteBatch batch;
   TextureRegion image;
   InputTaker input;

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 2, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      camera.update();
      batch.begin();
      batch.draw(image, 0, 0);
      batch.end();
   }

   @Override
   public void resize(int width, int height) {
     
   }

   @Override
   public void show() {
      input = new InputTaker(this);
      Gdx.input.setInputProcessor(input);
      camera = new OrthographicCamera();
      batch = new SpriteBatch();
      texture = new Texture(Gdx.files.internal("data/back.jpg"));

      image = new TextureRegion(texture, texture.getWidth(), texture.getHeight());

      camera.setToOrtho(false, 600, 400);
      camera.update();

      batch.setProjectionMatrix(camera.combined);

   }
// Other methods are omitted, they were empty anyway, except for dispose().
}


This is the class where I handle input:
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  
public class InputTaker implements InputProcessor {
   World world;

   public InputTaker(World world) {
      this.world = world;
   }

   @Override
   public boolean keyDown(int keycode) {
      switch (keycode) {
      case Keys.UP:
         if (world.camera.position.y < world.image.getRegionHeight() - Gdx.graphics.getHeight() / 2) {
            world.camera.position.y += 20;

         }
         break;
      case Keys.DOWN:
         if (world.camera.position.y > Gdx.graphics.getHeight() / 2) {
            world.camera.position.y -= 20;
         }
         break;
      case Keys.LEFT:
         if (world.camera.position.x > 0 + Gdx.graphics.getWidth() / 2) {
            world.camera.position.x -= 20;
         }
         break;
      case Keys.RIGHT:
         if (world.camera.position.x < world.image.getRegionWidth() - Gdx.graphics.getWidth() / 2) {
            world.camera.position.x += 20;
         }
         break;
      default:
         break;
      }
      world.batch.setProjectionMatrix(world.camera.combined);

      return true;

   }
//Other methods are omitted.


Well, it does not move to left and to down. Right and up are okay. So I thought it should be something about Gdx.graphics.getWidth() and getHeight().

libGDX calls resize(int width, int height) once after the screen is called. So I tried to get the width and height from there. But it did not change anything. Still there are no movement to left and down.

The funny thing is it worked when I put it in the same class. It is easy and dirty, but here it is:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public void handleTouch() {
      if (Gdx.input.isKeyPressed(Keys.UP) && camera.position.y < image.getRegionHeight() - Gdx.graphics.getHeight() / 2) {
         camera.position.y += 20;
      } else if (Gdx.input.isKeyPressed(Keys.DOWN) && camera.position.y > 0 + Gdx.graphics.getHeight() / 2) {
         camera.position.y -= 20;
      } else if (Gdx.input.isKeyPressed(Keys.LEFT) && camera.position.x > 0 + Gdx.graphics.getWidth() / 2) {
         camera.position.x -= 20;
      } else if (Gdx.input.isKeyPressed(Keys.RIGHT)
            && camera.position.x < image.getRegionWidth() - Gdx.graphics.getWidth() / 2) {
         camera.position.x += 20;
      }
      batch.setProjectionMatrix(camera.combined);
   }


What am I missing?

Other than that, when I use a separate class to handle events, I want it to continuously do what I want.
Let me explain (Damn not being native English speaker): Now, as one would expect, when I hit up, it only moves once until I remove my finger and press again. But I want it to do this continuously until I remove my hand off the key.
I tried a while loop, but it gave exception (Stack overflow).

It worked the way I wanted when I handled the input in the same class. This was because the handleTouch() method was inside of the game loop (render method I mean). But let's face it, I will have to use a separate class to handle input one day or another Tongue

Thanks in advance guys!
Offline kutucuk
« Reply #1 - Posted 2013-04-18 07:18:05 »

OK, I fixed what I called continuous movement. I just added a method to move the camera and added a condition (moveCam boolean). But the left and down movement problem persists. Now it can go left and down, but it stops after a while and I don't know why.

EDIT: Actually I solved that issue as well Cheesy

EDIT 2: No, it is not exactly solved. So help is appreciated Cheesy
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 (43 views)
2014-10-16 15:22:06

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

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

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

TehJavaDev (67 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!