如何检测在Javalibgdx中是否触摸了精灵?
在过去的几天里,我一直在将我的游戏()移植到Android移动平台。我在谷歌上快速搜索了sprite touch detection,但没有找到任何有用的东西。每个气球一碰就会爆炸,我只需要检测它是否被碰过。 以下是我的气球繁殖代码: 渲染(x、y、宽度和高度是随机的): 在主游戏类中产卵:如何检测在Javalibgdx中是否触摸了精灵?,java,android,libgdx,Java,Android,Libgdx,在过去的几天里,我一直在将我的游戏()移植到Android移动平台。我在谷歌上快速搜索了sprite touch detection,但没有找到任何有用的东西。每个气球一碰就会爆炸,我只需要检测它是否被碰过。 以下是我的气球繁殖代码: 渲染(x、y、宽度和高度是随机的): 在主游戏类中产卵: addBalloon(new Balloon()); public static void addBalloon(Balloon b) { balloons.add(b); } 在类中,使用re
addBalloon(new Balloon());
public static void addBalloon(Balloon b) {
balloons.add(b);
}
在类中,使用render方法可以执行以下代码:
Vector3 touchPoint=new Vector3();
void update()
{
if(Gdx.input.justTouched())
{
//unprojects the camera
camera.unproject(touchPoint.set(Gdx.input.getX(),Gdx.input.getY(),0));
if(balloon.getBoundingRectangles().contains(touchPoint.x,touchPoint.y))
{
// will be here when balloon will be touched
}
}
}
您有
Gdx.input.getX()
和Gdx.input.getY()
。最后一次触摸的X和Y坐标。
只需将它们与气球框进行比较。您可以在鼠标上添加一个小的1x1像素矩形,并检查它是否与其他框相交或包含其他框。您确实需要为所有要使其可单击的对象使用框。我就是这样做的,但根据您使用的场景和可触摸的元素,可以使用稍微优化的方法:
public GameScreen implements Screen, InputProcessor
{
@Override
public void show()
{
Gdx.input.setInputProcessor(this);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
for(int i = 0; i < balloons.size(); i++)
{
if(balloons.get(i).contains(pointerX, pointerY))
{
balloons.get(i).setSelected(true);
}
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
for(int i = 0; i < balloons.size(); i++)
{
if(balloons.get(i).contains(pointerX, pointerY) && balloons.get(i).getSelected())
{
balloons.get(i).execute();
}
balloons.get(i).setSelected(false);
}
return true;
}
public class InputTransform
{
private static int appWidth = 480;
private static int appHeight = 320;
public static float getCursorToModelX(int screenX, int cursorX)
{
return (((float)cursorX) * appWidth) / ((float)screenX);
}
public static float getCursorToModelY(int screenY, int cursorY)
{
return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ;
}
}
公共游戏屏幕实现屏幕、输入处理器
{
@凌驾
公开展览(
{
Gdx.input.setInputProcessor(此);
}
@凌驾
公共布尔接地(整数屏幕X、整数屏幕Y、整数指针、整数按钮)
{
float pointerX=InputTransform.getCursorToModelX(windowWidth,screenX);
float pointerY=InputTransform.getCursorToModelY(窗口高度,屏幕显示);
对于(int i=0;i
我制作了一个小类,用于游戏中检测精灵是否被触摸
public class SpriteTouchable extends Sprite {
public SpriteTouchable(Texture texture) {
super(texture);
}
public SpriteTouchable(Sprite sprite) {
set(sprite);
}
public static float xTrans(float x)
{
return x*Gdx.graphics.getWidth()/480;
}
public static float yTrans(float y)
{
return y*Gdx.graphics.getHeight()/320;
}
private boolean isTouched;
/**
* Type: Input Listener function
* listen if this sprite button was pressed (touched)
* @param marge : the extra touchable space out of sprite
* @param x : x position touched by user
* @param y : y position touched by user
*
* return true : Sprite touched
* return false : Sprite not touched
*/
public boolean isPressing(int marge,int x, int y) {
if((x>getX() -xTrans(marge))&& x<getX() +getWidth()+xTrans(marge)) {
if((y>getY() -yTrans(marge))&& y<getY()+getHeight()+yTrans(marge)) {
return true;
}
}
return false;
}
}
public boolean isTouched() {
return isTouched;
}
使用相同的逻辑,您可以检测精灵释放,也可以自定义精灵被触摸时的效果大小等
希望这是有帮助的!我有一个简单的解决方案
这就像创建一个1像素乘1像素的小矩形
rect矩形;
rect=新矩形(0,0,1,1);
然后创建一个名为
触摸检查();
在这个方法中,我们将做三件事
首先检查是否触摸了屏幕
public class SpriteTouchable extends Sprite {
public SpriteTouchable(Texture texture) {
super(texture);
}
public SpriteTouchable(Sprite sprite) {
set(sprite);
}
public static float xTrans(float x)
{
return x*Gdx.graphics.getWidth()/480;
}
public static float yTrans(float y)
{
return y*Gdx.graphics.getHeight()/320;
}
private boolean isTouched;
/**
* Type: Input Listener function
* listen if this sprite button was pressed (touched)
* @param marge : the extra touchable space out of sprite
* @param x : x position touched by user
* @param y : y position touched by user
*
* return true : Sprite touched
* return false : Sprite not touched
*/
public boolean isPressing(int marge,int x, int y) {
if((x>getX() -xTrans(marge))&& x<getX() +getWidth()+xTrans(marge)) {
if((y>getY() -yTrans(marge))&& y<getY()+getHeight()+yTrans(marge)) {
return true;
}
}
return false;
}
}
public boolean isTouched() {
return isTouched;
}
第二,将矩形放在触摸屏的位置
然后,最后检查您的矩形是否与精灵矩形重叠。
那样
private void\u checking(){
if(Gdx.input.isTouched()){
setPosition(Gdx.input.getX(),Gdx.input.getY());
if(rect.overlaps(back.getBoundingRectangle())){
//你想在这里干什么就干什么
}
}
}
我有一个叫做“关卡”的精灵数组,我检查我按了哪个关卡
private void touching_checking() {
if (Gdx.input.isTouched()) {
rect.setPosition(Gdx.input.getX(), Gdx.graphics.getWidth() - Gdx.input.getY());
for (int i = 0; i < levels.size; i++) {
if (rect.overlaps(levels.get(i).getBoundingRectangle())) {
//do what you want here
}
}
}
}
private void\u checking(){
if(Gdx.input.isTouched()){
setPosition(Gdx.input.getX(),Gdx.graphics.getWidth()-Gdx.input.getY());
对于(int i=0;i
这是我在sprite类中使用的内容。我在单击摄影机的位置为它指定向量。取消投影(new Vector3().set(x,y,0));。如果在sprite区域中单击,则返回true
/***
*
* @param v3 Vector with MouseClickPosition
* @return true if click was inside rectangle x --> x + width, y --> y +
* height
*/
public boolean clicked(Vector3 v3){
Rectangle rec = getBoundingRectangle();
if(v3.x >= rec.x && v3.x < rec.x + getWidth()){
if(v3.y >= rec.y && v3.y < rec.y + getHeight()){
System.out.println("click_on\t" + v3.x + ", " + v3.y);
return true;
}
else{
return false;
}
}
else{
return false;
}
}
/***
*
*@param v3带鼠标单击位置的向量
*@如果单击位于矩形x-->x+宽度,y-->y+内,则返回true
*高度
*/
已单击公共布尔值(Vector3 v3){
Rectangle=getBoundingRectangle();
如果(v3.x>=rec.x&&v3.x=rec.y&&v3.y
您尝试过什么?您阅读过wiki吗?(-检查输入处理部分。)windowWidth和WindowHeight在codeValid声明中未定义,如果我没有记错,它们是直接从Gdx.graphics.getWidth()和Gdx.graphics.getHeight()初始化的对我来说是可行的,但是没有内置的东西来处理这个问题真是太疯狂了!很好且简单的解决方案。例如,thx代码。在测试之前,可能需要转换触摸坐标以绘制坐标(反之亦然)。overlaps()
/***
*
* @param v3 Vector with MouseClickPosition
* @return true if click was inside rectangle x --> x + width, y --> y +
* height
*/
public boolean clicked(Vector3 v3){
Rectangle rec = getBoundingRectangle();
if(v3.x >= rec.x && v3.x < rec.x + getWidth()){
if(v3.y >= rec.y && v3.y < rec.y + getHeight()){
System.out.println("click_on\t" + v3.x + ", " + v3.y);
return true;
}
else{
return false;
}
}
else{
return false;
}
}