libgdx学习记录6——动作Action

摘要:
libgdx中的Action类能够有效的帮助我们实现位移、旋转、缩放、淡入淡出等效果,对游戏的设计很有用。Action是一个抽象类,本身不可以实例化。一般使用的它的继承类,常用的有MoveToAction、MoveByAction、RotateToAction、RotateByAction、ScaleToAction、ScaleByAction、FadeInAction、FadeOutAction等。如果要定义自己的Acion则需要重写其抽象方法act。

libgdx中的Action类能够有效的帮助我们实现位移、旋转、缩放、淡入淡出等效果,对游戏的设计很有用。

Action是一个抽象类,本身不可以实例化。一般使用的它的继承类,常用的有

MoveToAction、MoveByAction、RotateToAction、RotateByAction、ScaleToAction、ScaleByAction、FadeInAction、FadeOutAction等。

如果要定义自己的Acion则需要重写其抽象方法act。

例如:

Action action = new Action(){

@Override

public void act{

stage.getroot().removeActor( this.getActor() );

}

}

实例:

1 package com.fxb.newtest;
2 
3 import com.badlogic.gdx.ApplicationAdapter;
4 import com.badlogic.gdx.Gdx;
5 import com.badlogic.gdx.graphics.GL10;
6 import com.badlogic.gdx.graphics.Texture;
7 import com.badlogic.gdx.scenes.scene2d.Action;
8 import com.badlogic.gdx.scenes.scene2d.InputEvent;
9 import com.badlogic.gdx.scenes.scene2d.InputListener;
10 import com.badlogic.gdx.scenes.scene2d.Stage;
11 import com.badlogic.gdx.scenes.scene2d.actions.Actions;
12 import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
13 import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
14 import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
15 import com.badlogic.gdx.scenes.scene2d.actions.RotateByAction;
16 import com.badlogic.gdx.scenes.scene2d.actions.RotateToAction;
17 import com.badlogic.gdx.scenes.scene2d.actions.ScaleByAction;
18 import com.badlogic.gdx.scenes.scene2d.actions.ScaleToAction;
19 import com.badlogic.gdx.scenes.scene2d.ui.Image;
20 
21 public classLib005_Action extends ApplicationAdapter{
22 
23 Stage stage;
24 Image img;
25 Texture texture;
26 Action totalAction;
27 Action exitAction;
28     
29 @Override
30     public voidcreate() {
31         //TODO Auto-generated method stub
32         stage = newStage();
33         texture = new Texture( Gdx.files.internal( "data/pal4_1.jpg") );
34         img = newImage( texture );
35 img.setSize( texture.getWidth(), texture.getHeight() );
36         //img.setPosition( stage.getWidth()/2-img.getWidth()/2, stage.getHeight()/2-img.getHeight()/2 );
37         img.setOrigin( img.getWidth()/2, img.getHeight()/2);
38 stage.addActor( img );
39         
40         MoveByAction action1 = Actions.moveBy( stage.getWidth()/2-img.getWidth()/2, stage.getHeight()/2-img.getHeight()/2, 2);
41         ScaleByAction action2 = Actions.scaleBy( 2, 2, 2);
42         RotateByAction action3 = Actions.rotateBy( -360, 2);
43         
44         RotateToAction action4 = Actions.rotateTo( 0, 2);
45         ScaleToAction action5 = Actions.scaleTo( 1, 1, 2);
46         MoveToAction action6 = Actions.moveTo( 0, 0, 2);        
47         
48         totalAction =Actions.forever( Actions.sequence( Actions.sequence(action1, action2, action3), Actions.sequence(action4, action5, action6) ) );
49 img.addAction( totalAction );
50         
51         
52         AlphaAction action7 = Actions.fadeOut( 1);
53         AlphaAction action8 = Actions.fadeIn( 1);
54         Action action9 = newAction(){
55 @Override
56             public boolean act(floatdelta) {
57                 //TODO Auto-generated method stub
58                 stage.getRoot().removeActor( this.getActor() );
59                 return false;
60 }            
61 };
62         exitAction =Actions.sequence( action7, action8, action9 );
63         
64         img.addListener(newInputListener(){
65 @Override
66             public boolean touchDown(InputEvent event, float x, float y, int pointer, intbutton) {
67                 //TODO Auto-generated method stub
68 img.clearActions();
69 img.addAction( exitAction );
70                 return true;
71 }        
72 });
73         
74         
75 Gdx.input.setInputProcessor( stage );
76 }
77 
78 @Override
79     public voidrender() {
80         //TODO Auto-generated method stub
81         Gdx.gl.glClearColor( 1, 1, 1, 1);
82 Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
83         
84 stage.act();
85 stage.draw();        
86 }
87 
88 @Override
89     public voiddispose() {
90         //TODO Auto-generated method stub
91         
92 super.dispose();
93 }
94 
95 }

action一般是添加到Actor中的,当然也可以添加到stage中,使用起来很方便。程序中设计的是一个循环,当点击图片时会清除以前的Action并进行一次淡出淡入后消失。

运行效果:

libgdx学习记录6——动作Action第1张

免责声明:文章转载自《libgdx学习记录6——动作Action》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spring 4 @RestController 小试艾略特波浪理论下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android与Libgdx环境配置

此处所说的是基于windows和android版本的libgdx环境配置。 1. 下载所需软件 JDK 1.7。 下载地址: window x86版本地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html Android SDK。 在a...

Android与Libgdx入门实例

本文讲解如何实现Android与Libgdx各自的Hello World过程。 1. Android版Hello World 点击Eclipse快捷方式,选择New Android Application,创建project,填写相应名称。 2. 然后一路next,选择默认就ok了。 3. 创建的程序文件模型。 3. 修改界面显示字符串。 4. 选择...

Jenkins和Gitlab CI/CD自动更新k8s中pod使用的镜像说明

Jenkins 使用Jenkins的话,完成的工作主要有如下步骤:1.从Gogs或Gitlab仓库上拉取代码2.使用Maven编译代码,打包成jar文件3.根据jar文件使用相对应的Dockerfile文件制作成Docker镜像4.把Docker镜像推送到Nexus上的Docker仓库(或者Harbor仓库)5.运行shell脚本,给k8s的master主...

大数据--Spark原理

Apache Spark是一个围绕速度、易用性和复杂分析构建的大数据处理框架,最初在2009年由加州大学伯克利分校的AMPLab开发,并于2010年成为Apache的开源项目之一,与Hadoop和Storm等其他大数据和MapReduce技术相比,Spark有如下优势: 1.运行速度快,Spark拥有DAG执行引擎,支持在内存中对数据进行迭代计算。官方提供...

spark作业性能调优

spark作业性能调优 优化的目标 保证大数据量下任务运行成功 降低资源消耗 提高计算性能 一、开发调优: (1)避免创建重复的RDD RDD lineage,也就是“RDD的血缘关系链” 开发RDD lineage极其冗长的Spark作业时,创建多个代表相同数据的RDD,进而增加了作业的性能开销。 (2)尽可能复用同一个RDD 比如说,有一个RDD的...

spark 作业调度

一、调度分类 调度分为两种,一是应用之间的,二是应用内部作业的。 (一)应用之间 我们前面几章有说过,一个spark-submit提交的是一个应用,不同的应用之间是有调度的,这个就由资源分配者来调度。如果我们使用Yarn,那么就由Yarn来调度。调度方式的配置就在$HADOOP_HOME/etc/hadoop/yarn-site.xml中 [html...