If you think the Android project RollOverSphere---a-simple-libgdx-game listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.igorcrevar.rolloverchuck.objects;
//fromwww.java2s.comimport java.util.Random;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
publicclass StarsObject {
privateclass Star {
publicfloat speed;
public Sprite sprite;
public Star() {
sprite = new Sprite();
}
publicvoid setTexture(TextureRegion tr) {
sprite.setRegion(tr);
}
publicvoid init(float x, float y, float scale, float speed, float colorFactor) {
sprite.setBounds(x, y, 32 * scale, 32 * scale);
sprite.setColor(Color.WHITE.toFloatBits() * colorFactor);
this.speed = speed;
}
publicboolean update(float deltaTime) {
sprite.setY(sprite.getY() - speed * deltaTime);
return sprite.getY() >= 0;
}
}
privatestaticfinalint MAX_COUNT = 50;
private Star[] points = new Star[MAX_COUNT];
private Random rnd = new Random();
public StarsObject() {
for (int i = 0; i < MAX_COUNT; ++i) {
points[i] = new Star();
}
}
publicvoid init(TextureRegion tr) {
for (int i = 0; i < MAX_COUNT; ++i) {
points[i].setTexture(tr);
initStar(i, 1080);
}
}
publicvoid update(float deltaTime) {
for (int i = MAX_COUNT - 1; i >= 0; --i) {
Star p = points[i];
if (!p.update(deltaTime)) {
initStar(i, 0); // create new on ith place
}
}
}
publicvoid draw(SpriteBatch spriteBatch) {
for (int i = MAX_COUNT - 1; i >= 0; --i) {
Star p = points[i];
p.sprite.draw(spriteBatch);
}
}
privatevoid initStar(int i, int randomY) {
float yPos = 1080;
if (randomY > 0) {
yPos -= rnd.nextInt(randomY);
}
points[i].init(rnd.nextInt(1920), yPos, -rnd.nextFloat() + 1.0f, 75 + rnd.nextInt(150), rnd.nextFloat() * 0.3f + 0.7f);
}
}