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 class Lib005_Action extends ApplicationAdapter{ 22 23 Stage stage; 24 Image img; 25 Texture texture; 26 Action totalAction; 27 Action exitAction; 28 29 @Override 30 public void create() { 31 // TODO Auto-generated method stub 32 stage = new Stage(); 33 texture = new Texture( Gdx.files.internal( "data/pal4_1.jpg" ) ); 34 img = new Image( 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 = new Action(){ 55 @Override 56 public boolean act(float delta) { 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(new InputListener(){ 65 @Override 66 public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 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 void render() { 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 void dispose() { 90 // TODO Auto-generated method stub 91 92 super.dispose(); 93 } 94 95 }
action一般是添加到Actor中的,当然也可以添加到stage中,使用起来很方便。程序中设计的是一个循环,当点击图片时会清除以前的Action并进行一次淡出淡入后消失。
运行效果: