gpt4 book ai didi

java - 无法同时绘制 ninepatch 图像和舞台

转载 作者:行者123 更新时间:2023-11-29 08:53:58 27 4
gpt4 key购买 nike

每当我尝试绘制一个 ninepatch 图像和一个舞台时,最后调用的东西正在绘制。我曾尝试使用正交相机但没有成功。我尝试过的:

batch.setProjectionMatrix(camera.combined);
ninePatch.draw(batch, xPos, yPos, width, height);
stage.act(delta);
stage.draw();

camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.setCamera(camera)

编辑:代码

CreateQuiz 类

public class CreateQuiz implements Screen{

public CreateQuiz(Quiz quiz){
this.quiz = quiz;
}

private Quiz quiz;
private FallDownPanel fallDownPanel;
private OrthographicCamera camera;

@Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
quiz.beginBatch();
fallDownPanel.render(quiz.getSpriteBatch(), delta);
quiz.endBatch();
}

@Override
public void resize(int width, int height) {

}

@Override
public void show() {
fallDownPanel = new FallDownPanel(FallDownPanel.START_LOWER_SIDE, 200, quiz.getTextureAtlas().createPatch("fallDownWindow"));
Stage stage = new Stage(fallDownPanel.getWidth(), fallDownPanel.getHeight(), false);
TextFieldStyle style = quiz.getGreenTextFieldStyle();
style.font.scale(-0.30f);
TextFieldQuiz test = new TextFieldQuiz("Hej åäö 123 !", style, 0, 2, 400, 16);
test.setTextFieldListener(new TextFieldListener() {
@Override
public void keyTyped (TextField textField, char key) {
if (key == '\n') {
textField.getOnscreenKeyboard().show(false);
}
}
});
stage.addActor(test);
Gdx.input.setInputProcessor(stage);
fallDownPanel.setStage(stage);
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.setCamera(camera);
fallDownPanel.setCamera(camera);
}

@Override
public void hide() {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {

}

}

FallDownPanel.class

    public class FallDownPanel {

//With repeat
public FallDownPanel(int startSide, int size, NinePatch ninePatch){
Tween.registerAccessor(FallDownPanel.class, new FallDownPanelTween());
Tween.registerAccessor(Widget.class, new WidgetTween());
tweenManager = new TweenManager();
final int screenWidth = Gdx.graphics.getWidth();
final int screenHeight = Gdx.graphics.getHeight();
int target = 0;
int tweenType = 0;

if(startSide == START_LEFT_SIDE){
setSize(size, screenHeight);
xPos = -size;
yPos = 0;
target = 0;
tweenType = FallDownPanelTween.POSITION_X;
}

else if(startSide == START_RIGHT_SIDE){
setSize(size, screenHeight);
xPos = screenWidth;
yPos = 0;
target = screenWidth - size;
tweenType = FallDownPanelTween.POSITION_X;
}

else if(startSide == START_UPPER_SIDE){
setSize(screenWidth, size);
xPos = 0;
yPos = screenHeight + size;
target = screenHeight - size;
tweenType = FallDownPanelTween.POSITION_Y;
}

else if(startSide == START_LOWER_SIDE){
setSize(screenWidth, size);
xPos = 0;
yPos = -size;
target = 0;
tweenType = FallDownPanelTween.POSITION_Y;
}

Tween.to(this, tweenType, 1).target(target).start(tweenManager);
this.tweenType = tweenType;
this.startSide = startSide;

this.ninePatch = ninePatch;
}

private TweenManager tweenManager;
private NinePatch ninePatch;
private Stage stage;
private OrthographicCamera camera;

private float xPos, yPos;
private int width, height;
private int tweenType;
private int startSide;
public static final int START_LEFT_SIDE = 0, START_RIGHT_SIDE = 1, START_UPPER_SIDE = 2, START_LOWER_SIDE = 3;

public void render(SpriteBatch batch, float delta){
tweenManager.update(delta);
batch.setProjectionMatrix(camera.combined);
ninePatch.draw(batch, xPos, yPos, width, height);
stage.act(delta);
stage.draw();
}

public void setX(float x){
this.xPos = x;
}

public void setY(float y){
this.yPos = y;
}

public float getX(){
return xPos;
}

public float getY(){
return yPos;
}

public float getWidth(){
return width;
}

public float getHeight(){
return height;
}

private void setSize(int w, int h){
width = w;
height = h;
}

public Stage getStage() {
return stage;
}

public void setStage(Stage stage) {
this.stage = stage;
startWidgetTweens();
}

private void startWidgetTweens(){
float size = getShortestSide();
Array<Actor> actors = stage.getActors();
for(int i = 0; i < actors.size; i++){
Widget w = (Widget) actors.get(i);
Tween.to(w, tweenType, 1).target(0).start(tweenManager);
}
}

private float getShortestSide() {
if(startSide == START_LEFT_SIDE){
return width;
}

else if(startSide == START_RIGHT_SIDE){
return width;
}

else if(startSide == START_UPPER_SIDE){
return height;
}

else if(startSide == START_LOWER_SIDE){
return height;
}

return -1;
}

public void setCamera(OrthographicCamera camera) {
this.camera = camera;
}

}

最佳答案

始终确保您一次只有一个 Activity 的Renderer渲染器是:

  • 批量
  • SpriteBatch
  • ShapeRenderer
  • Box2DDebugRenderer 如果我没记错的话
  • ModelBatch 用于 3D

我希望我没有忘记一个。
确保在为新的调用 begin() 之前始终为 Activity 的调用 end()
还要记住,Stage 有自己的 SpriteBatch 并且它会为此 SpriteBatch 调用 begin(),如果您调用 stage.draw()。因此,请确保在 stage.draw() 之前 end() 激活 Renderer
如果您需要 ShapeRenderer 或任何其他渲染器来绘制 Actor(可能是调试原因),您必须结束 stageSpriteBatch,您可以通过 draw() 方法获得。确保在方法结束时再次begin()
希望这已经足够清楚了。

关于java - 无法同时绘制 ninepatch 图像和舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318030/

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