是否可以使用补间?-LibGdx进行缩放

时间:2017-06-02 04:55:01

标签: libgdx tween

有没有办法用补间来缩放actor?

我有这样的演员形象。

image1 = new Image(texture1);
stage.addActor(image1);

想让它缩放一段时间并恢复到原始尺寸。

是否可以使用补间?

3 个答案:

答案 0 :(得分:2)

我知道这不是你要求的,但我可以继续向你展示使用动作是多么容易,也许你重新考虑使用补间

    image1 = new Image(texture1);
    stage.addActor(image1);

    image1.addAction(
       Actions.sequence(
          Actions.sizeTo(scaledWidth, scaledHeight, durationInSecs),
          Actions.sizeTo(originalWidth, originalHeight, durationInSecs)
       )
    );

OR

    image1 = new Image(texture1);
    stage.addActor(image1);

    image1.setOrigin(Align.center); 

    image1.addAction(
       Actions.sequence(
          Actions.scaleTo(2,2,durationInSecs), //Scale to 200% here
          Actions.scaleTo(1,1,durationInSecs) //Scale to 100% (original size)
       )
    );

工作示例:

public class MainClass extends ApplicationAdapter {
    private Texture tex;
    private Image image;
    private Stage   stage;

    @Override
    public void create () {
        stage   = new Stage();
        tex     = new Texture("badlogic.jpg");
        image   = new Image(tex);

        image.setSize(100,100);
        image.setPosition(0,0);

        float durationInSecs = 1;
        image.addAction(
                Actions.sequence(
                        Actions.sizeTo(200,200,durationInSecs), 
                        Actions.sizeTo(100,100,durationInSecs)
                )
        );
        stage.addActor(image);

    }
    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }

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

答案 1 :(得分:1)

是可以使用tween engine

根据Aurelien Ribon的博客: 补间引擎的唯一限制是你的想象力。

OFF TOPIC

为什么universal-tween-engine使用LibGDX,如果您正在使用scene2d框架 内置了Action API用于补间。

修改

public class ActorAccessor implements TweenAccessor<Actor> {

    public static final int POS_XY = 1;
    public static final int SCALE_XY = 2;

    @Override
    public int getValues(Actor target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case POS_XY:
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                return 2;

            case SCALE_XY:
                returnValues[0] = target.getScaleX();
                returnValues[1] = target.getScaleY();
                return 2;

            default: assert false; return -1;
        }
    }

    @Override
    public void setValues(Actor target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case POS_XY: target.setPosition(newValues[0], newValues[1]); break;
            case SCALE_XY: target.setScale(newValues[0], newValues[1]); break;
            default: assert false;
        }
    }
}

通过您的Actor的注册和补间比例值将此ActorAccessorActor绑定。

编辑2

TweenManager tweenManager=new TweenManager();

Tween.registerAccessor(Actor.class,new ActorAccessor());

Image image=new Image(new Texture("badlogic.jpg"));
image.setOrigin(image.getWidth()/2,image.getHeight()/2);   // required for scaling from center
image.setPosition(200,100);
stage.addActor(image);

float scale_up_dur=0.4f;
float scale_factor=2 ;            // 200%
float pause_after_scale=3;
float scale_down_dur= 0.4f;

Timeline.createSequence().delay(2f).push(Tween.to(image, ActorAccessor.SCALE_XY,scale_up_dur).target(scale_factor, scale_factor)).pushPause(pause_after_scale).push(Tween.to(image, ActorAccessor.SCALE_XY, scale_down_dur).target(1, 1)).start(tweenManager);

render()方法

tweenManager.update(Gdx.graphics.getDeltaTime());

答案 2 :(得分:1)

在libgdx中使用补间扩展actor。

package com.vector.science11;
import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;

public class Test3Dline extends ApplicationAdapter {
    private SpriteBatch batch;
    private OrthographicCamera orthoCamera;

    private TweenManager tweenManager;
    public Stage stage;

    private Color bgColor;
    SpriteDrawable spriteDrawable;

    @Override
    public void create() {

        orthoCamera = new OrthographicCamera(960, 540);
        orthoCamera.position.set(960 / 2, 540 / 2, 0);
        orthoCamera.update();

        bgColor = Color.valueOf("ffffff");

        tweenManager = new TweenManager();

        stage = new Stage();
        stage.getViewport().setCamera(orthoCamera);
        batch = new SpriteBatch();

        Tween.registerAccessor(Actor.class, new ActorAccessors());

        Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
        image1.setPosition(100, 100);
        stage.addActor(image1);
        Gdx.input.setInputProcessor(stage);

        Timeline.createSequence()
                .delay(2f)
                .push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(2,
                        2))
                .pushPause(3f)
                .push(Tween.to(image1, ActorAccessors.SCALE_XY, 0.4f).target(1,
                        1)).start(tweenManager);
    }

    @Override
    public void render() {

        tweenManager.update(Gdx.graphics.getDeltaTime());
        Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(orthoCamera.combined);

        stage.draw();
        stage.act(Gdx.graphics.getDeltaTime());

        // Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
    }

    public static Texture createTexture(int width, int height, Color col,
            float alfa) {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
        Color color = col;
        pixmap.setColor(color.r, color.g, color.b, alfa);
        pixmap.fillRectangle(0, 0, width, height);

        Texture pixmaptexture = new Texture(pixmap);
        return pixmaptexture;
    }

}

使用Scene2D API,Action方法:

   package com.vector.science11;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;

public class Test3Dline extends ApplicationAdapter {
    private SpriteBatch batch;
    private OrthographicCamera orthoCamera;
    public Stage stage;
    private Color bgColor;
    SpriteDrawable spriteDrawable;

    @Override
    public void create() {

        orthoCamera = new OrthographicCamera(960, 540);
        orthoCamera.position.set(960 / 2, 540 / 2, 0);
        orthoCamera.update();

        bgColor = Color.valueOf("ffffff");
        stage = new Stage();
        stage.getViewport().setCamera(orthoCamera);
        batch = new SpriteBatch();
        Image image1 = new Image(createTexture(50, 30, Color.RED, 1));
        image1.setPosition(100, 100);
        stage.addActor(image1);

        image1.addAction(Actions.sequence(Actions.scaleTo(2, 2, 2), // scale to
                                                                    // 2 times
                                                                    // of actual
                                                                    // size
                Actions.scaleTo(1, 1, 2) // come to original size
                ));
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(orthoCamera.combined);
        stage.draw();
        stage.act(Gdx.graphics.getDeltaTime());
    }

    public static Texture createTexture(int width, int height, Color col,
            float alfa) {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
        Color color = col;
        pixmap.setColor(color.r, color.g, color.b, alfa);
        pixmap.fillRectangle(0, 0, width, height);
        Texture pixmaptexture = new Texture(pixmap);
        return pixmaptexture;
    }

}