Reputation: 303
public void chooseLane() {
lane = MathUtils.random(1, 3);
System.out.println(lane);
spawnCar();
}
public void spawnCar() {
if(lane == 1){
batch.begin();
batch.draw(carsb, carP1X, carP1Y);
batch.end();
}
if(lane == 2){
batch.begin();
batch.draw(carsb, carP2X, carP2Y);
batch.end();
}
if(lane == 3){
batch.begin();
batch.draw(carsb, carP3X, carP3Y);
batch.end();
}
Sprite appears for pretty much one frame then disappears. I'm guessing it has something to do with this all not being in the render()
method, but I'm not sure how to shift this into that method.
Upvotes: 0
Views: 650
Reputation: 19776
I assume that you call chooseLane()
from your create()
, show()
or resume()
method. That means that you will once spawn a car, choose a lane for that new car and then draw it once on the screen.
After that LibGDX will move to the endless loop and will keep call your render()
method continuously. Probably you followed some tutorial and got something like Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
in your render method. This will then clear your screen and remove your drawn car. After that it won't be drawn again.
You should probably change your code which looks like the following:
public void chooseLane() {
lane = MathUtils.random(1, 3);
System.out.println(lane);
}
public void drawCar() {
batch.begin();
if(lane == 1){
batch.draw(carsb, carP1X, carP1Y);
}
if(lane == 2){
batch.draw(carsb, carP2X, carP2Y);
}
if(lane == 3){
batch.draw(carsb, carP3X, carP3Y);
}
batch.end();
}
public void create()/show()/resume() { // choose whatever fits your case here
chooseLane();
}
public void render(float deltaTime) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawCar();
}
Upvotes: 2