如何检测在 Java libGDX 中是否触摸了精灵?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24501268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I detect if a sprite was touched in Java libGDX?
提问by miguel.renaud
In the past few days I've been porting my game (Apopalypse) to the Android Mobile platform. I've did a quick search on Google of sprite touch detection but didn't find anything helpful. Each balloon will pop once touched and I just need to detect if it's touched. Here's my balloon spawning code:
在过去的几天里,我一直在将我的游戏 ( Apopalypse)移植到 Android 移动平台。我在谷歌上快速搜索了精灵触摸检测,但没有找到任何有用的东西。每个气球一旦被触摸就会弹出,我只需要检测它是否被触摸。这是我的气球生成代码:
Rendering (x, y, width, and height are randomized):
渲染(x、y、宽度和高度是随机的):
public void render() {
y += 2;
balloon.setX(x);
balloon.setY(y);
balloon.setSize(width, height);
batch.begin();
balloon.draw(batch);
batch.end();
}
Spawning in main game class:
在主游戏类中生成:
addBalloon(new Balloon());
public static void addBalloon(Balloon b) {
balloons.add(b);
}
采纳答案by EpicPandaForce
This is how I did it, but depending on the scene you are using and the elements that can be touched, there can be slightly more optimized ways of doing this:
我是这样做的,但是根据您使用的场景和可以触摸的元素,可以有稍微优化的方法来执行此操作:
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) ;
}
}
回答by Kumar Saurabh
in your class having the render method use can do following code
在您的类中使用 render 方法可以执行以下代码
Vector3 touchPoint=new Vector3();
void update()
{
if(Gdx.input.justTouched())
{
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
}
}
}
回答by Hoodrij
You have Gdx.input.getX()
and Gdx.input.getY()
. The X and Y coordinates of the last touch.
Just compare them with balloon frame.
你有Gdx.input.getX()
和Gdx.input.getY()
。最后一次触摸的 X 和 Y 坐标。只需将它们与气球框架进行比较。
回答by Madmenyo
You could add a tiny 1x1 pixel rectangle to your mouse and check if it intersects or contains other boxes. You do need to use boxes for all your objects you want to make clickable this way.
您可以向鼠标添加一个微小的 1x1 像素矩形,并检查它是否相交或包含其他框。您确实需要为要以这种方式单击的所有对象使用框。
回答by Netero
i made a little class that i use for my games to detect is Sprite is touched
我做了一个小课,我用我的游戏来检测是否触动了雪碧
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;
}
here how i use it
在这里我如何使用它
Gdx.input.setInputProcessor(new GameInputListener() {
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDown(int x, int yy, int pointer, int button) {
int y = Gdx.graphics.getHeight() - yy;
// if sprite + 10 of px marge is touched
if(mySpriteTouchable.isPressing(10, x, y)) {
// sprite is touched down
}
return false;
}
}
with the same logic you can detect sprite releasing and also you can customize it for effect size when the sprite is touched and more...
使用相同的逻辑,您可以检测精灵释放,也可以在触摸精灵时自定义效果大小等等...
hope this was helpfull !
希望这有帮助!
回答by Kareem Hammad
i have a simple solution it's like that create a small rectangle 1 pixel by one pixel
我有一个简单的解决方案,就像创建一个 1 像素一个像素的小矩形一样
Rectangle rect;
Rectangle rect;
rect = new Rectangle(0, 0, 1, 1);
rect = new Rectangle(0, 0, 1, 1);
then make a method called
然后创建一个方法调用
touching_checking();
touching_checking();
in this method we will do three things
在这个方法中,我们将做三件事
first check if screen is touched
首先检查屏幕是否被触摸
second put the rectangle where you touch the screen
第二个把矩形放在你触摸屏幕的地方
then at last check if your rectangle overlaps with the sprite rectangle. like that
然后最后检查您的矩形是否与精灵矩形重叠。像那样
private void touching_checking() {
if (Gdx.input.isTouched()) {
rect.setPosition(Gdx.input.getX(), Gdx.input.getY());
if (rect.overlaps(back.getBoundingRectangle())) {
//do what you want here
}
}
}
i have an array of sprites called levels i check which one i pressed like that
private void touching_checking() {
if (Gdx.input.isTouched()) {
rect.setPosition(Gdx.input.getX(), Gdx.input.getY());
if (rect.overlaps(back.getBoundingRectangle())) {
//do what you want here
}
}
}
我有一组称为级别的精灵,我检查我像那样按下了哪个
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
}
}
}
}
回答by markus schwarzer
Here is what i use in my sprite class. I give it the vector where i clicked camera.unproject(new Vector3().set(x,y,0));
. returns true if clicked in the area of the sprite.
这是我在精灵类中使用的内容。我给它我点击的向量camera.unproject(new Vector3().set(x,y,0));
。如果在精灵区域中单击,则返回 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;
}
}