libgdx学习记录6——动作Action

时间:2022-09-29 17:18:51

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