我正在使用libGDX构建一个简单的自上而下的RPG游戏,但没有找到任何关于如何在平铺地图上检测碰撞的精确指针。
我有一个字符类,看起来像这样
public class Son{
SpriteBatch batch;
Texture texture;
Sprite sprite;
private TiledMapTileLayer collisionLayer;
public Son(float x, float y, TiledMapTileLayer collisionLayer){
this.collisionLayer = collisionLayer;
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("characters/xter1.png"));
sprite = new Sprite(texture);
sprite.setX(x);
sprite.setY(y);
}
public void render(){
processKeys();
batch.begin();
sprite.draw(batch);
batch.end();
}
private void processKeys(){
if(Gdx.input.isTouched()){
//Gdx.app.exit();
Gdx.app.log("x touched", Gdx.input.getX() + "");
Gdx.app.log("y touched", Gdx.input.getY() + "");
//480 by 840
//left
if(Gdx.input.getX() >= 0 && Gdx.input.getX() <= 80){
if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80){
sprite.setPosition(sprite.getX() - 10, sprite.getY());
}
}
//right
if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/8.4) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/8.4) + 80){
if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80) {
sprite.setPosition(sprite.getX() + 10, sprite.getY());
}
}
//up
if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/3.2 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/3.2 + 80){
sprite.setPosition(sprite.getX(), sprite.getY() + 10);
}
}
//down
if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/48 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/48 + 80){
sprite.setPosition(sprite.getX(), sprite.getY() - 10);
}
}
}
}
}
此代码只是根据用户按下屏幕的哪个部分更改角色的位置。
包含平铺地图的类如下所示
public class StageOneScreen implements Screen {
TiledMap tiledMap;
OrthographicCamera camera;
FitViewport viewPort;
TiledMapRenderer tiledMapRenderer;
Stage stage;
ControllerRenderer controllerRenderer;
Son son;
private Music music;
final float GAME_WORLD_HEIGHT = 100;
final float GAME_WORLD_WIDTH = 50;
private TiledMap collisionMap;
private TiledMapTileLayer collisionLayer;
public StageOneScreen(){
float aspectRatio = (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth();
camera = new OrthographicCamera(GAME_WORLD_HEIGHT * aspectRatio, GAME_WORLD_HEIGHT);
camera.position.set(GAME_WORLD_WIDTH/2, GAME_WORLD_HEIGHT/2, 0);
//viewPort = new FitViewport(800, 400, camera);
camera.setToOrtho(false, 800, 400);
camera.update();
tiledMap = new TmxMapLoader().load("stages/stage1.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
stage = new Stage(new ScreenViewport());
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
Gdx.input.setInputProcessor(stage);
}
@Override
public void show() {
// playMusic();
controllerRenderer = new ControllerRenderer();
son = new Son(100, 100, collisionLayer);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_BLEND_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
controllerRenderer.render(camera);
son.render();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
controllerRenderer.dispose();
}
public void playMusic(){
// load music asset and check if the loop state before playing it
music = SoundAsset.loadMusic(SoundAsset.MUSIC_NAME);
music.setLooping(true);
if (music.isLooping())
music.play();
}
}
在我正在渲染的平铺地图上,我已经给某些瓷砖赋予了属性“墙”。我也使用了两层。我不希望“儿子”角色能够穿过的瓷砖在第二层。我如何阻止角色穿过某些瓷砖?
如果你正在使用TiledMap编辑器:http://www.mapeditor.org/,,你可以使用编辑器中的一些标签来设置你的碰撞属性,并从你的代码中检索值。
当我在一个小型2D游戏中使用Libgdx时,我有一个绘制和移动对象的策略模式。假设您已经设置了一些属性,只需阅读它并执行逻辑即可。类似于“玩家移动时,如果nextCase==wall,nextCase=currentCase”