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

  }

}

实例:

 package com.fxb.newtest;

 import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.RotateByAction;
import com.badlogic.gdx.scenes.scene2d.actions.RotateToAction;
import com.badlogic.gdx.scenes.scene2d.actions.ScaleByAction;
import com.badlogic.gdx.scenes.scene2d.actions.ScaleToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image; public class Lib005_Action extends ApplicationAdapter{ Stage stage;
Image img;
Texture texture;
Action totalAction;
Action exitAction; @Override
public void create() {
// TODO Auto-generated method stub
stage = new Stage();
texture = new Texture( Gdx.files.internal( "data/pal4_1.jpg" ) );
img = new Image( texture );
img.setSize( texture.getWidth(), texture.getHeight() );
//img.setPosition( stage.getWidth()/2-img.getWidth()/2, stage.getHeight()/2-img.getHeight()/2 );
img.setOrigin( img.getWidth()/, img.getHeight()/ );
stage.addActor( img ); MoveByAction action1 = Actions.moveBy( stage.getWidth()/-img.getWidth()/, stage.getHeight()/-img.getHeight()/, );
ScaleByAction action2 = Actions.scaleBy( , , );
RotateByAction action3 = Actions.rotateBy( -, ); RotateToAction action4 = Actions.rotateTo( , );
ScaleToAction action5 = Actions.scaleTo( , , );
MoveToAction action6 = Actions.moveTo( , , ); totalAction = Actions.forever( Actions.sequence( Actions.sequence(action1, action2, action3), Actions.sequence(action4, action5, action6) ) );
img.addAction( totalAction ); AlphaAction action7 = Actions.fadeOut( );
AlphaAction action8 = Actions.fadeIn( );
Action action9 = new Action(){
@Override
public boolean act(float delta) {
// TODO Auto-generated method stub
stage.getRoot().removeActor( this.getActor() );
return false;
}
};
exitAction = Actions.sequence( action7, action8, action9 ); img.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
img.clearActions();
img.addAction( exitAction );
return true;
}
}); Gdx.input.setInputProcessor( stage );
} @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( , , , );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw();
} @Override
public void dispose() {
// TODO Auto-generated method stub super.dispose();
} }

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

运行效果:

libgdx学习记录6——动作Action的更多相关文章

  1. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  2. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  3. libgdx学习记录26——Polygon多边形碰撞检测

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  4. libgdx学习记录23——图片移动选择

    模拟移动选择图片,采用相机实现. package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter; import com.bad ...

  5. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

  6. libgdx学习记录20——多线程MultiThread资源处理

    在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...

  7. libgdx学习记录19——图片动态打包PixmapPacker

    libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...

  8. libgdx学习记录18——Box2d物理引擎

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

  9. libgdx学习记录17——照相机Camera

    照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...

随机推荐

  1. Azure 元数据服务:适用于 Windows VM 的计划事件(预览)

    计划事件是 Azure 元数据服务中的其中一个子服务. 它负责显示有关即将发生的事件(例如,重新启动)的信息,使应用程序可以为其做准备并限制中断. 它可用于所有 Azure 虚拟机类型(包括 PaaS ...

  2. 索引,B+ tree,动态hash表

    数据库课索引部分的学习笔记. 教材: Database System: The Complete Book, Chapter 15 Database System Implementation, Ch ...

  3. round()和trunc()用法

    round(数字 | 列 保留小数的位数):四舍五入. select a.*,round(s),round(-s) from bqh4 a trunc(数字 | 列 保留小数的位数):舍弃指定位置的内 ...

  4. 牢记 31 种 CSS 选择器用法

    原文 The 30 CSS Selectors you Must Memorize 由 Jeffrey Way 发表于 2011 年 6 月,介绍了 30 种最常用的 CSS 选择器用法,多加了一种, ...

  5. DAU、UV、独立IP、PV的区别和联系

    基本概念 DAU(Daily Active User)日活跃用户数量.常用于反映网站.互联网应用或网络游戏的运营情况.DAU通常统计一日(统计日)之内,登录或使用了某个产品的用户数(去除重复登录的用户 ...

  6. ab参数详解 – 压力测试

    命令参数:    -n requests     Number of requests to perform    //在测试会话中所执行的请求个数.默认时,仅执行一个请求    -c concurr ...

  7. CSS样式定义的优先级顺序总结

    CSS样式定义的优先级顺序总结 层叠优先级是: 浏览器缺省 < 外部样式表 < 内部样式表 < 内联样式 其中样式表又有: 类选择器 < 类派生选择器 < ID选择器 & ...

  8. android 登录效果

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  9. 连接远程数据库ORACLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORACLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...

  10. 主机ping不通virtualbox虚拟机的解决办法

    虚拟机与主机之间相互ping通有一个问题,就是虚拟机能够ping通主机 本地主机ping不通虚拟机: 解决办法: 1)如果虚拟机有两个网卡: 将虚拟机网卡2的连接方式改成桥接即可: ⚠️要将虚拟机重启 ...