【问题标题】:libGDX actor draw() not called未调用 libGDX 演员 draw()
【发布时间】:2014-12-24 08:51:58
【问题描述】:

请原谅我的英语。

开始探索libGDX,遇到了问题。当我在舞台上添加演员时,不会调用方法 draw()。 尝试应用绘制直线的方法,纹理绘制成功但不是actor,此方法不正确。

请帮忙。

SpiderHunt.java

package com.spiderhunt;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spiderhunt.screens.MainMenu;

    public class SpiderHunt extends Game{   
        private SpriteBatch batch; 
        public MainMenu mainMenu;

        private static SpiderHunt instance = new SpiderHunt();

        public SpiderHunt(){

        }

        public static SpiderHunt getInstance() {
            return instance;
        }

        public void create () {     

            //load textures
            Assets.load();

            batch = new SpriteBatch();

            mainMenu = new MainMenu(batch);
            this.setScreen(mainMenu);
        }

        public void showMainMenu(){     
            setScreen(mainMenu);
        }

        public void render (float delta) {

        }

        public void resize(int width, int height) {

        }

        public void pause() {

        }

        public void resume() {

        }

        public void dispose() {

        }   
    }

MainMenu.java

package com.spiderhunt.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.spiderhunt.Assets;
import com.spiderhunt.buttons.btnPlay;

public class MainMenu implements Screen {

    public btnPlay playButton;
    public Stage stage; 
    public SpriteBatch batch;    

    class GoToGameListener extends ClickListener {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            //some code for click or push
        }
    }

    public MainMenu(SpriteBatch batch_1) {
        batch = batch_1;

        stage = new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);  
        Gdx.input.setInputProcessor(stage);

        playButton = new btnPlay(); //make actor  

        stage.addActor(playButton);     
    }       

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //make background
        batch.begin();          
        batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960);
        batch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!!

        //batch.begin();
        //playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR
        //batch.end();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

     @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }   

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

btnPlay.java

package com.spiderhunt.buttons;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.spiderhunt.Assets;


public class btnPlay extends Actor {

    public btnPlay(){
        setSize(100, 40);
        setPosition(100, 100);
    }

    public void draw(SpriteBatch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
    }
}

资产.java

package com.spiderhunt;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Assets {
    public static Texture atlas;    
//backgrounds
    public static TextureRegion bgMenuRegion;
    public static TextureRegion bgSelectLevelRegion;
//buttons
    public static TextureRegion btnPlayRegion;
//objects
    public static TextureRegion objFlyRegion;

    public static void load(){
        atlas = new Texture("atlas.png");

        bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960);
        btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112);
    }
}

【问题讨论】:

  • “未调用”,因为您的代码未执行或没有可见的内容?你有没有试过在你的按钮的draw call周围batch.begin/end
  • 并且您已经用空方法覆盖了 Game 类中的所有方法。为了更短的代码?否则什么都不会渲染。

标签: java android libgdx actor stage


【解决方案1】:

感谢您的帮助。

现在我找到了我的错误。

我将 SpriteBatch 替换为 Batch 并且它可以工作。

改变

public void draw(SpriteBatch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
}

@Override
public void draw(Batch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
}

【讨论】:

  • 不错。我记得在升级旧项目的 Libgdx 库时遇到过这样的问题
  • 这是 @Override 关键字可以提供帮助的地方。将它放在您要覆盖的任何方法之前,当您没有完全正确时,它会警告您。
【解决方案2】:

我认为问题出在这一行:

new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);

Remove batch from Stage Initialization 把它改成这样:

new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));

您正在将批处理从屏幕传递到舞台,并且在 stage.draw() 之前调用 batch.end()。但是舞台有自己的批次,因此您不必通过批次来绘制舞台。如果出于您自己的原因(例如批量投影矩阵配置),您仍然希望将屏幕批次传递到舞台,请不要在 stage.draw() 之前调用 batch.end(),因为您的舞台批次和主批次是相同的。在 stage.draw() 之后调用 batch.end()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多