I am making a game using libgdx I need to the space to go forever I have written code but it is not very effective take a look. Look at the code that I used to move the background but I want to use a scrolling texture. Where in the code can I do this should I put the code, and what is the exact code.
package com.me.mygdxgame;
import java.awt.Font;
import java.util.Random;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
public class MyGdxGame implements ApplicationListener {
SpriteBatch batch;
Texture mario;
Player player;
CharSequence hello = "hello";
Texture playerTexture;
Vector2 backgroundpos;
Vector2 backgroundpos2;
Vector2 position3;
Vector2 position2;
Vector2 position4;
Texture asteroid;
Texture space;
TextureRegion[] frames;
Texture background;
Texture backgroundMove2;
Texture backgroundMove;
float stateTime;
Vector2 position;
Vector2 pos;
Vector2 ha;
Sprite asteroidSprite;
//OrthographicCamera camera ;
Animation animation;
@Override
public void create() {
batch = new SpriteBatch(); mario = new Texture("images-2.jpg"); position = new Vector2(10,0); //camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
pos = new Vector2(50,50);
position2 = new Vector2(50,50);
position3 = new Vector2(50,50);
position4 = new Vector2(50,50);
background = new Texture("spaceforgame.jpg");
backgroundMove = new Texture("spaceforgame.jpg");
backgroundpos = new Vector2();
backgroundpos2 = new Vector2(0, Gdx.graphics.getHeight());
backgroundMove2 = new Texture("spaceforgame.jpg");
space = new Texture("0.jpg");
ha = new Vector2(0,0);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//backgroundpos.y = backgroundpos.y -10;
//backgroundpos2.y = backgroundpos2.y - 10;
//if(backgroundpos.y < - Gdx.graphics.getHeight()){
// backgroundpos.y = Gdx.graphics.getHeight();
//}
//if(backgroundpos2.y < - Gdx.graphics.getHeight()){
//backgroundpos2.y = Gdx.graphics.getHeight() + 20;
//}
//System.out.println(backgroundpos.y);
//camera.position.set(position.x + 134/2, position.y + 268/2, 0);
//batch.setProjectionMatrix(camera.combined);
//camera.update();
// position.y = position.y + 5;
pos.y = pos.y - 10;
if(pos.y < 0){ Random rand = new Random();
pos.x = rand.nextInt(Gdx.graphics.getWidth()) + 2;
pos.y = Gdx.graphics.getHeight();
}
position2.y = position2.y - 10;
if(pos.y < 0){ Random rand = new Random();
pos.x = rand.nextInt(Gdx.graphics.getWidth()) + 2;
pos.y = Gdx.graphics.getHeight();
}
float accelX = Gdx.input.getAccelerometerX();
float accelY = Gdx.input.getAccelerometerY();
//up w
// if (accelX < -1 ){
// position.y -= accelX;
// }
//left a
if (accelY < -1){
position.x += accelY;
}
// down s
// if (accelX > +1){
// position.y -= accelX;
// }
//right d
if (accelY > +5 ){
position.x += accelY;
}
if(Gdx.input.isKeyPressed(Keys.W)){
position.x = position.x + 5;
}
batch.begin();
BitmapFont font = new BitmapFont() ;
font.setColor(0.0f, 1.0f, 1.0f,1.0f);
// batch.draw(space, 0,10,2000,1000 ); batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.draw(backgroundMove, backgroundpos.x, backgroundpos.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.draw(backgroundMove2, backgroundpos2.x, backgroundpos2.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.draw(asteroid, pos.x, pos.y, 100, 100);
batch.draw(mario, position.x, position.y, 134, 268);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}