libgdx学习记录6——动作Action
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的更多相关文章
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录23——图片移动选择
模拟移动选择图片,采用相机实现. package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter; import com.bad ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- libgdx学习记录17——照相机Camera
照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...
随机推荐
- hashCode()与equals()方法的对比
Java对于eqauls方法和hashCode方法是这样规定的: 1.如果两个对象相同,那么它们的hashCode值一定要相同: 2.如果两个对象的hashCode相同,它们并不一定相同(上面 ...
- Python数据类型之list和tuple
list是一种有序的集合,可以随时添加和删除其中的元素. 用len()函数可以获得list元素的个数. 用索引来访问list中每一个位置的元素,索引是从0开始的.如果要取最后一个元素,除了计算索引位置 ...
- 在web中如何调整上传过的图片方向 (exif)
前提: 相机中拍的照片放到web上不会自动识别方向,如有些竖向显示的照片放到web上横向显示.这些照片在windows上是正确显示的.但是web不会自动旋转照片到正确方向.下面我们通过两种方法来实现这 ...
- MySQL-死锁查询
1.查询是否锁表 show OPEN TABLES where In_use > 0; 查询到相对应的进程 === 然后 kill id 2.查询进程 show processlist 补 ...
- VMware虚拟机打开后不兼容
在版本VMware Workstation10.0设置兼容性,在编辑——首选项——工作空间——设置EXS兼容.计算机工作区域打开虚拟机,右键管理.兼容性从新配置
- spark的shuffle和原理分析
概述 Shuffle就是对数据进行重组,由于分布式计算的特性和要求,在实现细节上更加繁琐和复杂. 在MapReduce框架,Shuffle是连接Map和Reduce之间的桥梁,Map阶段 ...
- [Python2]介绍关于Uiautomator的watcher使用场景及使用方法
[官方的介绍]: Watcher You can register watcher to perform some actions when a selector can not find a mat ...
- linux禁止非法用户试探登录
当我们的linux主机一旦暴露在互联网上,就会遭受到来自网络上的一些非法用户的骚扰.如弱口令扫描,试探性登录:这些行为对linux主机构成一定的威胁.那怎样防范此类的攻击了,这里写了一个脚本,功能就是 ...
- python 爬取全量百度POI
在网上找了很多关于爬取百度POI的文章,但是对“全量”的做法并没有得到最终的解决方案,自己写了一个,但还是不能实现全量POI抓取,能够达到至少50%的信息抓取.注意:这里所指“全量”是能够达到100% ...
- JQuery 为radio赋值问题
今天用jquery 为radio赋值,从百度查了一下方法: $("input[name='radioName'][value=2]").attr("checked&quo ...