Android 安卓libgdx与演员合拍

Android 安卓libgdx与演员合拍,android,libgdx,actor,Android,Libgdx,Actor,我不能和我的演员一起拍。当我按下射击按钮时,我的演员面前有一颗子弹,但它不动。我不知道如何让它像陨石一样移动 public class Game implements ApplicationListener { Texture meteoriteImage; Texture shipImage; Texture bulletImage; SpriteBatch batch; OrthographicCamera camera; Rectangle ship;

我不能和我的演员一起拍。当我按下射击按钮时,我的演员面前有一颗子弹,但它不动。我不知道如何让它像陨石一样移动

public class Game implements ApplicationListener {
   Texture meteoriteImage;
   Texture shipImage;
   Texture bulletImage;
   SpriteBatch batch;
   OrthographicCamera camera;
   Rectangle ship;
   Rectangle bullet;
   Array<Rectangle> meteorites;
   long lastMeteoriteTime;


   public void create() {
      meteoriteImage = new Texture(Gdx.files.internal("meteorite.png"));
      shipImage = new Texture(Gdx.files.internal("ship.png"));
      bulletImage = new Texture(Gdx.files.internal("bullet.png"));

      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();

      ship = new Rectangle();
      ship.x = 800 - 48;
      ship.y = 480 / 2 - 48 / 2;
      ship.width = 48;
      ship.height = 48;

      meteorites = new Array<Rectangle>();
      spawnMeteorite();
   }

   private void spawnMeteorite() {
      Rectangle meteorite = new Rectangle();
      meteorite.y = MathUtils.random(0, 480-48);
      meteorite.x = 0;
      meteorite.width = 48;
      meteorite.height = 48;
      meteorites.add(meteorite);
      lastMeteoriteTime = TimeUtils.nanoTime();
   }


   public void render() {
      Gdx.gl.glClearColor(0, 0, 0.2f, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      camera.update();
      batch.setProjectionMatrix(camera.combined);

      batch.begin();
      batch.draw(shipImage, ship.x, ship.y);
      for(Rectangle meteorite: meteorites) {
         batch.draw(meteoriteImage, meteorite.x, meteorite.y);
      }
      batch.end();

      if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) ship.y += 200 * Gdx.graphics.getDeltaTime();
      if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) ship.y -= 200 * Gdx.graphics.getDeltaTime();
      if(Gdx.input.isKeyPressed(Keys.BUTTON_Y)){
              bullet = new Rectangle();
              bullet.x = ship.x - 48;
              bullet.y = ship.y;
              bullet.width = 48;
              bullet.height = 48;
              batch.begin();
              batch.draw(bulletImage, bullet.x, bullet.y);
              batch.end();

          while(bullet.x > 0){
              bullet.x -= 200 * Gdx.graphics.getDeltaTime();
          }
      }

      if(ship.x < 0) ship.x = 0;
      if(ship.x > 800 - 48) ship.x = 800 - 48;
      if(ship.y < 0) ship.y = 0;
      if(ship.y > 480 - 48) ship.y = 480 - 48;

      if(TimeUtils.nanoTime() - lastMeteoriteTime > 1000000000) spawnMeteorite();

      Iterator<Rectangle> iter = meteorites.iterator();
      while(iter.hasNext()) {
         Rectangle meteorite = iter.next();
         meteorite.x += 200 * Gdx.graphics.getDeltaTime();
         if(meteorite.x + 48 < 0) iter.remove();
         if(meteorite.overlaps(ship)) {

            iter.remove();
         }
      }
   }


   public void dispose() {
      shipImage.dispose();
      meteoriteImage.dispose();
      bulletImage.dispose();
      batch.dispose();
   }


   public void resize(int width, int height) {
   }


   public void pause() {
   }


   public void resume() {
   }
}
公共类游戏实现ApplicationListener{
纹理陨石图像;
纹理图像;
纹理图像;
喷雾批;
正交摄影机;
矩形船;
矩形子弹;
阵列陨石;
时间长;
公共void create(){
meteoriteImage=新纹理(Gdx.files.internal(“meteorite.png”);
shipImage=新纹理(Gdx.files.internal(“ship.png”);
bulletImage=新纹理(Gdx.files.internal(“bullet.png”);
摄影机=新的正交摄影机();
摄像头。设置为ToOrtho(假,800480);
批次=新的SpriteBatch();
ship=新矩形();
船舶x=800-48;
船舶y=480/2-48/2;
船宽=48;
船舶高度=48;
陨石=新阵列();
陨石();
}
私人陨石(){
矩形陨石=新矩形();
陨石y=MathUtils.random(0480-48);
陨石x=0;
陨石。宽度=48;
陨石高度=48;
陨石。添加(陨石);
lastMeteoriteTime=TimeUtils.nanoTime();
}
公共无效呈现(){
glClearColor(0,0,0.2f,1);
Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
批处理绘图(shipImage、ship.x、ship.y);
用于(矩形陨石:陨石){
绘制(陨石图像,陨石.x,陨石.y);
}
batch.end();
如果(Gdx.input.isKeyPressed(Keys.DPAD_UP))ship.y+=200*Gdx.graphics.getDeltaTime();
如果(Gdx.input.isKeyPressed(Keys.DPAD_DOWN))ship.y-=200*Gdx.graphics.getDeltaTime();
如果(Gdx.input.isKeyPressed(按键按钮Y)){
bullet=新矩形();
bullet.x=ship.x-48;
bullet.y=ship.y;
bullet.width=48;
bullet.height=48;
batch.begin();
批量绘制(bulletImage、bullet.x、bullet.y);
batch.end();
而(bullet.x>0){
bullet.x-=200*Gdx.graphics.getDeltaTime();
}
}
如果(ship.x<0)ship.x=0;
如果(ship.x>800-48)ship.x=800-48;
如果(ship.y<0)ship.y=0;
如果(ship.y>480-48)ship.y=480-48;
如果(TimeUtils.nanoTime()-lastMeteoriteTime>100000000)生成陨石();
迭代器iter=陨石。迭代器();
while(iter.hasNext()){
矩形陨石=iter.next();
meteorite.x+=200*Gdx.graphics.getDeltaTime();
如果(陨石x+48<0)iter.remove();
if(陨石重叠(船)){
iter.remove();
}
}
}
公共空间处置(){
dispose();
meteoriteImage.dispose();
bulletImage.dispose();
batch.dispose();
}
公共空心调整大小(整型宽度、整型高度){
}
公共空间暂停(){
}
公众简历(){
}
}

我使用了本教程:

这不是你想要的

while(bullet.x > 0){
    bullet.x -= 200 * Gdx.graphics.getDeltaTime();
}

你真的想让子弹的位置设置动画,对吗?因此,您需要将项目符号作为对象添加到世界中,然后在每次调用“渲染”时更改其x位置。否则你根本看不到它的动画效果…

好吧,这不是你想要的

while(bullet.x > 0){
    bullet.x -= 200 * Gdx.graphics.getDeltaTime();
}

你真的想让子弹的位置设置动画,对吗?因此,您需要将项目符号作为对象添加到世界中,然后在每次调用“渲染”时更改其x位置。否则你根本看不到它的动画…

似乎你错过了在子弹圈中检查哪颗陨石,试试这个:

while(iter2.hasNext()) {
  Rectangle bullet = iter2.next();
  bullet.x -= 200 * Gdx.graphics.getDeltaTime();
  if(bullet.x + 48 < 0) iter2.remove();
  iter = meteorites.iterator();
  while(iter.hasNext()) {
    if(bullet.overlaps(iter.next())) {
      iter2.remove();
      iter.remove();
    }
  }
}
while(iter2.hasNext()){
矩形项目符号=iter2.next();
bullet.x-=200*Gdx.graphics.getDeltaTime();
如果(bullet.x+48<0)iter2.移除();
iter=陨石。迭代器();
while(iter.hasNext()){
if(bullet.overlaps(iter.next())){
iter2.remove();
iter.remove();
}
}
}

请记住,不要简单地将原来的meteorite循环放入bullet循环,因为两个循环中都有翻译代码

似乎你错过了在子弹圈中检查哪颗陨石,试试这个:

while(iter2.hasNext()) {
  Rectangle bullet = iter2.next();
  bullet.x -= 200 * Gdx.graphics.getDeltaTime();
  if(bullet.x + 48 < 0) iter2.remove();
  iter = meteorites.iterator();
  while(iter.hasNext()) {
    if(bullet.overlaps(iter.next())) {
      iter2.remove();
      iter.remove();
    }
  }
}
while(iter2.hasNext()){
矩形项目符号=iter2.next();
bullet.x-=200*Gdx.graphics.getDeltaTime();
如果(bullet.x+48<0)iter2.移除();
iter=陨石。迭代器();
while(iter.hasNext()){
if(bullet.overlaps(iter.next())){
iter2.remove();
iter.remove();
}
}
}

请记住,不要简单地将原来的meteorite循环放入bullet循环,因为两个循环中都有翻译代码

只在按下按钮时更新bullet.x。只在按下按钮时更新bullet.x。