使用LibGDX生成随机级别

时间:2014-10-27 16:44:48

标签: java android libgdx

我为级别生成了一个类,并且到目前为止已经用它了:

public class LevelGenerator {

private Sprite environment;
private float leftEdge, rightEdge, minGap, maxGap, y;

public Enemy enemy;

public LevelGenerator(Sprite environment, float leftEdge, float rightEdge,
        float minGap, float maxGap) {
    this.environment = environment;
    this.leftEdge = leftEdge;
    this.rightEdge = rightEdge;
    this.minGap = minGap;
    this.maxGap = maxGap;
}

public void generate(float topEdge){
    if(y + MathUtils.random(minGap, maxGap) < topEdge)
        return;

    y = topEdge;

    float x = MathUtils.random(leftEdge, rightEdge);

}

基本上,我想要发生的是敌人块在屏幕两侧随机生成。这是敌人的街区类(非常简单):

public class Enemy extends Sprite{

public Enemy(Sprite sprite) {
    super(sprite);
}

@Override
public void draw(Batch spriteBatch){
    super.draw(spriteBatch);

    }

}

这就是当游戏块在游戏画面上以静态位置绘制时,游戏的样子:http://i.imgur.com/SIt18Qn.png。我想要实现的是这些“敌人”块在屏幕的两侧随机生成,但我似乎无法用我迄今为止的代码找到一种方法。

谢谢!

1 个答案:

答案 0 :(得分:0)

我无法测试,但我认为它会没问题,你有一个矩形,如果你想看看它是否与另一个演员碰撞,如果这样更新它在更新和绘制方法中的位置,ramdon方法开始自定义看如果坐标,哪个colicionan被分配给另一个演员rectagulo敌人或再见。

public class overFlowEnemy extends Sprite {

 private final float maxH = Gdx.graphics.getHeight();
 private final float maxW = Gdx.graphics.getWidth();

 private Rectangle rectangle;

 private Random random = new Random();

 private float inttt = 0;
 private float randomN = 0;

 private boolean hasCollided = false; 

public overFlowEnemy(Sprite sprite) {
    super(sprite);

    crearEnemigo();

    rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
 }

@Override
public void draw(Batch spriteBatch) {
    super.draw(spriteBatch);


}

private void crearEnemigo(){

    setX(RandomNumber((int)maxW));
    setY(RandomNumber((int)maxH));
}

private int RandomNumber(int pos) {

    random.setSeed(System.nanoTime() * (long) inttt);
    this.randomN = random.nextInt(pos);
    inttt += randomN;

    return (int)randomN;

}

public Rectangle getColliderActor(){
    return this.rectangle;
}

}

这个类应该创造一个随机的敌人。

编辑:重读你的问题,是我的英语不是很好,而且我认为你只想在屏幕两侧画画,告诉我或改编课程,因为当你创造思想时,这是在屏幕上。

我刚刚添加了另一个课程,如果可以,并且想要告诉我哪个是正确的,并删除另一个。

public class overFlow extends Sprite {

 private final float maxH = Gdx.graphics.getHeight();
 private final float maxW = Gdx.graphics.getWidth();

 private Rectangle rectangle;

 private Random random = new Random();

 private float inttt = 0;
 private float randomN = 0;

 private boolean hasCollided = false; 

public overFlow(Sprite sprite) {
    super(sprite);

    crearEnemigo();

    rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
 }

@Override
public void draw(Batch spriteBatch) {
    super.draw(spriteBatch);


}

private void crearEnemigo(){

    setX(RandomNumber((int)maxW, true));
    setY(RandomNumber((int)maxH, false));
}

private int RandomNumber(int pos, boolean w) {

    random.setSeed(System.nanoTime() * (long) inttt);
    if (w = true){

        this.randomN = random.nextInt((pos));

            if(randomN % 2 == 0){
                randomN = (pos - getWidth()); 
            }else{
                randomN = 0; //left screen
            }
    }else{

        this.randomN = random.nextInt(pos - (int)getHeight());
    }
        inttt += randomN;

    return (int)randomN;

}

public Rectangle getColliderActor(){
    return this.rectangle;
}

}