Проблема в том, что на GameScreen не появляются астероиды. Кажется, что они даже не создают. Я создал World class, который управляет астероидами (их порождение и уничтожение) и передал его GameScreen. Когда я вызываю метод world.spawnAsteroids прямо в методе GameScreen updateRunning (), они появляются на экране, но затем игра вылетает после очень быстрого появления 3-5 астероидов. Я понятия не имею, в чем проблема.
(...)
//world which manage asteroids
public class World {
//maximum y coordination for asteroids
static final int deathY = 890;
static final float TICK_INITIAL = 0.5f;
static final int screenWidth = 720;
float tickTime = 0;
float tick = TICK_INITIAL;
static final float TICK_DECREMENT = 0.05f;
public boolean gameOver = false;
public int actualScore = 0;
Random random = new Random();
List<Asteroid> asteroids;
//if asteroid go deathY
public boolean death = false;
public World(){
asteroids = new ArrayList<Asteroid>();
}
public void update(float deltaTime) {
if (gameOver)
return;
tickTime += deltaTime;
while (death) {
tickTime -= tick;
//spawning new asteroid
spawnAsteroid();
//all already created asteroids
for(Asteroid as : asteroids){
as.move();
//if any asteroid reaches max y
if(as.actualY >= deathY - as.asteroidIMG.getHeight()){
gameOver = true;
return;
}
}
}
}
//spawning asteroids method
public void spawnAsteroid(){
Random random = new Random();
//always on top of screen
int y = 0;
//random start x
int x = random.nextInt(720);
//random size of asteroid
int asteroidSize = random.nextInt(2);
//random color
int color = random.nextInt(5);
//create new asteroid and add to list of all asteroids
Asteroid as = new Asteroid(asteroidSize, color, x, y);
asteroids.add(as);
//move asteroid along y
as.move();
}
//delete asteroid method (asteroid is deleting after tapping on it)
private void destroyAsteroid(Asteroid asteroid){
asteroid.asteroidIMG.dispose();
asteroids.remove(asteroid);
}
}
/// класс игрового экрана
public class GameScreen extends Screen {
//game states
enum GameState {
Ready,
Running,
Paused,
GameOver
}
AndroidFastRenderView gameView;
static final int deathY = 890;
GameState state = GameState.Ready;
//world instance
World world;
int scoreInt = 0;
String score = "0";
//list of asteroids
List<Asteroid> asteroids = new ArrayList<Asteroid>();
public GameScreen(Game game){
super(game);
world = new World();
asteroids = world.asteroids;
}
(...)
//metoda update w stanie running
private void updateRunning(List <TouchEvent> touchEvents, float deltaTime){
//ile jest eventow dotyku
int len = touchEvents.size();
for(int i = 0; i < len; i++){
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
if(event.x > 589 && event.x < 689 && event.y > 1172) {
if(Settings.soundEnabled)
Assets.click.play(1);
state = GameState.Paused;
return;
}
}
if(event.type == TouchEvent.TOUCH_DOWN){
if(asteroids.size() > 0){
for(Asteroid as : asteroids){
//if asteroid gots clicked
if(event.x > as.actualX && event.y > as.actualY
&& event.x < (as.actualX +
as.asteroidIMG.getWidth())
&& event.y < (as.actualY +
as.asteroidIMG.getHeight())){
Assets.destroyAsteroid.play(1);
scoreInt += as.score;
}
}
}
}
}
world.update(deltaTime);
if(world.gameOver) {
if(Settings.soundEnabled)
Assets.gameOver.play(1);
state = GameState.GameOver;
}
//score update
if(scoreInt != world.actualScore) {
scoreInt = world.actualScore;
score = "" + scoreInt;
}
}
//method of drawing asteroids to screen
private void drawRunning(){
(...)
if(asteroids.size() > 0){
for(Asteroid as : asteroids){
//drawing asteroid
//(draw Pixmap is my own method inside framework)
g.drawPixmap(as.asteroidIMG, as.actualX, as.actualY);
}
}
//drawing text score
g.drawText(328, 1229, score);
}
}