程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Java:运动,需要放慢速度

发布于2024-03-09 13:31     阅读(643)     评论(0)     点赞(6)     收藏(4)


好吧,我已经尝试 Java 几个星期了,遵循课堂和在线教程。我做了一个简单的游戏,其中方块落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。

我遇到的问题是方块开始下降得太快。现在我将它们设置如下:

    ix = 0;
    iy = 1;

然后在我的 move() 方法中,我有以下内容:

    hitbox.x += ix;
    hitbox.y += iy;

在此示例中,ix 和 iy 都是整数。

我的第一个假设是将整数更改为浮点数,然后使用:

    ix= 0;
    iy = 0.5;

进而:

 hitbox.x += ix;
 hitbox.y += 0.5f;

但这只是将物体冻结在它们的轨道上。我相信这是因为绳索被视为整数,所以我想如果我修改 getX() 和 getY() 方法,也许我可以以某种方式操纵它们以使用十进制数字?但我不太确定该怎么做。对于此问题的任何帮助/提示/解决方案将不胜感激。

这是一些相关代码,如果需要的话请告诉我!

敌人经理:

 public class EnemyManager {

private int amount;
private List<Enemy> enemies = new ArrayList<Enemy>();
private GameInJavaOne instance;

public EnemyManager(GameInJavaOne instance, int a){
    this.amount = a;
    this.instance = instance;
    spawn();
}

private void spawn(){
    Random random = new Random();
    int ss = enemies.size();
    // If the current amount of enemies is less than the desired amount, we spawn more enemies.
    if(ss < amount){
        for(int i = 0; i < amount - ss; i++){
             enemies.add(new Enemy(instance, random.nextInt(778), random.nextInt(100)+1));
        }
        // If its greater than the desired number of enemies, remove them.
    }else if (ss > 20){
        for(int i = 0; i < ss - amount; i++){
             enemies.remove(i);
        } 
    }
}

public void draw(Graphics g){
    update();
    for(Enemy e : enemies) e.draw(g);

}

private void update() {
    boolean re = false;
    for(int i = 0; i < enemies.size(); i ++){
        if(enemies.get(i).isDead()){
            enemies.remove(i);
            re = true;
        }

    }
    if(re) spawn();
}

public boolean isColliding(Rectangle hitbox){
    boolean c =false;
     for(int i = 0; i < enemies.size(); i ++){
         if(hitbox.intersects(enemies.get(i).getHitbox())) c = true;
        }
        return c;

}

 }

实体:

 public abstract class Entity {

protected int x, y, w, h;
protected boolean removed = false;

public Entity(int x, int y){

    this.x = x;
    this.y = y;

}

public void Draw(Graphics g){

}

public int getX() { return x; }
public int getY() { return y; }
public int getH() { return h; }
public int getW() { return w; }




 }

和敌人类别:

 public class Enemy extends Entity{

private Rectangle hitbox;
private int ix, iy;
private boolean dead = false;
private GameInJavaOne instance;




public Enemy(GameInJavaOne instance, int x, int y){
    super(x, y);
    this.instance = instance;
    hitbox = new Rectangle(x, y, 32, 32);

    ix = 0;
    iy = 1;

}

private void move(){
    if(instance.getStage().isCollided(hitbox)){
        iy =0;
        dead = true;
    }
    hitbox.x += ix;
    hitbox.y += iy;
}


public boolean isDead() {return dead;}


public Rectangle getHitbox() {return hitbox; }


public void draw(Graphics g){
    move();
    g.setColor(Color.MAGENTA);
    g.fillRect(hitbox.x, hitbox.y, hitbox.height, hitbox.width);
}

 }

解决方案


您正在使用一个Rectangle类来表示您的盒子的位置(即使您称其为hitbox),Rectangle确实有成员x并且y哪些是整数,所以当您调用

rectangle.x+=0.5f;

你真正调用的是rectangle.x+=(int)0.5f;and (int)0.5f==0

如果您想要浮动精度,则该类Rectangle根本不适合保持框的位置。考虑将盒子的真实位置保留为 double 或 float 并转换为 int 以渲染它。

所以你的渲染代码将变成;

g.fillRect((int)positionX,(int)positionY, hitbox.height, hitbox.width);

其中positionXpositionY是双精度数。Vector2d(如果您愿意将x和放在一起,也可以使用y

其他要点

  • 您似乎正在使用和扩展一个Entity但从未使用它们,这看起来很危险,为什么您要扩展但重新创建自己的位置代码。xywhEntity
  • 你的游戏循环没有显示,但是,我可以看到你硬编码了 x 和 y 的变化。这可能是因为在你的游戏循环中你“要求”一些帧速度,比如 60fps,并假设你会得到它。这在资源丰富的系统上工作得很好,但是一旦出现资源短缺,您就会开始获得短于 60fps 的帧。在大多数游戏中,您甚至不会注意到这一点,因为它只是进行更大的跳跃来进行补偿,但在这里您假设 60 fps。明智的做法是获取实际的帧时间并将其乘以速度以获得 x 和 y 的变化。


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/686165/ceae9b90b71383627de4/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)