这是一款在 LibGDX 中制作的游戏。用户可以生成绵羊,然后绵羊会随机徘徊。有两个问题。
- 羊并不总是改变他们“正确”的变量。
- 它们似乎总是以相同的方式移动,这是不应该发生的。
这是 MainClass 和 Entity 类的代码。其他不应该是这个错误的原因。
主要:
package com.god.game;
import com.badlogic.gdx.*;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MainClass extends ApplicationAdapter {
public static final int width = 1600;
public static final int height = 1000;
public static final String title = "God Game";
public static EntityType[] entityTypes;
public static float rand, cameraSpeed;
public static Comparator<Entity> layerComp;
public static int selectedId;
public static String aiStr;
public static InputMultiplexer input;
SpriteBatch batch;
public static ArrayList<Entity> entities;
public static OrthographicCamera camera;
public static Vector3 mousePos;
@Override
public void create () {
input = new InputMultiplexer();
input.addProcessor(new com.god.game.Input());
input.addProcessor(new GestureDetector(new Gestures()));
Gdx.input.setInputProcessor(input);
entityTypes = new EntityType[]{new EntityType("Sheep", new Vector2(55, 38), new TextureAtlas(Gdx.files.internal("Textures/sheep.atlas")), 20, new AI(new String[]{"Wander"}))};
entities = new ArrayList<Entity>();
layerComp = new Comparator<Entity>() {
@Override
public int compare(Entity e1, Entity e2) {
if (e1.getPosition().y > e2.getPosition().y)
return -1;
if (e1.getPosition().y < e2.getPosition().y)
return 1;
return 0;
}
};
camera = new OrthographicCamera(width, height);
cameraSpeed = 500;
selectedId = 0;
batch = new SpriteBatch();
}
public void update(float deltaTime){
handleInput(deltaTime);
for(Entity e : entities){
e.update(deltaTime);
}
Collections.sort(entities, layerComp);
camera.update();
}
@Override
public void render () {
update(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0, 0.8f, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Entity e : entities){
if(!e.isRight())
entityTypes[e.getId()].getAtlas().findRegion(e.getFrame()).flip(true, false);
batch.draw(entityTypes[e.getId()].getAtlas().findRegion(e.getFrame()), e.getPosition().x, e.getPosition().y);
if(!e.isRight())
entityTypes[e.getId()].getAtlas().findRegion(e.getFrame()).flip(true, false);
}
batch.end();
}
@Override
public void dispose(){
batch.dispose();
for(EntityType e : entityTypes)
e.dispose();
}
public void handleInput(float deltaTime){
mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos);
if(Gdx.input.isKeyPressed(Input.Keys.W))
camera.translate(0, cameraSpeed*deltaTime);
if(Gdx.input.isKeyPressed(Input.Keys.A))
camera.translate(-cameraSpeed*deltaTime, 0);
if(Gdx.input.isKeyPressed(Input.Keys.S))
camera.translate(0, -cameraSpeed*deltaTime);
if(Gdx.input.isKeyPressed(Input.Keys.D))
camera.translate(cameraSpeed*deltaTime, 0);
}
}
实体:
package com.god.game;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
public class Entity {
private int id;
private Vector2 position, goalPos;
private boolean right;
private String frame;
private float animationTimer;
public Entity(int id, Vector2 position) {
this.id = id;
this.position = position;
this.right = true;
this.frame = "stand";
this.animationTimer = 0;
this.goalPos = Vector2.Zero;
}
public void update(float deltaTime){
MainClass.aiStr = MainClass.entityTypes[id].getAi().getBrainList()[0];
if(MainClass.aiStr == "Wander"){
MainClass.rand = MathUtils.random(1000);
if(MainClass.rand == 1){
goalPos.x = -1;
right = false;
}
else if(MainClass.rand == 2)
goalPos.x = 0;
else if(MainClass.rand == 3){
goalPos.x = 1;
right = true;
}
MainClass.rand = MathUtils.random(1000);
if(MainClass.rand == 1)
goalPos.y = -1;
else if(MainClass.rand == 2)
goalPos.y = 0;
else if(MainClass.rand == 3)
goalPos.y = 1;
moveForward(deltaTime);
}
else{
frame = "stand";
animationTimer = 0;
}
animationTimer += deltaTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Vector2 getPosition() {
return position;
}
public void setPosition(Vector2 position) {
this.position = position;
}
public boolean isRight() {
return right;
}
public void setRight(boolean right) {
this.right = right;
}
public String getFrame() {
return frame;
}
public void setFrame(String frame) {
this.frame = frame;
}
public float getAnimationTimer() {
return animationTimer;
}
public void setAnimationTimer(float animationTimer) {
this.animationTimer = animationTimer;
}
public Vector2 getGoalPos() {
return goalPos;
}
public void setGoalPos(Vector2 goalPos) {
this.goalPos = goalPos;
}
public void moveForward(float deltaTime){
position.x += MainClass.entityTypes[id].getMoveSpeed() * deltaTime * goalPos.x;
position.y += MainClass.entityTypes[id].getMoveSpeed() * deltaTime * goalPos.y;
if(goalPos.x == 0 && goalPos.y == 0){
frame = "stand";
animationTimer = 0;
}
else if(animationTimer >= 0.2f){
if(frame.equals("stand"))
frame = "move";
else
frame = "stand";
animationTimer = 0;
}
}
}
羊每次将 goalPos.x 更改为 -1 或 1 时都应该将其正确的变量设置为 true/false,但他们似乎没有这样做。它们似乎也不是独立移动的,而是像一个阵型,每次都朝着同一个方向移动。他们确实独立地将正确的变量设置为真/假,但这与他们的 Action 不匹配。例如:一只羊向右移动,然后在向右移动的同时随机向左转,等等。
任何帮助将不胜感激,谢谢!
最佳答案
首先,你永远不应该说 Java 的行为很奇怪,而应该说你的代码没有按照你的预期运行。如果您将错误归咎于某些内部 Java 功能,您将永远找不到它们。现在让我们看看如果你的人工智能设置为漫步
会发生什么。
if(MainClass.aiStr == "Wander"){
MainClass.rand = MathUtils.random(1000);
//Random number in the range of 0 to 1000.
if(MainClass.rand == 1){ //happens .1% of the time
goalPos.x = -1;
right = false;
}
else if(MainClass.rand == 2) //happens .1% of the time
goalPos.x = 0;
else if(MainClass.rand == 3){ //happens .1% of the time
goalPos.x = 1;
right = true;
}
//since there is no else nothing happens 99.7% of the time.
}
我的猜测是,您的代码的行为符合您的预期,并且您的计算机内部没有发生黑色巫术。因此,将来责怪自己并检查您的代码,实际上并没有那么多代码,您发布的内容中只有一小部分负责控制您的实体。
现在您可能想要的是这样的 MathUtils.random(1, 4)
因为最大值是独占的。或者如果您希望 25% 的时间什么都不发生,则使用 MathUtils.random(1, 5)
。
就机会而言,一种更具可扩展性的方式我总是更喜欢这样的东西:
int randomNumber = MathUtils.random(1, 101);
if (randomNumber <= 33)
{
//do something 33% of the time
}
else if (randomNumber <= 66)
{
//do something 33% of the time
}
else
{
//do something 34% of the time
}
现在这更容易更改,因为您所需要做的就是更改百分比,并且可以轻松添加另一个案例。
if (randomNumber <= 20)
{
//do something 20% of the time
}
else if (randomNumber <= 60)
{
//do something 40% of the time
}
else if (randomNumber <= 58)
{
//do something 18% of the time
}
else
{
//do something 22% of the time
}
关于java - LibGDX/Java 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537189/