1
\$\begingroup\$

Hello I want to spawn enemies at certain location but at random time,like for example every 10 seconds. Atm I am spawning enemies whenever I click to spawn heroes but with delay, but I sure does not work good, you are unable to get to the enemies base. Here is my code:

PlayStage.java

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.nitheism.uveggfruit.ActorScripts.FirstLevelScript;
import com.nitheism.uveggfruit.Players.FruitPlayer;
import com.nitheism.uveggfruit.Players.VeggiePlayer;
import com.nitheism.uveggfruit.UVeggFruit;
import com.uwsoft.editor.renderer.SceneLoader;
import com.uwsoft.editor.renderer.data.CompositeItemVO;
import com.uwsoft.editor.renderer.scene2d.CompositeActor;


public class PlayStage implements Screen {

private OrthographicCamera camera;
private Stage stage;
private BitmapFont bitmapFont;
private FruitPlayer fruitPlayer;
private VeggiePlayer veggiePlayer;
private UVeggFruit uvf;
private float densityT = 30 * Gdx.graphics.getDensity();


public PlayStage(UVeggFruit uvf) {
    this.uvf = uvf;

}


public void draw() {
    MenuStage mStage;
    if (veggiePlayer.getHealth() <= 0) {
        stage.getBatch().begin();
        bitmapFont.draw(stage.getBatch(), "DEFEAT", 640, 360);
        stage.getBatch().end();
        mStage = new MenuStage(uvf);
        this.hide();
        uvf.setScreen(mStage);
    }
    if (fruitPlayer.getHealth() <= 0) {
        stage.getBatch().begin();
        bitmapFont.draw(stage.getBatch(), "VICTORY", 640, 360);
        stage.getBatch().end();
        mStage = new MenuStage(uvf);
        uvf.setScreen(mStage);
    }
    stage.getBatch().begin();
    bitmapFont.draw(stage.getBatch(), Integer.toString(fruitPlayer.getHealth()), 1200, 390);
    bitmapFont.draw(stage.getBatch(), Integer.toString(veggiePlayer.getHealth()), 50, 390);
    bitmapFont.draw(stage.getBatch(), Integer.toString(veggiePlayer.getMoney()), 60, 420);
    stage.getBatch().end();


}


@Override
public void show() {
    camera = new OrthographicCamera();
    Viewport vp = new StretchViewport(1280, 720, camera);
    stage = new Stage(vp);
    Gdx.input.setInputProcessor(stage);
    fruitPlayer = new FruitPlayer();
    veggiePlayer = new VeggiePlayer();

    SceneLoader stageLoader = new SceneLoader();

    FreeTypeFontGenerator FTFG = new FreeTypeFontGenerator(Gdx.files.internal("Karmakooma.ttf"));
    FreeTypeFontGenerator.FreeTypeFontParameter FTFP = new FreeTypeFontGenerator.FreeTypeFontParameter();
    FTFP.color = Color.WHITE;
    FTFP.size = Math.round(densityT);
    bitmapFont = FTFG.generateFont(FTFP);
    FTFG.dispose();
    CompositeItemVO sceneComposites = new CompositeItemVO(stageLoader.loadScene("MainScene").composite);
    CompositeActor UI = new CompositeActor(sceneComposites, stageLoader.getRm());
    FirstLevelScript firstLevelScript = new FirstLevelScript(stageLoader, bitmapFont, stage, veggiePlayer, fruitPlayer);
    UI.addScript(firstLevelScript);


    stage.addActor(UI);
    draw();

}

@Override
public void render(float delta) {
    camera.update();
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();
    draw();


}

@Override
public void resize(int width, int height) {
    stage.getViewport().update(width, height, true);

}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void hide() {
}

@Override
public void dispose() {

    bitmapFont.dispose();


       }
  }

FirstLevelScript.java

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Timer;
import com.nitheism.uveggfruit.Players.FruitPlayer;
import com.nitheism.uveggfruit.Players.VeggiePlayer;
import com.uwsoft.editor.renderer.SceneLoader;
import com.uwsoft.editor.renderer.scene2d.ButtonClickListener;
import com.uwsoft.editor.renderer.scene2d.CompositeActor;
import com.uwsoft.editor.renderer.scripts.IActorScript;

import java.util.ArrayList;


public class FirstLevelScript implements IActorScript {

private SceneLoader stageLoader;
private BitmapFont bitmapFont;
private Stage stage;
private CompositeActor pear;
private PearScript pScript;
private VeggiePlayer veggiePlayer;
private FruitPlayer fruitPlayer;
private TomatoScript tScript;
private CompositeActor tomato;
private ArrayList<FruitScript> fruits;

    public FirstLevelScript(SceneLoader stageLoader,  BitmapFont    bitmapFont, Stage stage, VeggiePlayer vp, FruitPlayer fp) {
             this.stageLoader = stageLoader;
             this.bitmapFont = bitmapFont;
             this.stage = stage;
             this.veggiePlayer = vp;
             this.fruitPlayer = fp;
}

@Override
public void init(CompositeActor entity) {
    fruits = new ArrayList<FruitScript>();
    ButtonClickListener buttonClickListener = new ButtonClickListener();
    entity.getItem("tomatobtn").addListener(buttonClickListener);
    entity.getItem("tomatobtn").addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            if (veggiePlayer.getMoney() >= 10) {
                tScript = new TomatoScript(stage.getBatch(), bitmapFont, fruits, fruitPlayer);
                tomato = new CompositeActor(stageLoader.loadVoFromLibrary("tomato"), stageLoader.getRm());
                tomato.addScript(tScript);
                stage.addActor(tomato);
                veggiePlayer.setMoney(-10);
                Timer.schedule(new Timer.Task() {
                    @Override
                    public void run() {
                        pear = new CompositeActor(stageLoader.loadVoFromLibrary("pear"), stageLoader.getRm());
                        pScript = new PearScript(stage.getBatch(), bitmapFont, veggiePlayer);
                        pear.addScript(pScript);
                        stage.addActor(pear);
                        fruits.add(pScript);
                    }
                }, 2);

            }

        }
    });
}

@Override
public void act(float delta) {


}

@Override
public void dispose() {

     }
}

I will be glad if someone can help me on how to do it the way I want it, with delay I guess doesn't work I tried. So what I want is to spawn enemies every 10 seconds :) Tnx in advance.

\$\endgroup\$
1
\$\begingroup\$

You can use the delta variable of the render() function. This variable is a float value of the time elapsed since last render call (Something like time from frame to frame, not exactly, but this is the idea).

Simply add the delta time value to a variable and then execute the code you want when the variable gets to a specific (or random number).

Generic example:

@Override
public void render(float delta) {
    camera.update();
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);

    if(timeAux>=10){ //10 seconds
        //Your code to spawn enemy.
        timeAux=0;
    }else{
        timeAux+=delta;
    }
}

timeAux is a global variable.

10 is just an example value, you can create a variable and assign a new random value every time you create an enemy.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.