【问题标题】:Is scaling possible with tween ?-LibGdx补间是否可以缩放?-LibGdx
【发布时间】:2017-11-03 09:36:44
【问题描述】:

有没有办法用补间来缩放演员?

我有这样的演员形象。

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

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

补间可以吗?

【问题讨论】:

  • 我不知道补间,但为什么不使用Action
  • 如果您使用的是 Scene2D,则没有理由使用 UniversalTweenEngine。 Scene2D 本质上已经是一个使用动作的补间引擎,并且更容易使用(因为它不是“通用的”)。

标签: libgdx tween


【解决方案1】:

我知道这不是您要问的,但我可能会继续向您展示使用 Actions 是多么容易,也许您会重新考虑使用补间

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

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

    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();
    }
}

【讨论】:

  • 我尝试了你建议的第一个答案。但它对图像没有影响。如果你在答案中添加任何工作示例代码会很有帮助。@centenond
  • @Niranjana 你在update () 方法中调用stage.act () 吗?
  • yes.stage.act (delta) 在 update().@centenond
  • @Niranjana 我不明白为什么它不起作用,但我写了一个工作示例我现在将编辑我的答案
【解决方案2】:

在 libgdx 中使用补间缩放演员。

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;
    }

}

【讨论】:

    【解决方案3】:

    是的,tween engine 是可能的。

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


    题外话

    如果您使用的是 Scene2d 框架,为什么要使用 LibGDX universal-tween-engine 具有用于补间的内置 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());
    

    【讨论】:

    • 如果你提供一些参考代码会很有帮助。我搜索了很多以使这个效果起作用,但没有得到任何有用的东西。我从未使用过带有补间的 Action API。
    • 我已经有一个类似的访问器类。但是如何编写补间来缩放?​​我尝试使用 TimeLine,但缩放只发生在一个角落。
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多