Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android遊戲引擎libgdx使用教學4:舞台與演員的遊戲實例

編輯:Android 遊戲開發

大家先要建立「舞台」與「演員」的概念,下面用一張遊戲截圖來說明:

請仔細觀察圖中的元素:有些不能動、有些可以動,有些有特效、有些沒有,有各種圖片,但它們其實都可以統稱為「演員(Actor)」。圖中用框標出的就是演員,而整個遊戲介面就是我們的「舞台」。

演員是遊戲設計中常用的一個物件,它接受舞台的統一管理,擁有一些公共事件(例如觸摸、點擊),同時也有自身的回應與屬性;而舞台就是容納演員的場所,它統一管理所有演員、接收輸入,並提供一個方便的框架來處理演員隨時間的變化。

Stage 類別

protected final Group root;
protected final SpriteBatch batch;
protected Camera camera;

它擁有一個 Group、一個 SpriteBatch 和一個相機。SpriteBatch 前面幾篇已經說過,這裡不再重複。Group 是一個用於容納與控制演員的類別,但要注意,Group 本身其實也繼承自 Actor。相機這裡先跳過,之後再說,暫時可以理解成一個控制觀察視角與座標轉換的工具。

當我們擁有一個演員後,就可以呼叫 addActor 方法把它加入舞台。舞台可以接收輸入,但需要設定:

Gdx.input.setInputProcessor(stage);

舞台與演員實例

下面來個例子,控制一個人物前進。

控制人物的按鈕:

將所需的圖片放到 assets 中:

新增三個類別:FirstGame(實作 ApplicationListener)、FirstActor(繼承 Actor)、NarrowButton(繼承 Actor)。

先看 FirstGame:宣告一個 Stage,然後建立 FirstActor 與 NarrowButton 實例並加入舞台,最後把輸入回應設為 Stage。

package com.cnblogs.htynkn.listener;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.cnblogs.htynkn.domain.FirstActor;
import com.cnblogs.htynkn.domain.NarrowButton;

public class FirstGame implements ApplicationListener {
    private Stage stage;
    private FirstActor firstActor;
    private NarrowButton button;

    @Override
    public void create() {
        stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        firstActor = new FirstActor("renwu");
        button = new NarrowButton("narrow");
        stage.addActor(firstActor);
        stage.addActor(button);
        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();
    }
}

再看 FirstActor:宣告一個 Texture 用於繪製,在建構方法中取得高度與寬度,以便後續的 hit 事件判斷。

package com.cnblogs.htynkn.domain;

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 {
    Texture texture;

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        batch.draw(texture, this.x, this.y);
    }

    @Override
    public Actor hit(float x, float y) {
        if (x > 0 && y > 0 && this.height > y && this.width > x) {
            return this;
        } else {
            return null;
        }
    }

    public FirstActor(String name) {
        super(name);
        texture = new Texture(Gdx.files.internal("actor1.gif"));
        this.height = texture.getHeight();
        this.width = texture.getWidth();
    }

    // touchDown / touchDragged / touchUp 略
}

NarrowButton 的繪製部分和上面類似,重點在於點擊後控制人物行動。修改 touchDown 事件:透過 Group 取得 FirstActor,控制它的 x 值。

public boolean touchDown(float x, float y, int pointer) {
    Actor actor = this.parent.findActor("renwu");
    actor.x += 10;
    return false;
}

效果:

至此,一個最簡單的人物控制就實作完成了。不過這個實例還有很多可改進的地方,比如方向按鈕沒有點擊效果、人物沒有移動動畫。我們可以用 Animation 來實現。新增一張圖片:

具體原理可以看看 Animation 類別:

public class Animation {
    final TextureRegion[] keyFrames;
    public float frameDuration;

    public Animation (float frameDuration, TextureRegion... keyFrames) {
        this.frameDuration = frameDuration;
        this.keyFrames = keyFrames;
    }

    // 根據 stateTime(停留在該狀態的秒數)取出對應的畫面
    public TextureRegion getKeyFrame (float stateTime, boolean looping) {
        int frameNumber = (int)(stateTime / frameDuration);
        if (!looping) {
            frameNumber = Math.min(keyFrames.length - 1, frameNumber);
        } else {
            frameNumber = frameNumber % keyFrames.length;
        }
        return keyFrames[frameNumber];
    }
}

可以看出,所謂的動畫其實就是一張張圖片不斷切換(其實所有動畫都是這個樣子)。我們建立一個圖片列表,然後依時間不斷取出、重新繪製,就形成動畫了。注意傳入的時間與圖片列表大小的問題,修改 FirstActor 如下:

package com.cnblogs.htynkn.domain;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class FirstActor extends Actor {
    Texture texture1;
    Texture texture2;
    Animation animation;
    TextureRegion[] walksFrame;
    float stateTime;

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        stateTime += Gdx.graphics.getDeltaTime();
        TextureRegion currentFrame = animation.getKeyFrame(stateTime, true);
        batch.draw(currentFrame, this.x, this.y);
    }

    @Override
    public Actor hit(float x, float y) {
        if (x > 0 && y > 0 && this.height > y && this.width > x) {
            return this;
        } else {
            return null;
        }
    }

    public FirstActor(String name) {
        super(name);
        texture1 = new Texture(Gdx.files.internal("actor1.gif"));
        texture2 = new Texture(Gdx.files.internal("actor2.gif"));
        this.height = texture1.getHeight();
        this.width = texture1.getWidth();
        TextureRegion region1 = new TextureRegion(texture1);
        TextureRegion region2 = new TextureRegion(texture2);
        walksFrame = new TextureRegion[30];
        for (int i = 0; i < 30; i++) {
            walksFrame[i] = (i % 2 == 0) ? region1 : region2;
        }
        animation = new Animation(0.25f, walksFrame);
    }

    // touchDown / touchDragged / touchUp 略
}

效果:

這裡要注意,為什麼我們要把 Texture 轉為 TextureRegion?這是因為在實際開發中,圖片通常是整合在一起的,比如所有角色要用的圖片都放在同一張圖裡,然後分割截取,對應的輔助方法是 TextureRegion.split。

另外可以發現,NarrowButton 和 FirstActor 中有大量重複的程式碼,可能有朋友覺得應該提取出來——其實 libgdx 已經幫我們做好了,可以參考圖中的常用 UI 控制項,估計下一篇就會講到。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved