[英]How do I detect if a sprite was touched in Java libGDX?
在過去的幾天里,我一直將我的游戲( Apopalypse )移植到Android移動平台。 我已經在Google上快速搜索了精靈觸摸檢測,但沒有找到任何有用的信息。 一旦觸摸,每個氣球都會彈出,我只需要檢測它是否被觸摸過。 這是我的氣球產卵代碼:
渲染(x,y,寬度和高度隨機化):
public void render() {
y += 2;
balloon.setX(x);
balloon.setY(y);
balloon.setSize(width, height);
batch.begin();
balloon.draw(batch);
batch.end();
}
在主要游戲類中產卵:
addBalloon(new Balloon());
public static void addBalloon(Balloon b) {
balloons.add(b);
}
在您的類中使用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
}
}
}
這就是我這樣做的方式,但根據您使用的場景和可觸摸的元素,可以采用稍微更優化的方式:
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) ;
}
}
我做了一個小課程,我用我的游戲來檢測Sprite是否被觸及
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;
}
我在這里如何使用它
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;
}
}
使用相同的邏輯,您可以檢測精靈釋放,也可以在觸摸精靈時自定義效果大小等等...
希望這是有幫助的!
你有Gdx.input.getX()
和Gdx.input.getY()
。 最后一次觸摸的X和Y坐標。 只需將它們與氣球框架進行比較
您可以在鼠標上添加一個小的1x1像素矩形,並檢查它是否相交或包含其他框。 您需要使用方框來為您想要以這種方式點擊的所有對象。
我有一個簡單的解決方案,就像創建一個像素一個像素的小矩形
Rectangle rect;
rect = new Rectangle(0, 0, 1, 1);
然后制作一個叫做的方法
touching_checking();
在這種方法中,我們將做三件事
首先檢查屏幕是否被觸摸
第二個將矩形放在觸摸屏幕的位置
然后最后檢查你的矩形是否與精靈矩形重疊。 像那樣
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.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
}
}
}
}
這是我在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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.