Silentasssasssin
Silentasssasssin

Reputation: 75

Touch Event not working properly..libgdx

I want to execute a function only when the actor on my stage is touched.. But the problem is the function gets executed irrespective of where its touched on the stage.. Even if its touched in some random place on stage the function is executed.. I want the function to execute only when the actor is touched..I have setbounds...Still its not working..

public Restart() 
    {
       atlas = new TextureAtlas(Gdx.files.internal("pages-info.atlas"));
       sprite = atlas.createSprite("restart");
       this.touchable = true;
        sprite.setBounds(x, y, sprite.getWidth(), sprite.getHeight());

    }



public void draw(SpriteBatch batch,float parentAlpha)
    {       
        batch.draw(sprite, x, y , width, height  );

    }



@Override
    public Actor hit(float x, float y) 
    {
        // TODO Auto-generated method stub
        Gdx.app.log( FirstGame.LOG, " restart working " );
        return null;
    }

Upvotes: 0

Views: 1451

Answers (1)

P.T.
P.T.

Reputation: 25177

The hit method is not doing what you think it does. The hit method is for testing if the current actor intersects the given x,y or not, so its always called. (Its useful if you want to throw away "hits" because your actor is not rectangular.)

Use addListener to add an event listener to receive touch events and react to them.

Upvotes: 1

Related Questions