Libgdx 之Actions 动作类

Posted zqiang_55

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
            
    
        
    

以上是关于Libgdx 之Actions 动作类的主要内容,如果未能解决你的问题,请参考以下文章

Libgdx之Table 表格排版

Libgdx之Table 表格排版

为啥 Libgdx 的 Table 不接受缩放动作?

abbix 配置动作(Actions)

zabbix专题:第六章 动作Actions告警方式Medias

ROS2学习笔记7--认识ros2动作actions