0
\$\begingroup\$

Im making a soccer game where you basically have to shoot the ball to the goal and the ball goes to where you touch the screen. Im having an issue with the ball though, when the ball spawns on the player it goes directly to the top left corner of the screen, without even touching the screen. This might be really easy but I cant seem to find the solution

Here's the code:

Ball class

public void shootToward(float targetX, float targetY) {

    velocity.set(targetX - position.x, targetY - position.y);
}

public void update(float deltaTime) {

    position.add(velocity.x * deltaTime, velocity.y * deltaTime);
    velocity.scl(1 - (1f * deltaTime));
}

Game class

public void create () {

    cam = new OrthographicCamera();
    cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch = new SpriteBatch();
    batch.setProjectionMatrix(cam.combined);

    player = new Sprite(player1);
    player.setX(MathUtils.random(Gdx.graphics.getWidth()));
    player.setY(MathUtils.random(Gdx.graphics.getHeight() / 2));

    b.position.set(player.getX(), player.getY());

}

public void render () {
    Gdx.gl.glClearColor(255, 255, 255, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    b.update(Gdx.graphics.getDeltaTime());

    batch.begin();
    batch.draw(bg, 0, 0);
    batch.draw(goal1, Gdx.graphics.getWidth()/4-20, Gdx.graphics.getHeight() - 180, 420, 130);
    if (!Gdx.input.isTouched()) {
        batch.draw(bs, b.position.x, b.position.y);
        b.shootToward(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
    }
    player.draw(batch);
    batch.end();
}

Thanks in advance.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

My guess is that you have a stray ! in your code. Remove the ! in:

if (!Gdx.input.isTouched()) {

This is making it so shootToward() is being called when the player is not touching the screen.

\$\endgroup\$
1
  • \$\begingroup\$ thanks it was actually pretty simple I ended up just doing this: if (!Gdx.input.isTouched()) { batch.draw(bs, b.position.x, b.position.y); }else if (Gdx.input.isTouched()) b.shootToward(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY()); \$\endgroup\$
    – gafflx
    Commented May 4, 2016 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .