Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法
本文實(shí)例講述了Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法。分享給大家供大家參考,具體如下:
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,如果您對(duì)向下兼容沒(méi)什么需要選擇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";
//舞臺(tái)
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");
}
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無(wú)法接收到Touch事件的問(wèn)題
- 詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法
- Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
- Android drawable微技巧,你不知道的drawable細(xì)節(jié)
- Android指紋識(shí)別API講解,一種更快更好的用戶體驗(yàn)
- Android在Kotlin中更好地使用LitePal
- Android Studio輕松構(gòu)建自定義模板的步驟記錄
- 詳解Android 檢測(cè)權(quán)限的三種寫(xiě)法
- Android最簡(jiǎn)單的狀態(tài)切換布局實(shí)現(xiàn)教程
- android自定義環(huán)形對(duì)比圖效果
- Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
相關(guān)文章
Android Material加載進(jìn)度條制作代碼
這篇文章主要為大家詳細(xì)介紹了AndroidMaterial加載進(jìn)度條的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android SpringAnimation彈性動(dòng)畫(huà)解析
這篇文章主要為大家詳細(xì)介紹了Android SpringAnimation彈性動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法,結(jié)合實(shí)例形式分析了Android5.0以上實(shí)現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01
Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
這篇文章主要給大家介紹了關(guān)于Android單項(xiàng)綁定MVVM項(xiàng)目模板的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android shell命令行中過(guò)濾adb logcat輸出的幾種方法
本文主要介紹Android shell命令行中過(guò)濾adb logcat輸出的方法,這里整理了幾種方法,并詳細(xì)的說(shuō)明,有需要的朋友可以參考下2016-08-08
Android ViewPager實(shí)現(xiàn)無(wú)限循環(huán)的實(shí)例
這篇文章主要介紹了Android ViewPager實(shí)現(xiàn)無(wú)限循環(huán)的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android開(kāi)發(fā)之在程序中時(shí)時(shí)獲取logcat日志信息的方法(附demo源碼下載)
這篇文章主要介紹了Android開(kāi)發(fā)之在程序中時(shí)時(shí)獲取logcat日志信息的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了實(shí)時(shí)獲取logcat日志的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶相應(yīng)的demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02

