K
K
krembrule20162019-05-01 11:52:54
Java
krembrule2016, 2019-05-01 11:52:54

How to handle a touch on the screen in the area where the sprite is located?

Hey!
All those involved with the past holidays!
There is a class, when instantiated, a collection appears that contains sprites.
Everything is as usual:
I declare a variable in the class
static ArrayList<Sprite> caterpillar;
I fill in the constructor with sprites with this function:

private void building(){
        //тут будет забивать массив спрайтами
        for( int i = 0; i < numberOfElements; i ++){
            sprite = new Sprite(region);
            sprite.setX(positionX);
            sprite.setY(positionY);
            caterpillar.add(sprite);
            positionY -= 150;
        }
    }

When rendering, everything is tip-top, everything is as it should be.
There is a method that returns this collection.
public static ArrayList<Sprite> getCaterpillar(){return caterpillar;}

In the controller class I'm trying to implement the essence of the issue:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        for(Sprite i : ourCaterpillar){ if(i.getBoundingRectangle().contains(screenX, screenY)) System.out.println("Есть попадание!!"); }
        for(int i = 0 ; i < 8; i ++){
           if(ourCaterpillar.get(i).getBoundingRectangle().contains(screenX,screenY)){ System.out.println("Есть попадание"); }
        }
        System.out.println(ourCaterpillar.size());
        return true;
    }

uh... nothing comes out.
If it matters, then my sprite caterpillar is also moving.
What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
krembrule2016, 2019-05-01
@krembrule2016

The problem turned out to be that despite rendering the sprite larger than it is, the container is looking for points that fill the original size of the sprite.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question