直接贴本demo的核心代码,如果有什么不明白的,请参考前面的博客
package com.doodle.rdemo3;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
public class FirstGame implements ApplicationListener {
private Stage stage;
private Texture texture;
@Override
public void create() {
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
texture = new Texture(Gdx.files.internal("star.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
int maxWidth = Gdx.graphics.getWidth() - texture.getWidth();
int maxHeight = Gdx.graphics.getHeight() - texture.getHeight();
float duration = 4f;
int i;
for(i = 0 ; i < 20 ; ++i){
Image image = new Image(texture);
image.setX(MathUtils.random(0,maxWidth));
image.setY(MathUtils.random(0,maxHeight));
//!!!!每一个Action都尽量要设置duration,否则Action在一瞬间就完成了
Action moveAction = Actions.sequence(Actions.moveTo(MathUtils.random(0, maxWidth),MathUtils.random(0,maxHeight),duration/2) , Actions.moveBy(MathUtils.random(0, maxWidth),MathUtils.random(0,maxHeight) ,duration / 2));
Action rotateAction = Actions.rotateTo(360,duration);
Action repeatAction = Actions.repeat(10,Actions.sequence( Actions.fadeIn(duration / 20) , Actions.fadeOut(duration / 20)));
Action parallelAction = Actions.parallel(moveAction,rotateAction,repeatAction);
image.addAction(parallelAction);
stage.addActor(image);
}
Gdx.input.setInputProcessor(stage);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}