2012-09-19 43 views
13

Tengo dificultades para obtener eventos que funcionen con mi Actor en libgdx. Estoy usando versiones nocturnas.No se pueden obtener eventos que funcionen en mi libgdx Actor

Mi etapa se configura en el método de una subclase Screenshow():

stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); 
Gdx.input.setInputProcessor(stage); 
TestActor actor = new TestActor(); 
stage.addActor(actor); 

Y mi clase el actor se ve así:

class TestActor extends Actor { 
    private Sprite sprite; 
    private TextureAtlas atlas; 

    public TestActor() { 
     atlas = new TextureAtlas(Gdx.files.internal("textures/images-packed.atlas")); 
     sprite = atlas.createSprite("logo-96"); 

     setTouchable(Touchable.enabled); 
     addListener(new InputListener() { 
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
       Gdx.app.debug(TestGame.TAG, "TestActor.touchDown()"); 
       return true; // must return true for touchUp event to occur 
      } 
      public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
       Gdx.app.debug(TestGame.TAG, "TestActor.touchUp()"); 
      } 
     }); 
    } 

    @Override 
    public void draw(SpriteBatch batch, float parentAlpha) { 
     Color color = getColor(); 
     batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); 
     batch.draw(sprite, getX(), getY()); 
    }    
} 

no parecen Los eventos para disparar. Por extraño que parezca, he usado widgets UI integrados como el TextButton y puedo hacer que esos eventos funcionen bien. ¿Alguien puede ver lo que estoy haciendo mal?

Respuesta

16

También debe establecerBounds a su actor. mejor manera de hacerlo (si quieres el mismo tamaño que su textura) añadir estas líneas a su constructor:

setWidth(sprite.getWidth()); 
setHeight(sprite.getHeight()); 
setBounds(0, 0, getWidth(), getHeight()); 

aviso que también puede configurar la ubicación de los límites con los 2 primeros parámetros.

+0

Sí! ¡Gracias! Me preguntaba si tenía algo parecido. –

Cuestiones relacionadas