Libgdx使用ShapeRenderer自定义Actor,无法接收到Touch事件
来源:互联网 发布:鬼吹灯网络季播剧 编辑:程序博客网 时间:2024/06/06 02:24
今天在项目中实现了一个效果,主要是画一个圆。为了后续使用方便,将这个圆封装在一个自定义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(); }
5 0
- Libgdx使用ShapeRenderer自定义Actor,无法接收到Touch事件
- libgdx 自定义角色actor
- Libgdx使用ShapeRenderer画图形,设置抗锯齿
- LibGdx将ShapeRenderer和Path结合使用,实现填充效果
- libgdx [Actor]
- libGDX中Stage,Actor,Action的使用
- Libgdx中ScrollPane和Actor事件冲突问题的解决方案
- [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Actor与Action
- Struts2 自定义拦截器时Action无法接收到参数
- Activity是如何接收到touch事件的(窗口与用户输入系统)
- mono touch中自定义事件
- Libgdx 之Actor 演员类
- Scala:简单使用Actor的消息发送与接收求和
- 自定义View想使用Scroller实现滑动效果,但是事件接收出现问题,只收到ACTION_DOWN,而无法收到ACTION_MOVE, ACTION_UP等消息的处理
- Dialog的自定义view无法获取到点击事件
- UIScrollView无法响应touch事件的解决办法
- UIScrollView无法响应touch事件的解决办法
- scrollview/tableview 无法响应touch事件
- Linux (1) : 基础介绍
- JavaScript UI框架jQWidgets更新至v4.5.1丨附下载
- [Bartender]Bartender条形码制作软件如何实现左侧自动补零
- Oracle数据库下 查询那张表被锁定 被谁锁定
- 生成服务端推送证书
- Libgdx使用ShapeRenderer自定义Actor,无法接收到Touch事件
- Java基本类型占用的字节数(char占用几个字节问题)
- cnn介绍
- sql增加索引
- 使用commons-fileupload上传文件到服务器
- 从Code Review 谈如何做技术
- PLSQL远程连接oracle数据库配置
- centos7下创建新用户并授权
- Python基础进阶篇——函数式编程