package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Background {
class Star {
Vector2 position;
float speed;
public Star() {
position = new Vector2((float)Math.random()*1280, (float)Math.random()*720);
speed = 4.0f;
}
public void update() {
position.x -= speed;
if(position.x < -20) {
position.x = 1280;
position.y = (float)Math.random()*720;
}
}
}
Texture texture;
Texture textureStar;
Star[] stars;
public Background() {
texture = new Texture("bg.png");
textureStar = new Texture("star12.tga");
stars = new Star[40];
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star();
}
}
public void render(SpriteBatch batch) {
batch.draw(texture, 0, 0);
for (int i = 0; i < stars.length; i++) {
batch.draw(textureStar, stars[i].position.x, stars[i].position.y);
}
}
public void update() {
for (int i = 0; i < stars.length; i++) {
stars[i].update();
}
}
public void dispose() {
texture.dispose();
}
}
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Star[] stars = new Star[4];
stars[0] = new Star(0, 0);
stars[1] = new Star(1, 0);
stars[2] = new Star(0, 1);
stars[3] = new Star(1, 1);
System.out.println(Arrays.toString(stars));
}
static class Star {
int x;
int y;
public Star(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Star{" +
"x=" + x +
", y=" + y +
'}';
}
}
}
[Star{x=0, y=0}, Star{x=1, y=0}, Star{x=0, y=1}, Star{x=1, y=1}]