package com.xuefei.mygame;
import android.os.Bundle;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.android.AndroidApplication;
public class MainActivity extends AndroidApplication {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize((ApplicationListener) new GameActivity(), false);
}
}
package com.xuefei.mygame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class GameActivity implements ApplicationListener {
Stage stage;
float width;
float height;
TextureAtlas atlas;
TextureAtlas buttons;
Image player;
ImageButton up;
ImageButton down;
ImageButton left;
ImageButton right;
@Override
public void create() {
// TODO Auto-generated method stub
//获取设备宽高
width =Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
stage = new Stage(width,height, true);
// 创建一个Label样式,使用默认黑色字体
LabelStyle labelStyle = new LabelStyle(new BitmapFont(), Color.BLACK);
// 创建标签,显示的文字是FPS:
Label label = new Label("FPS:", labelStyle);
// 设置标签名称为fpsLabel
label.setName("fpsLabel");
// 设置Y为0,即显示在最下面
label.setY(0);
// 设置X值,显示在屏幕最左侧
label.setX(label.getTextBounds().width);
// 将标签添加到舞台
stage.addActor(label);
atlas =new TextureAtlas("role0.pack");
buttons = new TextureAtlas("buttons.pack");
player = new Image(atlas.findRegion("image 5"));
player.setName("player");
player.setX(0);
player.setY(height/2-player.getHeight()/2);
stage.addActor(player);
Texture upTexture = new Texture(Gdx.files.internal("up.png"));
SpriteDrawable upDraw = new SpriteDrawable(new Sprite(upTexture));
up = new ImageButton(upDraw);
up.setPosition(64f, 128f);
up.addListener(new ClickListener(){
public void clicked(InputEvent event,float x,float y){
if(event.getTarget()==up){
System.out.println("click:"+x+","+y);
}
}
});
stage.addActor(up);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
stage.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
// 获取名为fpsLabel的标签
Label label = (Label) stage.getRoot().findActor("fpsLabel");
label.setText("FPS:" + Gdx.graphics.getFramesPerSecond());
// 更新X值以保证显示位置正确性
label.setX(label.getTextBounds().width);
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}