java - 无法在 libgdx 中隐藏碰撞图像?

标签 java android libgdx

我刚刚开始使用 LibGDX,但不知道如何在图像与对象碰撞时隐藏图像。在我的游戏中,一些点来自屏幕顶部并与底部的点相遇。当他们相遇时,这些点应该隐藏没有发生的事情。

这是主要的游戏类

public class GameScreen implements Screen,InputProcessor {
   final AmazingDot game;

   //setting the height and width variables
   private int height;
   private int width;
   private static int touchCounter;

   //setting the two dots variables
   private Texture playerDotImage;
   private Texture gameDotImage;
   private Texture gameDotImage1;

   private Rectangle playerDotRectangle;
   private Map<Rectangle,Texture> gameDotMap;

   //storing the time of last dot in nano seconds
   private long lastDotTime;

   public GameScreen(final AmazingDot gam){
       this.game = gam;
       Gdx.input.setInputProcessor(this);

       //getting the height and width of the user's screen
       height = Gdx.graphics.getHeight();
       width = Gdx.graphics.getWidth();
       touchCounter =0;

       //loading the images in the variables
       playerDotImage = new Texture(Gdx.files.internal("images/dot2.png"));

       gameDotImage = new Texture(Gdx.files.internal("images/dot1.png"));
       gameDotImage1 = new Texture(Gdx.files.internal("images/dot2.png"));

       //placing the player dot in the middle of the screen
       playerDotRectangle = new Rectangle();
       playerDotRectangle.x = width/ 2 - 64 / 2;
       playerDotRectangle.y = 20;
       playerDotRectangle.width = 64;
       playerDotRectangle.height = 64;

       gameDotMap = new ConcurrentHashMap<Rectangle, Texture>();

       populateDots();
   }

   private void populateDots(){
       Rectangle dots = new Rectangle();
       dots.x = randomLocation();
       dots.y = height;
       dots.width = 64;
       dots.height = 64;

       Random rand = new Random();
       int  n = rand.nextInt(2) + 1;
       if(n==1){
           gameDotMap.put(dots,gameDotImage1);
       }
       else{
           gameDotMap.put(dots,gameDotImage);
       }

       lastDotTime = TimeUtils.nanoTime();
   }

   private int randomLocation(){
       int[] locations = new int[3];
       locations[0]=0;
       locations[1]=width/2-64/2;
       locations[2]=width-64;

       Random generator = new Random();
       int randomIndex = generator.nextInt(locations.length);
       return locations[randomIndex];
   }

   @Override
   public void render(float delta) {
       Gdx.gl.glClearColor(0, 0, 0, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
       game.batch.begin();

       game.batch.draw(playerDotImage,playerDotRectangle.x,playerDotRectangle.y,playerDotRectangle.width,playerDotRectangle.height);

       for(Map.Entry<Rectangle,Texture> dots : gameDotMap.entrySet()){
           game.batch.draw(dots.getValue(),dots.getKey().x,dots.getKey().y);
       }

       game.batch.end();

       // check if we need to create a new dot
       if(TimeUtils.nanoTime() - lastDotTime > 1000000000) populateDots();

       for(Rectangle dot : gameDotMap.keySet()){

           int gameSpeed = 400;
           int xSpeed = calXSpeed(gameSpeed);
           dot.y = dot.y - gameSpeed * Gdx.graphics.getDeltaTime();

           if(dot.x <width/2-64/2){
               dot.x = dot.x + xSpeed*Gdx.graphics.getDeltaTime();
           }
           else if(dot.x>width/2-64/2){
               dot.x = dot.x - xSpeed*Gdx.graphics.getDeltaTime();
           }
           if(dot.y + 64 < 0) gameDotMap.remove(dot);
           if(dot.overlaps(playerDotRectangle)) {
               //this is where I am trying to remove the map object on collision
               gameDotMap.remove(dot);
           }
       }
   }

   private int calXSpeed(int gameSpeed){
       int seconds = height/gameSpeed;
       int distance = width/2-64/2;
       int speed = distance/seconds;
       return speed;
   }

   @Override
   public boolean touchDown(int screenX, int screenY, int pointer, int button) {
       touchCounter++;
       if(touchCounter % 2==0){
           playerDotImage = new Texture(Gdx.files.internal("images/dot2.png"));
       }
       else{
           playerDotImage = new Texture(Gdx.files.internal("images/dot1.png"));
       }
       return true;
   }

   @Override
   public void dispose() {
       playerDotImage.dispose();
       gameDotImage.dispose();
       gameDotImage1.dispose();
   }
}

编辑

enter image description here

正如您在上图中看到的那样,当移动点到达静止点时,它应该消失。但在我的代码中,点只是移动经过固定点。我使用矩形(LibGdx)来检测点是否相互重叠。

最佳答案

将您的Map定义为ArrayMap像这样:

private ArrayMap<Rectangle, Texture> gameDotMap;

然后你可以像这样重写你的 render 方法:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    game.batch.begin();

    game.batch.draw(playerDotImage, playerDotRectangle.x, playerDotRectangle.y, playerDotRectangle.width, playerDotRectangle.height);

    for (int i = 0; i < gameDotMap.size; i++) {
        game.batch.draw(gameDotMap.getValueAt(i), gameDotMap.getKeyAt(i).x, gameDotMap.getKeyAt(i).y);
    }

    game.batch.end();

    // check if we need to create a new dot
    if (TimeUtils.nanoTime() - lastDotTime > 1000000000) {
        populateDots();
    }

    for (Iterator<ObjectMap.Entry<Rectangle, Texture>> iter = gameDotMap.iterator(); iter.hasNext();) {
        ObjectMap.Entry<Rectangle, Texture> entry = iter.next();
        Rectangle dot = entry.key;
        int gameSpeed = 400;
        int xSpeed = calXSpeed(gameSpeed);
        dot.y = dot.y - gameSpeed * Gdx.graphics.getDeltaTime();

        if (dot.x < width / 2 - 64 / 2) {
            dot.x = dot.x + xSpeed * Gdx.graphics.getDeltaTime();
        } else if (dot.x > width / 2 - 64 / 2) {
            dot.x = dot.x - xSpeed * Gdx.graphics.getDeltaTime();
        }
        if (dot.y + 64 < 0) {
            iter.remove();
        }
        if (dot.overlaps(playerDotRectangle)) {
            //this is where I am trying to remove the map object on collision
            iter.remove();
        }
    }
}

LibGDX 有特定的集合类型,推荐使用它们而不是 JDK 的集合。

关于java - 无法在 libgdx 中隐藏碰撞图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38608418/

相关文章:

java - libGDX 相机不翻译

java - 尝试访问 session bean 时出错

java - 如何找到哪个 java 类读取特定的 xml 文件?

java EE 安装和设置

android - ListView 具有延迟加载和下载图像功能

java - 如何使用Input/OutputStream读取/写入数据而不创建新对象LibGDX

android - 如何检测 libgdx 中的 Actor 何时被触摸?

java - 当出现内存不足错误时自动重启tomcat

php - 将应用程序连接到本地主机上的 php 服务器

android - 如何在Gradle中删除任务名称中的斜杠