這篇文章給大家分享的是有關Android中Libgdx如何使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創新互聯建站專注于網站建設,為客戶提供成都網站制作、成都網站建設、網頁設計開發服務,多年建網站服務經驗,各類網站都可以開發,成都品牌網站建設,公司官網,公司展示網站,網站設計,建網站費用,建網站多少錢,價格優惠,收費合理。
在項目中實現了一個效果,主要是畫一個圓。為了后續使用方便,將這個圓封裝在一個自定義Actor(CircleActot)中,后續想顯示一個圓的時候,只要創建一個CircleActor中即可。 部分代碼如下所示:
package com.ef.smallstar.unitmap.widget;
import android.content.res.Resources;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.ef.smallstar.EFApplication;
import com.ef.smallstar.R;
/**
* Created by ext.danny.jiang on 17/4/17.
*
* A Widget currently used in the UnitMap, shown as a CIRCLE shape
* if text not null, there would be a text drawn in the center of the circle
*/
public class CircleActor extends Actor {
private float centerX;
private float centerY;
private String text;
private float radius;
private ShapeRenderer sr;
private BitmapFont bitmapFont;
public CircleActor(float x, float y, float radius) {
this(x, y, radius, null);
}
public CircleActor(float x, float y, float radius, String text) {
this.centerX = x;
this.centerY = y;
this.radius = radius;
this.text = text;
sr = new ShapeRenderer();
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw(Batch batch, float parentAlpha) {
...
batch.end();
sr.setProjectionMatrix(batch.getProjectionMatrix());
sr.setTransformMatrix(batch.getTransformMatrix());
sr.begin(ShapeRenderer.ShapeType.Filled);
sr.circle(centerX, centerY, radius);
sr.end();
batch.begin();
...
}然后創建一個Stage對象,并將CircleActor對象添加到Stage中即可顯示。 但是無法給此CircleActor對象添加一個ClickLitener監聽。
例如如下代碼:
Stage stage = new Stage();
CircleActor ca = new CircleActor(100, 100, 50, "Hello World");
ca.addListener(new ClickListener(){
public void click(){
Gdx.app.log("TAG", "ca is clicked");
}
})
stage.add(ca);上述代碼中的click方法永遠無法被調用! 后續調了大半天之后終于弄清楚了原因:雖然在CircleActor的draw方法中通過ShapeRenderer.circle方法將一個圓畫到了屏幕上的某一位置,但是此ShapeRenderer其實和Actor之間并沒有太多的聯系。唯一的聯系就是以下兩句代碼, 意思應該是將ShapeRenderer的camera和Actor對象一致。
sr.setProjectionMatrix(batch.getProjectionMatrix()); sr.setTransformMatrix(batch.getTransformMatrix());
但是此時,CircleActor并沒有設置真正的大小與位置, 因此解決上述問題,需要在構造器中將CircleActor的大小和位置與ShapeRenderer做到一致 !!
如下代碼所示,只要添加兩行代碼即可:
public EfCircle(float x, float y, float radius, String text) {
this.centerX = x;
this.centerY = y;
this.radius = radius;
this.text = text;
//解決ShapeRenderer無法獲取Touch事件
setPosition(centerX - radius, centerY - radius);
setSize(radius * 2, radius * 2);
sr = new ShapeRenderer();
}感謝各位的閱讀!關于“Android中Libgdx如何使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網頁標題:Android中Libgdx如何使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題
本文路徑:http://www.yijiale78.com/article36/pchspg.html
成都網站建設公司_創新互聯,為您提供小程序開發、、服務器托管、云服務器、網站制作、網站設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯