gpt4 book ai didi

java - LibGDX:似乎无法绘制数组中的对象

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:01 24 4
gpt4 key购买 nike

敌方等级

public class Enemy extends Sprite{

private Vector2 velocity = new Vector2(0,0);
private float speed = 30, gravity = 30 * 1.8f;


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

public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}

public void update(float delta) {

velocity.y -= gravity * delta;
setY(velocity.y + speed * delta);
}

}

PlayScreen 类

public class PlayScreen implements Screen {
private Player player;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private TiledMap map;
private Rectangle rightRectangle, leftRectangle, playerRectangle;

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());

boolean wallLeft = leftRectangle.overlaps(player.rectangle);
boolean wallRight = rightRectangle.overlaps(player.rectangle);

if(wallLeft){
System.out.println("wallLeft Overlap");
player.velocity.x = 0;
}
else if(wallRight){
System.out.println("wallRight Overlap");
player.velocity.x = 0;
}

enemies = new ArrayList<Enemy>();
enemy = new Enemy(new Sprite(new Texture("img/player.png"))));

enemies.add(new Enemy(new Sprite(new Texture("img/player.png"))));
enemies.add(new Enemy(new Sprite(new Texture("img/player.png"))));


for(it = enemies.iterator(); it.hasNext();){

enemy.draw(renderer.getSpriteBatch());
enemy.setOrigin(500, 500);

}


renderer.getSpriteBatch().end();

}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void show() {
camera = new OrthographicCamera();
map = new TiledMap();
renderer = new OrthogonalTiledMapRenderer(map);

player = new Player(new Sprite(new Texture("img/player.png")));
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);

player.setPosition(
Gdx.graphics.getWidth()/2f - player.getWidth()/2f,
Gdx.graphics.getHeight()/2f - player.getHeight()/2f
- Gdx.graphics.getHeight()/5f);
}
}

因此,如果我运行它,应用程序就会卡住并且不再响应。我相信问题是当我尝试将“敌人”添加到 PlayScreen 类中时。当我没有将“Enemy”类放入项目中时,它之前运行良好。我想做的是,将每个敌人绘制在敌人阵列中,然后设置他们的位置。

最佳答案

是的,您在这里缺少一个 it.next() 来推进循环。

for(it = enemies.iterator(); it.hasNext();){

enemy.draw(renderer.getSpriteBatch());
enemy.setOrigin(500, 500);

}

我认为您也不希望每次都绘制相同的enemy,因此您应该在此循环开始时设置enemy = it.next()

另外,我不是 %100 确定它的后端是如何工作的,但我很确定你不想在下面的代码片段中加载同一个 Sprite 的 3 个副本。您应该加载一次 Sprite ,并为每个敌人使用相同的 Sprite

enemy = new Enemy(new Sprite(new Texture("img/player.png"))));
enemies.add(new Enemy(new Sprite(new Texture("img/player.png"))));
enemies.add(new Enemy(new Sprite(new Texture("img/player.png"))));

关于java - LibGDX:似乎无法绘制数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com