LINUX.ORG.RU

LibGDX не рисует Actor-ов на Scene

 ,


0

2

Други, все обгуглил, обчитал. Делаю по примеру из книги (да и в их вики похоже) - не работает.

Сцена, создаю актора, содержащего спрайт, добавляю его на сцену, запускаю - не видно актора! Рассудите, где я что-то упустил?

public class DropTheBrickGame extends Game {

	private Random random;
	public Random getRandom() {
		return random;
	}

    private OrthographicCamera camera;

	private SpriteBatch batch;

	private GameScreen gameScreen;
	
	@Override
	public void create () {
		random = new Random();

		batch = new SpriteBatch();
		gameScreen = new GameScreen(this);

		setScreen(gameScreen);
	}

	@Override
	public void render () {
		super.render();
//		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 0.5f);
//		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//        batch.setProjectionMatrix(camera.combined);
//        batch.begin();
//
//        gameScreen.render(Gdx.graphics.getDeltaTime());
//
//        batch.end();
	}
	
	@Override
	public void dispose () {
	    super.dispose();
		batch.dispose();

        Gdx.app.exit();
	}
}
public class GameScreen implements Screen {

    private static final float C_WIDTH = 640;
    private static final float C_HEIGHT = 800;

    private static final float MIN_FLOOR_SIZE = 200;
    private static final float MAX_FLOOR_SIZE = 500;

    private Stage stage;

    private DropTheBrickGame game;

    private Brick brick;

    SpriteBatch batch;

    Texture test = AssetsFactory.createTexture(AssetsFactory.A_S_BRICK);

    public GameScreen(DropTheBrickGame game) {
        this.game = game;

        batch = new SpriteBatch();
    }

    @Override
    public void show() {
        stage = new Stage(new ScreenViewport());

        brick = new Brick();
        brick.setPosition(10,10);
        brick.setVisible(true);
        stage.addActor(brick);

    }

    @Override
    public void render(float delta) {
        batch.begin();
        stage.act(delta);

        stage.getRoot().draw(batch, 1);

        Gdx.gl.glClearColor(0.8f, 0.8f, 0.8f, 0.2f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();

        batch.draw(test, 50,50);

        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
//        brick.dispose();
    }
}

public class Brick extends SpritedActor {

    public Brick() {
        super(AssetsFactory.createTexture(AssetsFactory.A_S_BRICK));
        this.textureRegion.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    }

}
abstract public class SpritedActor extends Actor implements Updatable, Disposable {

    protected TextureRegion textureRegion;
    protected Rectangle boundary;
    protected Vector2 velocity;

    public SpritedActor() {
    }

    public SpritedActor(Texture texture) {
        textureRegion = new TextureRegion(texture);
        boundary = new Rectangle();
        velocity = new Vector2();
    }

    public TextureRegion getTextureRegion() {
        return textureRegion;
    }

    public void setTextureRegion(TextureRegion textureRegion) {
        this.textureRegion = textureRegion;
    }

    public Rectangle getBoundingRectangle() {
        boundary.set(getX(), getY(), getWidth(), getHeight());
        return boundary;
    }

    @Override
    public void act(float dt) {
        super.act( dt );
        update(dt);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color c = getColor();
        batch.setColor(c.r, c.g, c.b, c.a);
        if (isVisible()) {
            batch.draw(textureRegion, getX(), getY(), getOriginX(), getOriginY(),
                    getWidth(), getHeight(), getScaleX(),
                    getScaleY(), getRotation());
        }

        //super.draw(batch, parentAlpha);
        //batch.draw(texture, (parent != null ? parent.getX() : 0) + getX(), (parent != null ? parent.getY() : 0) + getY(), getWidth(), getHeight());
    }

    @Override
    public void dispose() {
        if (textureRegion != null) {
            textureRegion.getTexture().dispose();
        }
    }

    @Override
    public void update(float dt) {}
}
★★★★★