gpt4 book ai didi

java - 实体到达屏幕边缘时减速

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

这是我的核心项目:

public class GameClass extends Game {

public static int screenWidth, screenHeight;

public static CustomScreen currentScreen;
public static PlayScreen playScreen;

@Override
public void create () {
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
CustomScreen.initialize();

playScreen = new PlayScreen(this);

SetScreen(playScreen);
}

public void SetScreen(CustomScreen screen) {
currentScreen = screen;
setScreen(currentScreen);
}
}

public abstract class CustomScreen implements Screen {

GameClass game;

static BitmapFont font;
static SpriteBatch batcher;
static OrthographicCamera cam;

public CustomScreen(GameClass game) {
this.game = game;
}

public static void initialize() {
cam = new OrthographicCamera();
cam.setToOrtho(true, GameClass.screenWidth, GameClass.screenHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
font = new BitmapFont();
font.setScale(4f, -4f);
}

public void Clear() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}

@Override
public abstract void render(float delta);
}

public class PlayScreen extends CustomScreen {

public static final int speed = 300;

public ArrayList<Entity> entityList;

Random rand = new Random();
float timer = rand.nextInt(2) + rand.nextFloat();

public PlayScreen(GameClass game) {
super(game);

entityList = new ArrayList<Entity>();
}

void update(float delta) {
timer -= delta;
if (timer <= 0) {
entityList.add(new Enemy(GameClass.screenWidth, rand.nextInt(GameClass.screenHeight - Enemy.Height)));
timer += rand.nextInt(2) + rand.nextFloat() + 1/2;
}

for (int i = entityList.size(); i > 0; --i)
entityList.get(i-1).update(delta);
}

@Override
public void render(float delta) {
Clear();
update(delta);

batcher.begin();

for (int i = 0; i < entityList.size(); ++i) {
entityList.get(i).Display(batcher);
}

if (entityList.size() > 1)
System.out.println(entityList.get(1).posX - entityList.get(0).posX);

batcher.end();
}
}

public abstract class Entity {

protected Sprite sprite;
public int posX, posY, width, height;

public Entity(int posX, int posY, int width, int height) {
this.posX = posX;
this.posY = posY;
this.width = width;
this.height = height;
}

public abstract void update(float delta);

public void Display(SpriteBatch batcher) {
batcher.draw(sprite, posX, posY, width, height);
}
}

public class Enemy extends Entity {

static Sprite texture = new Sprite(new Texture(Gdx.files.internal("enemy.png")));
public static int Width = 300, Height = 200;

public Enemy(int posX, int posY) {
super(posX, posY, Width, Height);
this.sprite = Enemy.texture;
}

@Override
public void update(float delta, int i) {
posX -= delta * PlayScreen.speed;

if (posX + width < 0) {
GameClass.playScreen.entityList.remove(this);
}
}

}

在 PlayScreen 中,敌人不断随机生成,并且以恒定速度从屏幕右侧向左侧移动(最终整数 300)。但是当它们到达屏幕的左边缘时(当 posX <= 0 时),它们会减慢,原因未知。问题是,当敌人到达屏幕边缘时,我没有编写任何程序来发生任何事情。(当 posX + width <= 0 时,我将它们编程为当它们完全在屏幕之外时消失,但这与我的问题无关,因为即使当我删除它时,它们在到达屏幕边缘时也会继续减速)。

桌面和 Android 项目都会发生这种情况,所以这肯定来自 Core 项目。

我不知道为什么会发生这种情况,这真的非常非常尴尬。

这里有几张图片向您展示会发生什么。

/image/DrOSH.png

/image/Zjtju.png

我们可以看到第二张图片上的两个敌人比第一张图片上的距离更近。

你可以将PlayScreen.speed设置为100而不是300,它会更明显。如果您将其设置为足够低的值,例如 20,敌人不仅会减慢速度,他们基本上会停止移动

我迷路了,不知道如何解决这个问题。如果您有的话,请随时分享。

最佳答案

我修好了。问题是 Enemy.posX 是一个 int 而不是 float。

关于java - 实体到达屏幕边缘时减速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254646/

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