編輯:關於 Android 程式設計
本文示範如何在 libgdx 中做出一組虛擬方向鍵,用來控制角色在畫面上移動。
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); // 初始化游戲屏幕,並設置是否支持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(); } }
用 Stage 管理角色與四顆方向按鈕,並在 click 回呼中依按鈕方向改變角色座標。
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"); } } }
整體結構其實很直觀:用 Stage 當容器,把角色與四顆 Button 都 addActor 進去,再用 NinePatch 讓按鈕圖片可伸縮;點擊時透過 findActor 找到角色並微調 x 或 y 座標,就完成了方向鍵移動。若要更順滑,可改成按住連續移動,或在 render 中依狀態持續位移。
希望本文所述對大家 Android 程式設計有所幫助。
Android AlertDialog 點擊外部自動消失的成因與兩個解法
開發時遇到一個惱人的現象:手指一碰到對話框以外的區域,它就自己關掉了。查了一下才發現,從 Android 4.0 開始,AlertDialog 的預設行為變了——點擊對話
Android 教學:用 ViewPager 實現自動滾動循環輪播效果
本文為對 Android 利用 ViewPager 實現圖片左右循環滑動效果感興趣的朋友而寫,主要介紹如何實現 ViewPager 的自動播放與循環滾動,並順便解決 Vi
Android 4.4 ART模式怎麼開?效能與相容性一次看
Android 4.4.x可開啟ART模式,提升流暢度並降低記憶體占用。以OPPO為例,進入設定、開發者選項、運行模式,選擇使用ART。 若沒有開發者選項,可連續點擊版本
Android Studio 下去不掉標題欄?三種解法與背後的原因
在 Eclipse 時代,要拿掉 Activity 的預設標題欄,我習慣寫這一行: requestWindowFeature(Window.FEATURE_NO_TIT