前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题

详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题

作者头像
砸漏
发布2020-10-22 11:26:09
7880
发布2020-10-22 11:26:09
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题

今天在项目中实现了一个效果,主要是画一个圆。为了后续使用方便,将这个圆封装在一个自定义Actor(CircleActot)中,后续想显示一个圆的时候,只要创建一个CircleActor中即可。 部分代码如下所示:

代码语言:javascript
复制
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监听。

例如如下代码:

代码语言:javascript
复制
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对象一致。

代码语言:javascript
复制
sr.setProjectionMatrix(batch.getProjectionMatrix());
sr.setTransformMatrix(batch.getTransformMatrix());

但是此时,CircleActor并没有设置真正的大小与位置, 因此解决上述问题,需要在构造器中将CircleActor的大小和位置与ShapeRenderer做到一致 !!

如下代码所示,只要添加两行代码即可:

代码语言:javascript
复制
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();
  }

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档