简体   繁体   中英

Libgdx Actor doesn't respond to input

I defined new actor this way:

class MyActor extends Image
{
    Texture texMyActor;

    public MyActor()
    {
        super.setTouchable(Touchable.enabled);
        texMyActor = new Texture("data/myActor.png");
        addListener(new InputListener()
        {
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button)
            {
                System.out.println("down");
                return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button)
            {
                System.out.println("up");
            }
        });
    }

    @Override
    public void act(float delta)
    {
        super.act(delta);
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha)
    {
        batch.draw(texMyActor, 200, 200);
    }
}

I also added actor to stage, registered stage as current input processor. Then I call stage.act(deltaTime) and stage.draw(). I see my actor but it doesn't respont to input What's wrong with my code? :?:

You have to setBounds of your actor as well add to your constructor :

texMyActor = new Texture("data/myActor.png");
//setting width and height according to the texture
setWidth(texMyActor.getWidth());
setHeight(texMyActor.getHeight());
//setting bound of the actor
setBounds(200, 200, getWidth(), getHeight());

notice i set the bounds position to 200 , 200 since you draw there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM