使用吐温可以缩放吗?-LibGdx

使用吐温可以缩放吗?-LibGdx,libgdx,tween,Libgdx,Tween,有没有办法用吐温来衡量一个演员 我有一个这样的演员形象 image1 = new Image(texture1); stage.addActor(image1); 想让它缩放一段时间,并恢复到原来的大小 用吐温可以吗?是的,用吐温可以 根据Aurelian Ribon的博客: tween引擎的唯一限制是你的想象力 离题 如果您使用的是scene2d框架,那么为什么要使用LibGDX的通用tween引擎 具有用于粗花呢的内置API 编辑 public class ActorAccessor i

有没有办法用吐温来衡量一个演员

我有一个这样的演员形象

image1 = new Image(texture1);
stage.addActor(image1);
想让它缩放一段时间,并恢复到原来的大小

用吐温可以吗?

是的,用吐温可以

根据Aurelian Ribon的博客: tween引擎的唯一限制是你的想象力


离题

如果您使用的是scene2d框架,那么为什么要使用LibGDX的
通用tween引擎
具有用于粗花呢的内置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;
        }
    }
}
render()
方法中

tweenManager.update(Gdx.graphics.getDeltaTime());
是的,这是可能的

根据Aurelian Ribon的博客: tween引擎的唯一限制是你的想象力


离题

如果您使用的是scene2d框架,那么为什么要使用LibGDX的
通用tween引擎
具有用于粗花呢的内置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;
        }
    }
}
render()
方法中

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

在libgdx中用tween缩放演员

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,操作方法:

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

}

在libgdx中用tween缩放演员

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,操作方法:

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

}

我知道这不是你所要求的,但我可能会继续告诉你行动是多么容易,也许你会重新考虑使用吐温

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

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

工作示例:

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

我知道这不是你所要求的,但我可能会继续告诉你行动是多么容易,也许你会重新考虑使用吐温

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

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

工作示例:

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

我不知道tween的情况,但为什么不使用
操作
?如果您使用的是Scene2D,那么就没有理由使用UniversalWeeneEngine。Scene2D本质上已经是一个使用动作的tween引擎,而且更容易使用(因为它不是“通用的”)。我不知道tween,但为什么不使用
动作
?如果使用Scene2D,就没有理由使用UniversalTweeneEngine。Scene2D本质上已经是一个使用动作的tween引擎,而且更易于使用(因为它不是“通用的”)。如果您提供一些参考代码,这将非常有帮助。我搜索了很多以使此效果起作用,但找不到任何有用的东西。我从未将Action API用于tweening。我已经有了一个类似的访问器类。但是如何将tween写入缩放?我尝试使用TimeLine,但缩放仅从一个角发生。如果您提供一些参考,这将非常有用代码。我搜索了很多使这个效果工作,但没有得到任何有用的东西。我从未在tweening中使用过Action API。我已经有了一个类似的访问器类。但是如何编写tween来缩放?我尝试了TimeLine,但缩放只从一个角发生。我尝试了您建议的第一个答案。但它对图像没有影响。这会很有帮助如果在答案中添加任何工作示例代码@centenond@Niranjana是否在
update()
方法中调用
stage.act()
?是的,update()中有.stage.act(delta)@centenond@Niranjana我不明白为什么它不起作用,但是我写了一个工作示例,我现在将编辑我的答案。我尝试了您建议的第一个答案。但是它对图像没有影响。如果您在答案中添加任何工作示例代码,这将非常有用@centenond@Niranjana您是否在
update()
方法中调用
stage.act()
?是的,update()中有.stage.act(delta).@centenond@Niranjana我不明白为什么它不起作用,但我写了一个有效的例子,我现在将编辑我的答案