繁体   English   中英

Libgdx对图像的操作适用于整个演员

[英]Libgdx actions to image is applied to whole actors

我正在尝试使图像淡出然后淡入并且可以工作,但是背景图像也淡出然后淡入。另外,当我应用旋转动作(已注释)时,尽管我放了它,但它只旋转图像一次在事件Touchup上,淡入和淡出应用于每次触摸,而淡入应用于图像和背景:在这里我在此类中应用动作:

   public class MainButtons {
    public Viewport viewport;
    public Stage stage;
    public boolean centerPressed;

    public Image fire;
    public Image center;
    public Sound flap;

    public OrthographicCamera camera;
    public static Table table;


    //Constructor.
    public MainButtons(SpriteBatch spriteBatch) {
        camera = new OrthographicCamera();
        viewport = new StretchViewport(KidTele.V_WIDTH,KidTele.V_HIEGH,camera);
        stage = new Stage(viewport, spriteBatch);
        //InputMultiplexer this is why only input handling  from controller
        flap= Gdx.audio.newSound(Gdx.files.internal("one.mp3"));
        Gdx.input.setInputProcessor(stage);

        fire= new Image(new Texture("cars/fire.png"));

        //buttonone.setSize(10, 5);


        menuTable();
        defineCenter();

    }
    public void defineCenter()
    {
        center=new Image(new Texture(Gdx.files.internal("0.png")));

        center.setBounds(viewport.getWorldWidth() / 5f, viewport.getWorldHeight() / 3f, viewport.getWorldWidth() / 1.5f, viewport.getWorldHeight() / 3f);
        center.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                centerPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                //center.setOrigin(center.getWidth()/2,center.getHeight()/2);
                //center.addAction(Actions.sequence(Actions.rotateTo(360, 2), Actions.fadeIn(1)));
                center.addAction(Actions.sequence(Actions.fadeOut(1), Actions.fadeIn(1)));
                centerPressed = false;
            }
        });
        //center.setVisible(false);
        stage.addActor(center);
    }
    public void draw() {
        stage.draw();
    }
    public void resize(int width, int height) {
        viewport.update(width, height);
    }
}

这是屏幕的代码:

public class PlayScreen implements Screen {
    private KidTele game;
    private OrthographicCamera gamecam;
    private Viewport gameport;
    private World world;
    private Box2DDebugRenderer b2dr;
    private MainButtons mainButtons;
    private Texture background;
    private Sound flap;
    public PlayScreen(KidTele game)
    {
        this.game=game;
        gamecam=new OrthographicCamera();
        gameport=new StretchViewport(KidTele.V_WIDTH,KidTele.V_HIEGH,gamecam);
        //stage=new Stage(gameport,((KidTele) game).batch);
        background=new Texture("background2.png");
        gamecam.position.set(gameport.getWorldWidth()/2f,gameport.getWorldHeight()/2f,0);
        b2dr=new Box2DDebugRenderer();
        world=new World(new Vector2(0,0.8f),true);
        flap= Gdx.audio.newSound(Gdx.files.internal("one.mp3"));
        mainButtons=new MainButtons(game.batch);
    }
    @Override
    public void show() {

    }
    public void handleinput(float dt)
    {

        if(Gdx.input.isKeyJustPressed(Input.Keys.BACK)) {
            mainButtons.stopMusic();
            game.setScreen(new PlayScreen(game));
        }

    }
    public void update(float dt)
    {
        handleinput(dt);
        world.step(1 /60f, 6, 2);
        //player.update(dt);

        gamecam.update();
    }

    @Override
    public void render(float delta) {

        update(delta);
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

         b2dr.render(world, gamecam.combined);
        game.batch.setProjectionMatrix(gamecam.combined);

        game.batch.begin();

        game.batch.draw(background, 0, 0, gameport.getWorldWidth(), gameport.getWorldHeight());
        game.batch.end();
        mainButtons.stage.act();
        mainButtons.stage.draw();
        mainButtons.draw();
    }

    @Override
    public void resize(int width, int height) {
        gameport.update(width, height);
        mainButtons.resize(width, height);
    }

调用stage.draw() ,可以将SpriteBatch设置为任意颜色。 在您的情况下,它会设置为使某个演员褪色的部分Alpha。 要解决此问题,请确保在绘制背景之前立即调用batch.setColor(1, 1, 1, 1) ,以便可以保证使用哪种颜色绘制背景。

图像仅旋转一次,因为您正在使用rotateTo并且每次都赋予它相同的值。 一旦旋转到360度,每次您再次调用rotateTo时,它将已经处于360度,因此什么也没有发生。 在您的情况下,应改为使用rotateBy

顺便说一句,当您执行此操作时,您将绘制两次舞台:

    mainButtons.stage.draw();
    mainButtons.draw();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM