Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am beginner in libgdx and trying to experiment .I am using the simple example here .So, I have 2 questions:

1) I want to use my background picture.So, I have a class Assets where I have all the assets.And I use:

public static void load () {
        background = loadTexture("background.png");
        backgroundRegion = new TextureRegion(background, 0, 0, 800, 480);

        ...
        dropImage = new Texture(Gdx.files.internal("droplet.png"));
        ...

In GameScreen class:

public class GameScreen implements Screen {
....
public GameScreen (Game game) {
        this.game = game;

   // create the camera and the SpriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        batch = new SpriteBatch();

     // create a Rectangle to logically represent the bucket
          bucket = new Rectangle();
....
public void draw (float deltaTime) {
         // Gdx.gl.glClearColor(0, 0, 0.2f, 1);
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

          // tell the camera to update its matrices.
          camera.update();

          batch.setProjectionMatrix(camera.combined);

          // begin a new batch and draw the bucket and all drops
          batch.begin();
...

The problem is that the background doesn't appear (I have it in assets folder as the other files and it is 800x480 pixels).

2) Using

if(Gdx.input.isTouched()) {
             Vector3 touchPos = new Vector3();
             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);

the bucket appears where i press the screen. If I want to make bucket movement smooth,to drug it, I realize that I must use event handling and inputprocessor as here.

But I can't find a way to make it work. I mean , I must create a class MyInputProcessor like the above , then at my drop class (main class) do :

public class Drop extends Game {
   @Override
   public void create() {

            Settings.load();
        Assets.load();

        MyInputProcessor inputProcessor = new MyInputProcessor();
        Gdx.input.setInputProcessor(inputProcessor);

        setScreen(new GameScreen(this));    

and then? How should I use it?

Right now I have in GameScreen class:

public void draw (float deltaTime) {
         // Gdx.gl.glClearColor(0, 0, 0.2f, 1);
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);       
         ...

          // process user input
          if(Gdx.input.isTouched()) {
             Vector3 touchPos = new Vector3();
             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
             camera.unproject(touchPos);
             bucket.x = touchPos.x - 64 / 2;
             bucket.y = touchPos.y - 64 / 2;
          }

Also , the MyInputProcessor gives

"The type MyInputProcessor must implement the inherited abstract method InputProcessor.mouseMoved(int, int)"

and at touchMoved

The method touchMoved(int, int) of type MyInputProcessor must override or implement a supertype method

I want to use

touchDragged

inside GameScreen class?or in MyInputProcessor?How should I do it?

Thank you!

-----------------------UPDATE----------------------------------------------

import com.badlogic.drop.GameScreen;
public class MyInputProcessor implements InputProcessor {

    GameScreen fromgamescreen;

    private Vector3 v = new Vector3(0,0,0);
...
@Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
           v.x = screenX;
           v.y = screenY;
           fromgamescreen.camera.unproject(v);
           fromgamescreen.bucket.x = v.x - 64 / 2;
           fromgamescreen.bucket.y = v.y - 64 / 2;
           return true;


       }
share|improve this question

closed as too localized by Byte56, Sean Middleditch, Josh Petrie, Anko, Nate Jun 13 '13 at 16:44

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Hi George, while using this site try, please, to keep one question per post. It makes for much better answering and searching since the site intends to be a q/a searchable format. –  petervaz Jun 11 '13 at 13:08

1 Answer 1

up vote 1 down vote accepted

1) In the draw method, you need to call batch.draw(backgroundRegion,x,y); to draw the background. Call it after beginning the batch, but before drawing the drops and the bucket.

2) To get rid of the two warnings, change touchMoved to mouseMoved.

Polling, rather than event-based input handling, is probably adequate for your game. However, if you want to use an InputProcessor, you would get rid of this code:

if(Gdx.input.isTouched()) {
             Vector3 touchPos = new Vector3();
             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
             camera.unproject(touchPos);
             bucket.x = touchPos.x - 64 / 2;
             bucket.y = touchPos.y - 64 / 2;
          }

and in the input processor, you would have something like:

    private Vector3 v = new Vector3(0,0,0);
    //you don't want to recreate this vector every time an event comes in: that wastes memory
    ...
    public boolean touchDragged(int screenX, int screenY, int pointer) {
            v.x = screenX;
            v.y = screenY;
            camera.unproject(v);
            bucket.x = v.x - 64 / 2;
            bucket.y = v.y - 64 / 2;
            return true;
        }

To do it this way, the input processor would need references to the camera and bucket.

share|improve this answer
    
:Ok, for the background.For referencing the bucket for example how should I do it?Thanks! –  George Jun 11 '13 at 9:57
    
you could pass the camera and bucket in through the constructor, or you could pass the game screen in and create methods to access the camera and bucket. –  Ian Markowitz Jun 12 '13 at 4:01
    
:I did that but now the bucket doesn't appear.In the place where I remove the "if(Gdx.input.isTouched())",should I put sth ?Or the touchDragged is been called "automatically"? –  George Jun 12 '13 at 7:36
    
:Am I doing sth wrong?Check my update please,thanks! –  George Jun 12 '13 at 7:39
    
I'm not sure why that doesn't work. The problem might be in your rendering code. I can't write the entire application for you. –  Ian Markowitz Jun 12 '13 at 7:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.