Java 如何使用libgdx的不同方法绘制精灵
嗨,我在我的创建方法中创建了一个按钮。我向按钮添加了一个addlistener和一个inputevent。但是,如果按下按钮,如何在“创建”方法中渲染精灵。一个精灵被画出来了我怎么做/Java 如何使用libgdx的不同方法绘制精灵,java,android,mobile,libgdx,2d,Java,Android,Mobile,Libgdx,2d,嗨,我在我的创建方法中创建了一个按钮。我向按钮添加了一个addlistener和一个inputevent。但是,如果按下按钮,如何在“创建”方法中渲染精灵。一个精灵被画出来了我怎么做/ public void create () { buttonStyle = new TextButtonStyle(); buttonStyle.up = skin.getDrawable("button"); buttonStyle.over = skin.getDrawable("b
public void create () {
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.over = skin.getDrawable("buttonpressed");
buttonStyle.down = skin.getDrawable("buttonpressed");
buttonStyle.font = font;
button = new TextButton("START", buttonStyle);
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
button.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button ){
drawTile(200,50);
return true;
}
});
// method used to draw a sprite when passing certain coordinates
public void drawTile(int x , int y){
spriteBatch.draw(sprite, x , y );
}
public void render () {
Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
spriteBatch.begin();
spriteBatch.draw(background, 0, 0);
drawGrid();
spriteBatch.draw(startButton, 0, 0);
stage.draw();
spriteBatch.end()
}
通常我的实体有一个管理器来控制这样的情况(不确定是否是最好的方法),您必须调整代码 首先,我有一个抽象类
实体
,它将是我游戏中所有实体的父类,因此如果我有敌人或玩家,例如,我将有两个类敌人
和玩家
,它们将从实体
扩展。这个抽象类有两个主要方法,update()
和render()
:
最后,无论你在哪里有你的按钮监听器
myButton.addListener(new InputListener() {
public boolean touchDown (...) {
Gdx.app.log("I'll add an entity");
EntityManager.add(new Enemy()); // <--- here, new entity added!
// EntityManager.add(new OtherEnemy()); <-- other example
return false;
}
public void touchUp (...) {
Gdx.app.log("blah");
}
});
myButton.addListener(新的InputListener(){
公共布尔接地(…){
Gdx.app.log(“我将添加一个实体”);
EntityManager.add(new敌手());//我正在使用Screen-2D构建一个按钮。我想在按钮单击时为其提供一个函数。将绘制一个精灵。我如何执行此操作。这不是我所有的代码,但足以显示我在说什么。
public class EntityManager{
// A list of ALL your entities in your game...
private static Array<Entity> myEntityList = new Array<Entity>();
/**
* A very simple add method... This is what you will call when you touch
* your button.
*/
public static void add(Entity newEntity){
myEntityList.add(newEntity);
}
public static void update(float delta) {
// Take care of those entities that are being updated or expired!
...
// Update each of my entities
for(Entity e : myEntityList){
e.update(delta); // <- I'll update all my entities
}
...
}
public static void render(SpriteBatch spritebatch){
// draw each of my entities
for(Entity e : myEntityList){
e.render(spritebatch); // <- I'll draw all my entities
}
}
// dispose and other things are here too (omitted for simplicity)
}
@Override
public void render(float delta){
// code here
...
EntityManager.update(delta); // <- Update all your entities
spritebatch.begin();
EntityManager.render(spritebatch); // <- Draw all your entities
spritebatch.end();
...
// more code here
}
myButton.addListener(new InputListener() {
public boolean touchDown (...) {
Gdx.app.log("I'll add an entity");
EntityManager.add(new Enemy()); // <--- here, new entity added!
// EntityManager.add(new OtherEnemy()); <-- other example
return false;
}
public void touchUp (...) {
Gdx.app.log("blah");
}
});