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. 软件工程-XP方法十二个最佳实践

  2. kafka入门1:安装及配置

    1下载 官方下载地址:https://kafka.apache.org/downloads 案例使用版本:kafka_2.11-1.1.0.tgz 2上传服务器 使用ftp工具将压缩包放置到服务器上 ...

  3. SQL SERVER_Restore(version)

    Background: We have a project use SQL SERVER 2012. When I tried to restore a backup into my local da ...

  4. Tidb缩减tikv机器

    生产环境下,如何缩减机器? 1.首先是检查出来那个tikv节点需要缩减 " -d store { ", "stores": [ { "store&qu ...

  5. 乘风破浪:LeetCode真题_038_Count and Say

    乘风破浪:LeetCode真题_038_Count and Say 一.前言     这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...

  6. 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array

    乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言     将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...

  7. November 13th, 2017 Week 46th Monday

    Don't undermine your worth by comparing yourself with others. 别拿自己和他人比较,这只会降低你原有的价值. Honestly, I don ...

  8. 基础知识整理汇总 - Java学习(一)

    java 语言规范及相关文档资源 Java源码:安装目录下 src.zip 文件 java文档:https://docs.oracle.com/en/java/ 语言规范:http://docs.or ...

  9. C++构造析构函数生命期及对象生命期

  10. BZOJ1458:士兵占领(有上下界最小流)

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...