I want to add a booster. For example, when the score is 50, I draw a Texture. When the user clicks on this Texture, I want the enemies on the screen to disappear and return to the original speed. I have completed the drawing process. How can I get this new texture to destroy all enemies on the screen when clicked? Should I do it under public boolean touchDown?
public class Game extends ApplicationAdapter implements InputProcessor { SpriteBatch batch; Texture background; Texture health; Texture virus; Texture bigvirus; Texture medicine; Texture radiation; Texture booster; //new things to try private Vector2 vector2; Sprite boosterSprite; BitmapFont font; FreeTypeFontGenerator fontGen; Random random = new Random(); Array<Items> itemsArray = new Array<Items>(); int lives = 0; int score = 0; float genCounter = 0; private final float startGenSpeed = 1.1f; //başlatma hızı float genSpeed = startGenSpeed; private double currentTime; private double gameOverTime = -1.0f; @Override public void create () { batch = new SpriteBatch(); background = new Texture("backgroundlungs.png"); health = new Texture("health.png"); virus = new Texture("virus.png"); bigvirus = new Texture("bigvirus.png"); medicine = new Texture("medicine.png"); radiation = new Texture("radiation.png"); booster = new Texture("booster.png"); vector2=new Vector2(); boosterSprite=new Sprite(booster); boosterSprite.setPosition((Gdx.graphics.getWidth()/10)*9,(Gdx.graphics.getHeight()/ 10)*8); Items.radius = Math.max(Gdx.graphics.getHeight(), Gdx.graphics.getWidth()) / 13f; //resim boyutları buradan ayarlanıyor Gdx.input.setInputProcessor(this); fontGen = new FreeTypeFontGenerator(Gdx.files.internal("robotobold.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter(); params.color = Color.WHITE; params.size = 50; params.characters = "0123456789 ScreCutoplay:.+-"; font = fontGen.generateFont(params); } @Override public void render () { batch.begin(); batch.draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); double newTime = TimeUtils.millis() / 1000.0; System.out.println("newTime: " + newTime); double frameTime = Math.min(newTime - currentTime,0.3); System.out.println("frameTime: " + frameTime); float deltaTime = (float) frameTime; System.out.println("deltaTime: " + deltaTime); currentTime = newTime; if (lives <= 0 && gameOverTime == 0f) { //gameo gameOverTime = currentTime; } if (lives > 0 ) { //game mode genSpeed -= deltaTime * 0.0075f; //item yoğunluğunu buradan ayarliyoruz. System.out.println("genspeed: " + genSpeed); System.out.println("gencounter: " + genCounter); if (genCounter <= 0f) { genCounter = genSpeed; addItem(); } else { genCounter -= deltaTime; } for (int i = 0; i< lives; i++) { batch.draw(health,i*30f + 20f,Gdx.graphics.getHeight()-25f,25f,25f); } for (Items items : itemsArray) { items.update(deltaTime); switch (items.type) { //dikdörtgen sıkıntı yapabilir. case REGULAR: batch.draw(virus,items.getPos().x, items.getPos().y, Items.radius,Items.radius); break; case EXTRA: batch.draw(bigvirus,items.getPos().x, items.getPos().y, Items.radius,Items.radius); break; case ENEMY: batch.draw(radiation,items.getPos().x, items.getPos().y, Items.radius,Items.radius); break; case LIFE: batch.draw(medicine,items.getPos().x, items.getPos().y, Items.radius,Items.radius); break; } if ( lives > 0 && score >= 10 ) { boosterSprite.draw(batch); } boolean holdives = false; Array<Items> toRemove = new Array<Items>(); for (Items items: itemsArray) { if (items.outOfScreen()) { toRemove.add(items); if (items.living && items.type == REGULAR) { lives--; holdives = true; break; } } } if (holdives) { for (Items f : itemsArray) { f.living = false; } } for (Items f : toRemove) { itemsArray.removeValue(f,true); } } font.draw(batch,"Score: " + score, 40,50); if (lives <= 0) { font.draw(batch,"Cut to play",Gdx.graphics.getWidth()*0.43f,Gdx.graphics.getHeight()*0.5f); } batch.end(); } private void addItem() { float pos = random.nextFloat() * Math.max(Gdx.graphics.getHeight(), Gdx.graphics.getWidth()); Items item = new Items(new Vector2(pos,-Items.radius),new Vector2((Gdx.graphics.getWidth() * 0.5f - pos) * 0.3f + (random.nextFloat() - 0.5f),Gdx.graphics.getHeight()*0.5f)); float type = random.nextFloat(); if (type > 0.98) { item.type = Items.Type.LIFE; }else if (type > 0.88) { item.type = Items.Type.EXTRA; }else if (type > 0.809) { item.type = Items.Type.ENEMY; } itemsArray.add(item); } @Override public void dispose () { batch.dispose(); font.dispose(); fontGen.dispose(); } @Override public boolean keyDown(int keycode) { return false; } @Override public boolean keyUp(int keycode) { return false; } @Override public boolean keyTyped(char character) { return false; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { if (lives <= 0 && currentTime - gameOverTime > 2f ) { //menu mode gameOverTime = 0f; score = 0; lives = 4; genSpeed = startGenSpeed; itemsArray.clear(); }else { //game mode Array<Items> toRemove = new Array<Items>(); Vector2 pos = new Vector2(screenX,Gdx.graphics.getHeight() - screenY); int plusScore =0; for (Items f: itemsArray) { System.out.println("distance: " + pos.dst2(f.pos)); System.out.println("distance: " + f.clicked(pos)); System.out.println("distance: "+ Items.radius * Items.radius + 1); if (f.clicked(pos)) { toRemove.add(f); switch (f.type) { case REGULAR: plusScore++; break; case EXTRA: plusScore+=2; score++; break; case ENEMY: lives--; break; case LIFE: lives++; break; } } } score += plusScore * plusScore; for (Items f : toRemove) { itemsArray.removeValue(f,true); } } return false; } @Override public boolean mouseMoved(int screenX, int screenY) { return false; } @Override public boolean scrolled(float amountX, float amountY) { return false; }
}
public class Items {
public static float radius = 60f; Vector2 vector2; public static int boostX = Gdx.graphics.getWidth() / 10; public static int boostY= (Gdx.graphics.getHeight()/ 10)*9; public enum Type { REGULAR, EXTRA, ENEMY, LIFE, BOOSTER } Type type; Vector2 pos,velocity; public boolean living =true; Items (Vector2 pos, Vector2 velocity) { this.pos = pos; this.velocity = velocity; type = Type.REGULAR; } public boolean clicked(Vector2 click) { if (pos.dst2(click) <= radius * radius + 1) { return true; } return false; } public final Vector2 getPos() { return pos; } public boolean outOfScreen() { return (pos.y < -2f * radius); } public void update(float dt) { velocity.y -= dt * (Gdx.graphics.getHeight() * 0.2f); velocity.x -= dt * Math.signum(velocity.x) * 4f; pos.mulAdd(velocity,dt); }
}