libgda入口类:Cuboc
public class Cuboc extends AndroidApplication {
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize(new ActorDemo(), true);
}
}
ActorDemo 类:
import com.badlogic.cuboc.demo.actor.FirstActor;
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.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
/***
* 四个方向键移动
* @author cui.li
*
*/
public class ActorDemo implements ApplicationListener, ClickListener {
private static String UP = "up";
private static String DOWN = "down";
private static String LEFT = "left";
private static String RIGHT = "right";
// 舞台
private Stage stage;
// 演员
private Actor firstActor;
private Texture texture;
private Button buttonUp, buttonDown, buttonLeft, buttonRight;
private NinePatch patch1, patch2;
private int screenWidth;
private int screenHeight;
@Override
public void create() {
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
true);
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
firstActor = new FirstActor("ceshi");
buttonUp = initButton(UP, 68, 130);
buttonDown = initButton(DOWN, 68, 0);
buttonLeft = initButton(LEFT, 0, 68);
buttonRight = initButton(RIGHT, 130, 68);
buttonUp.setClickListener(this);
buttonDown.setClickListener(this);
buttonLeft.setClickListener(this);
buttonRight.setClickListener(this);
stage.addActor(firstActor);
stage.addActor(buttonUp);
stage.addActor(buttonDown);
stage.addActor(buttonLeft);
stage.addActor(buttonRight);
Gdx.input.setInputProcessor(stage);
}
@Override
public void resize(int width, int height) {
Log.i("", "width: " + width + " height: " + height);
}
@Override
public void render() {
// 清屏
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
// 释放占用的资源
stage.dispose();
}
public Button initButton(String name, int x, int y) {
if (name.equals(UP)) {
texture = new Texture(Gdx.files.internal("data/texture/up_alt.png"));
} else if (name.equals(DOWN)) {
texture = new Texture(
Gdx.files.internal("data/texture/down_alt.png"));
} else if (name.equals(LEFT)) {
texture = new Texture(
Gdx.files.internal("data/texture/back_alt.png"));
} else if (name.equals(RIGHT)) {
texture = new Texture(
Gdx.files.internal("data/texture/forward_alt.png"));
}
patch1 = new NinePatch(texture, 0, 0, 0, 0);
Button button = new Button(new ButtonStyle(patch1, patch1, patch1, 0f,
0f, 0f, 0f), name);
button.x = x;
button.y = y;
button.width = 128;
button.height = 128;
return button;
}
@Override
public void click(Actor button, float x, float y) {
Actor actor = null;
if (button.equals(buttonUp)) {
actor = button.parent.findActor("ceshi");
if (actor.y + actor.height <= screenHeight) {
actor.y += 10;
}
} else if (button.equals(buttonDown)) {
actor = button.parent.findActor("ceshi");
if (actor.y >= 0) {
actor.y -= 10;
}
} else if (button.equals(buttonLeft)) {
actor = button.parent.findActor("ceshi");
if (actor.x - 10 >= 0) {
actor.x -= 10;
}
} else if (button.equals(buttonRight)) {
actor = button.parent.findActor("ceshi");
if (actor.x + actor.width <= screenWidth) {
actor.x += 10;
}
}
}
}
FirstActor 类:
public class FirstActor extends Actor {
private Texture texture;
@Override
public void draw(SpriteBatch batch, float arg1) {
batch.draw(texture, this.x, this.y);
}
@Override
public Actor hit(float arg0, float arg1) {
if (x > 0 && y > 0 && this.height > y && this.width > x) {
return this;
} else {
return null;
}
}
@Override
public boolean touchDown(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
@Override
public void touchDragged(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void touchUp(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public FirstActor(String name) {
super(name);
texture = new Texture(Gdx.files.internal("data/texture/bt.png"));
this.height = texture.getHeight();
this.width = texture.getWidth();
}
}