我是 libgdx 的新手,而且我只使用了 2 周。我的问题是理解纹理加载的工作原理。
我设法使用 gdx.files.internal(player.png) 或类似的东西加载我的第一个纹理(播放器),并且工作正常。我添加了一些功能,让他通过“如果按下按键”命令从一侧移动到另一侧,这也有效。
加载另一个纹理时出现问题。我想制作一个敌方玩家,但对如何做到这一点知之甚少。我想如果我执行“gdx.files.internal(enemy.png)”,它会加载到我的敌人纹理中,但事实并非如此,而是加载到另一个player.png纹理中。
我的问题是如何加载敌人.png?我看过一些有用的教程,但它每次都会一次又一次地加载我的播放器。
如果有人可以解释如何有效地使用多个纹理,那将会很有帮助,因为我觉得我这样做的方式并不完全是最佳实践。
package com.mohamed.JungleFighter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
//i'm extending libgdx's built in game class which implements the activity listener
public class JungleFighterMain extends Game {
private OrthographicCamera camera;
private SpriteBatch sBatch;
private Texture player;
private Texture enemy;
//private SpriteBatch enemyBatch;
private Sprite sprite;
//just setting my game heighty and width
public static int gameWidth = 500, gameHeight = 500;
@Override
public void create () {
//camera related
camera = new OrthographicCamera(gameWidth, gameHeight);
//end of camera related
sBatch = new SpriteBatch();
player = new Texture(Gdx.files.internal("plane.png"));
sprite = new Sprite(player);
enemy = new Texture(Gdx.files.internal("enemy.png"));
sprite = new Sprite(enemy);
}
public void dispose() {
sBatch.dispose();
player.dispose();
enemy.dispose();
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// camera related
sBatch.setProjectionMatrix(camera.combined);
//keyboard functions
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
sprite.translateX(-1f);
else
sprite.translateX(-20.1f);
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
sprite.translateX(1f);
else
sprite.translateX(20.1f);
}
sBatch.begin();
//sprite.setPosition(-200, -200);
sprite.draw(sBatch);
sBatch.end();
}
public void resize(int width, int height){
}
public void pause(){
}
public void resume(){
}
}
我想我找到了你的问题
player = new Texture(Gdx.files.internal("plane.png"));
sprite = new Sprite(player);
enemy = new Texture(Gdx.files.internal("enemy.png"));
sprite = new Sprite(enemy);
看,你只有一个精灵。如果你只想让一个敌人加载他,如果你想要两个,就制作 2 个精灵..
player = new Texture(Gdx.files.internal("plane.png"));
sprite1 = new Sprite(player);
enemy = new Texture(Gdx.files.internal("enemy.png"));
sprite2 = new Sprite(enemy);
记住,如果你想绘制纹理,你也可以使用
batch.draw(textureName,xPos,yPos);