gpt4 book ai didi

java - Android Studio LibGDX 游戏不显示障碍物和地板

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

我正在尝试制作一款回避游戏,已完成 95%,但是,当我尝试添加无限障碍时,游戏从一开始就看起来像这样(没有错误):How the game looks

我不知道我做错了什么,有人可以帮助我吗?我将非常感激,因为这是我为让我的游戏正常运行而需要做的最后一件事。我的 GameScreen 中的代码:

    package com.circlecrashavoider;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.circlecrashavoider.entities.FloorEntity;
import com.circlecrashavoider.entities.ObstacleEntity;
import com.circlecrashavoider.entities.ObstacleEntity2;
import com.circlecrashavoider.entities.PlayerEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class GameScreen extends BaseScreen {

private Stage stage;
private World world;
private PlayerEntity player;
private List<FloorEntity> floorList = new ArrayList<FloorEntity>();
private List<ObstacleEntity> obstacleList = new ArrayList<ObstacleEntity>();
private List<ObstacleEntity2> obstacle2List = new ArrayList<ObstacleEntity2>();

public GameScreen(MainGame game) {
super(game);
stage = new Stage(new FitViewport(1024, 620));
world = new World(new Vector2(0, -10), true);


world.setContactListener(new ContactListener() {

private boolean areCollided(Contact contact, Object userA, Object userB) {
return (contact.getFixtureA().getUserData().equals(userA) && contact.getFixtureB().getUserData().equals(userB)) ||
(contact.getFixtureA().getUserData().equals(userB) && contact.getFixtureB().getUserData().equals(userA));
}

@Override
public void beginContact(Contact contact) {
if (areCollided(contact, "player", "floor")) {
player.setJumping(false);
if (Gdx.input.isTouched()) {
player.setMustJump(true);
}
}

if (areCollided(contact, "player", "obstacle")) {
player.setAlive(false);
System.out.println("GAME OVER");

}

if (areCollided(contact, "player", "obstacle2")) {
player.setAlive(false);
System.out.println("GAME OVER");
}
}

@Override
public void endContact(Contact contact) {

}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {

}

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {

}
});
}

@Override
public void show() {
}

private float spawnTime = 4f;
private float timer = 0;

public void update(float deltaTime) {
timer += deltaTime;


if (timer >= spawnTime) {
this.spawnEntity();

spawnTime = MathUtils.random(2f, 4f);
timer = 0;
}
}


private void spawnEntity(){

Texture floorTexture = game.getManager().get("floor.png");
Texture overfloorTexture = game.getManager().get("overfloor.png");
Texture overfloor2Texture = game.getManager().get("overfloor2.png");
Texture obstacleTexture = game.getManager().get("obstacle.png");
Texture obstacle2Texture = game.getManager().get("obstacle2.png");
//Spawn your object
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture, 0, 1000, 1));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,8, 10 ,5));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,10, 10 ,8));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,34 , 3 ,5));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,19 , 8 ,4));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,24 , 8 ,1.5f));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,27 , 5 , 2));
obstacleList.add(new ObstacleEntity(world, obstacleTexture, 6, 1));

stage.addActor(player);
for (FloorEntity floor: floorList) {
stage.addActor(floor);
}
for (ObstacleEntity obstacle : obstacleList) {
stage.addActor(obstacle);
}
}

@Override
public void render(float delta) {
Gdx.gl20.glClearColor(0.5f, 0.6f, 1, 3f);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);



stage.act();
world.step(delta, 6, 2);
stage.draw();
}



@Override
public void dispose() {
stage.dispose();
world.dispose();
}
}

我的 MainGame 类中的代码:

    package com.circlecrashavoider;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.circlecrashavoider.scene2d.Box2DScreen;
import com.circlecrashavoider.scene2d.Scene2DScreen;

public class MainGame extends Game {

private AssetManager manager;

public AssetManager getManager() {
return manager;
}

@Override
public void create() {
manager = new AssetManager();
manager.load("floor.png", Texture.class);
manager.load("overfloor.png", Texture.class);
manager.load("overfloor2.png", Texture.class);
manager.load("obstacle.png", Texture.class);
manager.load("obstacle2.png", Texture.class);
manager.load("crash.png", Texture.class);
manager.load("player.png", Texture.class);
manager.finishLoading();

setScreen(new GameScreen(this));
}
}

最佳答案

PlayerEntity、FloorEntity 和 ObstacleEntity 类的代码在这篇文章 https://stackoverflow.com/questions/35678654/issues-getting-android-studio-game-to-run 中。在此链接上,您将找到所需的代码 Fuat Coskun

关于java - Android Studio LibGDX 游戏不显示障碍物和地板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665703/

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