How to detect if an enemy was touched ?

Forum for the Android Game Development Workshop. See

How to detect if an enemy was touched ?

Postby Mr Zero » Fri Oct 21, 2016 12:13 pm

I want to add to the score of my game +1 when an enemy was touched, I tried two methods "addListener" and "touchDown" but not worked for me or I didn't use them right , However "Gdx.input.isTouched()" worked but if i touch the right side of the shape that's mean there is a coordinate problem .

How can I do that my (enemy object is linked to an userData and Actor classes, I regroup many different sizes for my enemy in an enum class also those enemies move from the top of the screen to bot , this code is based from William Mora Tutorial ). please How to detect if an enemy was touched ? (sorry for my english)

my GameStage Code :

Code: Select all
public class GameStage extends Stage  implements InputProcessor {
    // This will be our viewport measurements while working with the debug renderer
    private static final int VIEWPORT_WIDTH = 12;
    private static final int VIEWPORT_HEIGHT = 20;

    private World world;
    private Ground ground;
    private Enemy enemy;
    private final float TIME_STEP = 1 / 300f;
    private float accumulator = 0f;
    private Rectangle bounds;
    private Vector3 touchPoint = new Vector3();;
    private int score;
    private String yourScoreName;
    BitmapFont yourBitmapFontName;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private Box2DDebugRenderer renderer;

    public GameStage() {
        world = WorldUtils.createWorld();
        renderer = new Box2DDebugRenderer();
        batch = new SpriteBatch();
        score = 0;
        yourScoreName = "score: 0";
        yourBitmapFontName = new BitmapFont();

    public void setUpWorld(){
        world = WorldUtils.createWorld();
        bounds = new Rectangle(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());

       // (1) *****using addListener method
       /* enemy.addListener(new InputListener()
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button)
                yourScoreName = "score: " + score;
                return true;


    private void setUpGround(){
        ground = new Ground (WorldUtils.createGround(world));

    private void createEnemy() {
        enemy = new Enemy(WorldUtils.createEnemy(world));
       // enemy.setBounds(enemy.getBody().getPosition().x, enemy.getBody().getPosition().y, enemy.getUserData().getWidth(),  enemy.getUserData().getHeight());


    private void setUpCamera() {
        camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);

    public void act(float delta) {
  // (3) **** isTouched Method

     /*  if (Gdx.input.isTouched()){

           Vector3 tmp=new Vector3(Gdx.input.getX(),Gdx.input.getY(),0);
            Rectangle textureBounds=new Rectangle(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
            // texture x is the x position of the texture
            // texture y is the y position of the texture
            // texturewidth is the width of the texture (you can get it with texture.getWidth() or textureRegion.getRegionWidth() if you have a texture region
            // textureheight is the height of the texture (you can get it with texture.getHeight() or textureRegion.getRegionhHeight() if you have a texture region
                yourScoreName = "score: " + score;

        // Fixed timestep
        accumulator += delta;

        while (accumulator >= delta) {
            world.step(TIME_STEP, 6, 2);
            accumulator -= TIME_STEP;

        //TODO: Implement interpolation


    private void checkEnemy(){
       final Body  body = enemy.getBody();
        UserData userData = enemy.getUserData();

       if (!BodyUtils.enemyInBounds(body,userData)){

  // (2) ****using TouchDown method
        public boolean touchDown(int x, int y, int pointer, int button) {

            // Need to get the actual coordinates
            translateScreenToWorldCoordinates(x, y);

              //  world.destroyBody(enemy.getBody());
                yourScoreName = "score: " + score;
            return super.touchDown(x, y, pointer, button);
    private boolean enemyTouched(float x, float y) {
        return bounds.contains(x, y);

    private void translateScreenToWorldCoordinates(int x, int y) {
        getCamera().unproject(touchPoint.set(x, y, 0));

    public void draw() {
        yourBitmapFontName.setColor(1.0f, 1.0f, 1.0f, 1.0f);
        yourBitmapFontName.draw(batch, yourScoreName, 25, 100);
         renderer.render(world, camera.combined);


Mr Zero
Posts: 1
Joined: Fri Oct 21, 2016 11:53 am

Re: How to detect if an enemy was touched ?

Postby heatherreynolds » Thu Aug 24, 2017 10:50 am

Have you solved this problem?
I am a new game developer and I want to find here useful tips for me this problem?
Posts: 1
Joined: Wed Aug 23, 2017 12:54 pm

Return to Android Game Development Workshop

Who is online

Users browsing this forum: No registered users and 3 guests