欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動的方法

 更新時(shí)間:2015年12月29日 14:45:56   作者:q757989418  
這篇文章主要介紹了Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動的方法,實(shí)例分析了Android中使用libgdx框架實(shí)現(xiàn)響應(yīng)方向鍵的技巧,適用于Android游戲開發(fā)領(lǐng)域,需要的朋友可以參考下

本文實(shí)例講述了Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動的方法。分享給大家供大家參考,具體如下:

package com.demo;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
//Libgdx的Texture與Sprite使用
public class LibgdxActivity extends AndroidApplication {
  public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    // 初始化游戲屏幕,并設(shè)置是否支持GLES 2.0,如果您對向下兼容沒什么需要選擇true即可(2.1以上),否則選擇false。
//   initialize(new FirstGame(), true);
    initialize(new Box2DDemo(), true);
  }
}

package com.demo;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
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("bt.png"));
    this.height = texture.getHeight();
    this.width = texture.getWidth();
  }
}

package com.demo;
import android.util.Log;
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.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
class FirstGame 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;
  @Override
  public void create() {
    stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    firstActor = new FirstActor("renwu");
    buttonUp = initButton(UP,40,80);
    buttonDown = initButton(DOWN,40,0);
    buttonLeft = initButton(LEFT,0,40);
    buttonRight = initButton(RIGHT,80,40);
    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 render() {
    // 清屏
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
  }
  @Override
  public void dispose() {
    // 釋放占用的資源
    stage.dispose();
  }
  @Override
  public void resume() {
  }
  @Override
  public void pause() {
  }
  @Override
  public void resize(int width, int height) {
  }
  public Button initButton(String name,int x,int y){
    if(name.equals(UP)){
      texture = new Texture(Gdx.files.internal("up_alt.png"));
    }else if(name.equals(DOWN)){
      texture = new Texture(Gdx.files.internal("down_alt.png"));
    }else if(name.equals(LEFT)){
      texture = new Texture(Gdx.files.internal("back_alt.png"));
    }else if(name.equals(RIGHT)){
      texture = new Texture(Gdx.files.internal("forward_alt.png"));
    }
    patch1 = new NinePatch(texture, 0, 0, 0, 0);
    Button button = new Button(new ButtonStyle(patch1, patch1, patch1, 0f, 0f, 0f, 0f, null, null), name);
    button.x = x;
    button.y = y;
    button.width = 32;
    button.height = 32;
    return button;
  }
  @Override
  public void click(Actor button) {
    if(button.equals(buttonUp)){
      Actor actor = button.parent.findActor("renwu");;
      actor.y += 10;
      Log.i("touch", "up");
    }else if(button.equals(buttonDown)){
      Actor actor = button.parent.findActor("renwu");;
      actor.y -= 10;
      Log.i("touch", "down");
    }else if(button.equals(buttonLeft)){
      Actor actor = button.parent.findActor("renwu");;
      actor.x -= 10;
      Log.i("touch", "left");
    }else if(button.equals(buttonRight)){
      Actor actor = button.parent.findActor("renwu");;
      actor.x += 10;
      Log.i("touch", "right");
    }
  }
}

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 詳解Android TableLayout表格布局

    詳解Android TableLayout表格布局

    表格布局的標(biāo)簽是TableLayout,TableLayout繼承了LinearLayout。所以它依然是一個(gè)線性布局,通過本文給大家介紹Android TableLayout表格布局,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • Android二級緩存加載圖片實(shí)現(xiàn)照片墻功能

    Android二級緩存加載圖片實(shí)現(xiàn)照片墻功能

    這篇文章主要為大家詳細(xì)介紹了Android二級緩存加載圖片實(shí)現(xiàn)照片墻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android圖片識別應(yīng)用詳解

    Android圖片識別應(yīng)用詳解

    這篇文章主要為大家詳細(xì)介紹了Android圖片識別的應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android  AsyncTask的缺陷和問題總結(jié)

    Android AsyncTask的缺陷和問題總結(jié)

    這篇文章主要介紹了Android AsyncTask的缺陷和問題總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android編程獲取手機(jī)屏幕分辨率大小的方法

    Android編程獲取手機(jī)屏幕分辨率大小的方法

    這篇文章主要介紹了Android編程獲取手機(jī)屏幕分辨率大小的方法,涉及Android基于getWindowManager()方法獲取手機(jī)屏幕相關(guān)信息的使用技巧,需要的朋友可以參考下
    2016-02-02
  • Android編譯出現(xiàn)Warning:Mapping?new?ns?to?old?ns報(bào)錯的解決方案

    Android編譯出現(xiàn)Warning:Mapping?new?ns?to?old?ns報(bào)錯的解決方案

    android在編譯的過程中難免會出現(xiàn)些錯誤,下面這篇文章主要給大家介紹了關(guān)于Android編譯出現(xiàn)Warning:Mapping?new?ns?to?old?ns報(bào)錯的解決方案,需要的朋友可以參考下
    2023-02-02
  • Android開發(fā)Jetpack組件LiveData使用講解

    Android開發(fā)Jetpack組件LiveData使用講解

    LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時(shí)候才會更新數(shù)據(jù)
    2022-08-08
  • Android開啟ADB網(wǎng)絡(luò)調(diào)試方法

    Android開啟ADB網(wǎng)絡(luò)調(diào)試方法

    今天小編就為大家分享一篇Android開啟ADB網(wǎng)絡(luò)調(diào)試方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Android垂直滾動控件ScrollView使用方法詳解

    Android垂直滾動控件ScrollView使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android垂直滾動控件ScrollView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android仿開心消消樂大樹星星無限循環(huán)效果

    Android仿開心消消樂大樹星星無限循環(huán)效果

    這篇文章主要為大家詳細(xì)介紹了Android仿開心消消樂大樹星星無限循環(huán)效果,自定義view實(shí)現(xiàn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評論