libgdx实例分析小记(一)

 

SuperJumper
public class SuperJumperAndroid extends AndroidApplication {
 /** Called when the activity is first created. */
 @Override
 public void onCreate (Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  initialize(new SuperJumper(), false);
 }
}

public class SuperJumper extends Game {
 boolean firstTimeCreate = true;

 @Override
 public Screen getStartScreen () {
  return new MainMenuScreen(this);
 }

 @Override
 public void create () {
  Settings.load();
  Assets.load();
  super.create();
 }
}

public abstract class Game implements ApplicationListener
Game还是个ApplicationListener  多了一个方法
 public abstract Screen getStartScreen ();

并且把逻辑规范化
 @Override
 public void render () {
  screen.update(Gdx.graphics.getDeltaTime());  //处理逻辑部分
  screen.present(Gdx.graphics.getDeltaTime()); //处理渲染部分
 }

MainMenuScreen启动画面,逻辑
 @Override
 public void update (float deltaTime) {
  if (Gdx.input.justTouched()) {   //点击
   guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

   if (OverlapTester.pointInRectangle(playBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);  //点击的音效
    game.setScreen(new GameScreen(game));  //跳转
    return;
   }
   if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);
    game.setScreen(new HighscoresScreen(game));
    return;
   }
   if (OverlapTester.pointInRectangle(helpBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);
    game.setScreen(new HelpScreen(game));
    return;
   }
   if (OverlapTester.pointInRectangle(soundBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);
    Settings.soundEnabled = !Settings.soundEnabled;
    if (Settings.soundEnabled)
     Assets.music.play();
    else
     Assets.music.pause();
   }
  }
 }

 public MainMenuScreen (Game game) {
  super(game);
  guiCam = new OrthographicCamera(320, 480);   //正交投影,投影的大小320 480
  guiCam.position.set(320 / 2, 480 / 2, 0);    //观察点坐标,正中间的位置
  batcher = new SpriteBatch();
  soundBounds = new Rectangle(0, 0, 64, 64);  //声音按钮的位置在左下角
  playBounds = new Rectangle(160 - 150, 200 + 18, 300, 36); 
  highscoresBounds = new Rectangle(160 - 150, 200 - 18, 300, 36); //高分按钮的位置
  helpBounds = new Rectangle(160 - 150, 200 - 18 - 36, 300, 36); //帮助按钮的位置
  touchPoint = new Vector3();
 }

//渲染
 @Override
 public void present (float deltaTime) {
  GLCommon gl = Gdx.gl;
  gl.glClearColor(1, 0, 0, 1);
  gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  guiCam.update();
  batcher.setProjectionMatrix(guiCam.combined);

  batcher.disableBlending();
  batcher.begin();
  batcher.draw(Assets.backgroundRegion, 0, 0, 320, 480);  //画背景
  batcher.end();

  batcher.enableBlending();
  batcher.begin();
  batcher.draw(Assets.logo, 160 - 274 / 2, 480 - 10 - 142, 274, 142); //绘制logo
  batcher.draw(Assets.mainMenu, 10, (int)(200 - 110 / 2), 300, 110);  //绘制主菜单
  batcher.draw(Settings.soundEnabled ? Assets.soundOn : Assets.soundOff, 0, 0, 64, 64);
  batcher.end();
 }


OverlapTester检测是否在范围内
public class OverlapTester {
 public static boolean overlapRectangles (Rectangle r1, Rectangle r2) {
  if (r1.x < r2.x + r2.width && r1.x + r1.width > r2.x && r1.y < r2.y + r2.height && r1.y + r1.height > r2.y)
   return true;
  else
   return false;
 }

 public static boolean pointInRectangle (Rectangle r, Vector2 p) {
  return r.x <= p.x && r.x + r.width >= p.x && r.y <= p.y && r.y + r.height >= p.y;
 }

 public static boolean pointInRectangle (Rectangle r, float x, float y) {
  return r.x <= x && r.x + r.width >= x && r.y <= y && r.y + r.height >= y;
 }
}

你可能感兴趣的:(android,开发系列教程)