0
\$\begingroup\$

I am trying to make an object move from off screen on the right to off screen left and continuously generate (like the flappy bird pipes). How would I do this with my current code below? I have tried creating a class like I did with the "ball" class but I can't seem to get it to work... Any examples or help would be appreciated. (I am very inexperienced in game development and java so please bear with me). This is my current code, I have the ball that jumps in the air and now want to make the spike move from off screen right to off screen left and generate another "spike" each time. I have drawn the spike which at the moment is just a texture that does not do anything. Thanks.

class Ball {

public static final float GRAVITY = -500;
public static final float BOUNCE_DAMPENING = 0.0f;

public final Vector2 position = new Vector2();
public final Vector2 velocity = new Vector2();
public final Vector2 acceleration = new Vector2(0, GRAVITY);

public void update (){

 float deltaTime = Gdx.graphics.getDeltaTime();
    velocity.add(acceleration.x * deltaTime, acceleration.y * deltaTime);
    position.add(velocity.x * deltaTime, velocity.y * deltaTime);

    if (position.y <0){
        position.y = -position.y * BOUNCE_DAMPENING;
        velocity.y = -velocity.y * BOUNCE_DAMPENING;
    }
}
}


public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture background;
Texture ballTexture;
Ball ball;
Texture spikeTexture;



@Override
public void create () {
    batch = new SpriteBatch();
    background = new Texture("gamebackground.png");

    ballTexture = new Texture("ball2.png");
    ballTexture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

    ball = new Ball();

    spikeTexture = new Texture("spike2.png");

}

@Override
public void render () {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        if (Gdx.input.justTouched())
            ball.velocity.y += 350;
        ball.update();

    batch.begin();
        float scaleFactor = 2.0f;
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.draw(ballTexture, ball.position.x, ball.position.y, ballTexture.getWidth() * scaleFactor, ballTexture.getHeight() * scaleFactor);

        batch.draw(spikeTexture, 80,-260, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

        batch.end();


}

@Override
public void dispose () {

    batch.dispose();
    background.dispose();
    ballTexture.dispose();

}

}

\$\endgroup\$
0
\$\begingroup\$

For this you should make a Sprite (so you'll be able to have more than one spikes at the same time in a clean way) of your spikes like this

// Declaration
Sprite spikes;

In your create

// Make your sprite
spikes = new Sprite(spikesTexture);
spikes.setPosition(x, y);

In your render (you should have an update method called by render)

// Move your sprite
spikes.translateX(ball.velocity.x);

// If your spikes is off screen
if (spikes.getX() < 0 - spikes.getWidth()) // You sub the width of the spikes because the originX is in the left-bottom corner
    spikes.setPosition(Gdx.graphics.getWidth(),  spikes.getY()); // Set the spikes at the right edge of the screen

Your draw method will change just a bit

spikes.draw(batch);

Like this, you'll have a ball that stay at it's x position and the spikes moving from right to left. Once the spikes is totally off screen (by left), it will be move (by set, so you won't see it moving) to the right of your screen. Hope it helps

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer. I have tried it out but seem to get an error with the spikes.setPosition(Gdx.graphics.getWidth()); I think I have to set the position with integers, which works fine but then when I run the app the spike just appears really big and stretches up from the bottom of the screen to the top and doesn't move. Any ideas? \$\endgroup\$ – Richard Flack Feb 8 '17 at 16:38
  • \$\begingroup\$ I have sorted out that error but now the spike just won't move and is just at the position that I have set \$\endgroup\$ – Richard Flack Feb 8 '17 at 16:52
  • \$\begingroup\$ Oh, that's my bad i've forgot the y position in the setPosition. Hmm, this means your ball.velocity.x is 0. Check it (it's the spikes.translateX which should move your spikes) \$\endgroup\$ – Sebastien Servouze Feb 9 '17 at 8:09
  • \$\begingroup\$ Now the spike is disappearing when the screen is touched and then reappears in another position each time haha. Any thoughts? I'm kind of stuck now \$\endgroup\$ – Richard Flack Feb 9 '17 at 13:16
  • \$\begingroup\$ There is no reasons for that, is your ball.velocity.x constant ? \$\endgroup\$ – Sebastien Servouze Feb 9 '17 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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