1
\$\begingroup\$

I have an image and I want to make clickable this image. I have already done this a lot of times, but this time doesn't work. Probably because the image is from a class which extends Image:

public class BitmapFontGenerale extends Image
{

BitmapFont bitmapFont;
String scritta;
private float x;
private float y;

public BitmapFontGenerale(BitmapFont bitmapFont1, String string, float scale)
{
    bitmapFont = bitmapFont1;
    bitmapFont.setColor(Color.BLACK);
    bitmapFont.getData().setScale(scale);
    scritta = string;
}

@Override
public void draw(Batch batch, float parentAlpha)
{
    bitmapFont.draw(batch,scritta,x,y);
}

public void setX(float x)
{
    this.x = x;
}

public void setY(float y)
{
    this.y = y;
}

public float getX()
{
    return x;
}

public float getY()
{
    return y;
}

}

This is the code which I used to set the input:

image.addListener(new ClickListener()
{
    @Override
    public void clicked (InputEvent event, float x, float y)
    {
       Gdx.app.log("CLICKED ", "CLICKED");
    }
});
stage.add(image);
Gdx.input.setInputProcessor(stage);

This code works if image is from Image class and not if it's from a class which extends Image. Note: I need to extend Image class. What's the problem?

\$\endgroup\$
  • \$\begingroup\$ Show us your entire class file please Curio :) Also try logging Gdx.input.getInputProcessor() == stage to see if your stage is your actual input processor. \$\endgroup\$ – Charanor Jul 8 '17 at 12:55
  • \$\begingroup\$ @Charanor done. The stage isn't the actual input processor \$\endgroup\$ – Curio Jul 8 '17 at 12:59
  • \$\begingroup\$ Wait, why are you extending image to draw a font? Just use the Label actor. Also since stage isn't your input processor your problem is 99% that you're using another input processor that overrides the stage one. \$\endgroup\$ – Charanor Jul 8 '17 at 13:05
  • \$\begingroup\$ @Charanor but in fact I added Gdx.input.setInputProcessor(stage); \$\endgroup\$ – Curio Jul 8 '17 at 13:07
  • 1
    \$\begingroup\$ Yes but if you do Gdx.input.setInputProcessor(stage); then Gdx.input.setInputProcessor(somethingElse); your stage won't register input. You can only have one input processor at a time. If you want multiple you need to use an InputMultiplexer. \$\endgroup\$ – Charanor Jul 8 '17 at 13:09
1
\$\begingroup\$

You must set size in constructor. If width and height equals 0, no clicks will be detected

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.