Libgdx 之Actions 动作类

Libgdx提供了一下Action类来实现简单动画。动作类主要分为2类,一个是控制类,一个是表现类。Libgdx提供了Actions类来方便管理各个动作类,可以直接通过Actions调用

控制类,主要控制表现类来执行

  1. SequenceAction sequence (Action… actions)
    将添加的Actions按添加的顺序依次执行
  2. ParallelAction parallel (Action… actions)
    将添加的Actions同时执行
  3. RepeatAction repeat (int count, Action repeatedAction)
    将添加的repeatedAction执行count次
  4. RepeatAction forever (Action repeatedAction)
    将添加的repeatedAction无限次重复执行
  5. DelayAction delay (float duration)
    将某个Action延迟duration秒后执行
    DelayAction delay (float duration, Action delayedAction)
    设定dealyedAction duration秒后执行

表现类


  1. 移动动作类
    MoveToAction moveTo (float x, float y, float duration, Interpolation interpolation)
    将Actor经过duration秒后移动到屏幕(x, y)处, interpolation是插值算法,后面会具体说一下
    MoveByAction moveBy (float amountX, float amountY, float duration, Interpolation interpolation)
    将Actor经过duration后在X轴移动距离amountX, 在Y轴移动距离amountY

注意:在Libgdx中toAciton是移动的坐标,byActon是移动的距离,后面还有还有很多类似的,就不在一一介绍了。

  • SizeToAction sizeTo (float x, float y, float duration, Interpolation interpolation)
    将Actor经过duration后在X轴大小缩放至X,在y轴大小缩放至y
    SizeByAction sizeBy (float amountX, float amountY, float duration, Interpolation interpolation)
    将Actor经过duration后再X轴减少amountX,在Y轴减少amountY

  • ScaleToAction scaleTo (float x, float y, float duration, Interpolation interpolation)

  • RotateToAction rotateTo (float rotation, float duration, Interpolation interpolation)
    相对于原来旋转90,只旋转一次,如经过别的方法已经旋转90那么也不再旋转
    RotateByAction rotateBy (float rotationAmount, float duration, Interpolation interpolation)
    相对于原来位置每次都旋转90

  • ColorAction color (Color color, float duration, Interpolation interpolation
    将Actor转变为指定颜色
  • AlphaAction alpha (float a, float duration, Interpolation interpolation)
    在指定时间将alpha转换为a
    AlphaAction fadeOut (float duration, Interpolation interpolation)
    在指定时间将alpha转换为0
    AlphaAction fadeIn (float duration, Interpolation interpolation)
    在指定时间将alpha转换为1
    此外还有一些其他Actions 比如: show(), hide(), visible(), run()
  • Interpolation 插值算法

    主要作用是将一组线性值,转换为非线性值。利用Interpolation可以做些简单的动画,可以查看后面代码
    这里写图片描述
    测试代码

        Stage stage;
        Image img;
        Texture texture;
    
        int width, height;
        float orginXX, orginYY;
    
        @Override
        public void create() {
            stage = new Stage();
    
            width = 128;
            height = 128;
            orginXX = stage.getWidth() / 2 - width / 2;
            orginYY = stage.getHeight() / 2 - height / 2;
    
            texture = new Texture("badlogic.jpg"); 
            img = new Image(texture);
            initActor();
            img.setOrigin(width / 2,  height / 2);
            stage.addActor(img);
    
            InputProcessorA a = new InputProcessorA();
            Gdx.input.setInputProcessor(a);
        }
    
        @Override
        public void render() {
            Gdx.gl.glClearColor(0.39f, 0.58f, 0.92f, 1.0f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            stage.act();
            stage.draw();
        }
    
        @Override
        public void dispose() {
            stage.dispose();
            texture.dispose();
        }
    
        private void initActor() {
            img.setSize(width, height);
            img.setPosition(orginXX, orginYY);
        }
    
        private class InputProcessorA extends InputAdapter {
    
            @Override
            public boolean keyTyped(char character) {
                if (character == 'r') {
                    initActor();
                } else if (character == 'a') {
                    // 有一个不带时间的方法,代表立即执行,加上Interpolation方法可以看到先移动屏幕外,再移动回来
                    img.addAction(Actions.moveTo(0, 0, 1, Interpolation.swingOut));
                } else if (character == 'b') {
                    img.addAction(Actions.moveBy(60, 60, 1, Interpolation.swingIn));
                } else if (character == 'c') {
                    img.addAction(Actions.sizeTo(-40, -40, 1, Interpolation.circleOut));
                } else if (character == 'd') {
                    img.addAction(Actions.sizeBy(-40, -40, 1, Interpolation.circleIn));
                } else if (character == 'e') {
                    img.addAction(Actions.scaleTo(2, 2, 1, Interpolation.sineIn));
                } else if (character == 'f') {
                    img.addAction(Actions.scaleBy(2, 2, 1, Interpolation.sineOut));
                } else if (character == 'g') {
                    img.addAction(Actions.rotateTo(90, 1, Interpolation.bounceIn));
                } else if (character == 'h') {
                    img.addAction(Actions.rotateBy(90, 1, Interpolation.bounceOut));
                } else if (character == 'i') {
                    img.addAction(Actions.color(Color.RED, 2, Interpolation.elasticOut));
                } else if (character == 'j') {
                    img.addAction(Actions.alpha(0, 2, Interpolation.elasticIn));
                }
                return true;
            }
    
        }
    
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值