I am trying to handle the touch of one of the Actor
s in the stage. The following is the code I have written:
public class MyGame extends ApplicationAdapter
{
private GameStage gameStage; //Game stage is custom stage class.
@Override
public void create ()
{
gameStage = new GameStage();
Gdx.input.setInputProcessor(gameStage); //Set the input processor
}
@Override
public void dispose()
{
super.dispose();
gameStage.dispose();
}
@Override
public void render ()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.act(Gdx.graphics.getDeltaTime());
gameStage.draw();
}
}
Now in the GameStage
class:
public class GameStage extends Stage implements ContactListener
{
MyActor myActor; //Custom actor object.
public GameStage()
{
super(new ScalingViewport(Scaling.fill, Constants.APP_WIDTH, Constants.APP_HEIGHT, new OrthographicCamera(Constants.APP_WIDTH, Constants.APP_HEIGHT)));
setUpWorld(); //Code to setup the world. added background and other actors. none of them are touchable.
addActor();
}
private void addActor()
{
myActor = new MyActor(100, 100, 100, 100, 1000, 0);
myActor.setTouchable(Touchable.enabled);
myActor.addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button)
{
actorTouched(); //This method is not getting triggered becaused the call never comes in this function.
return true;
}
});
addActor(myActor);
}
}
The custom actor class initializes the actor with a sprite image.
public class MyActor extends Actor
{
public MyActor(int startX, int startY, int startWidth, int startHeight, int endX, int speed)
{
TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal(Constants.ATLAS_PATH));
TextureRegion[] runningFrames = new TextureRegion[Constants.MOVING_REGION_NAMES.length];
for (int i = 0; i < Constants.MOVING_REGION_NAMES.length; i++)
{
String path = Constants.MOVING_REGION_NAMES[i];
runningFrames[i] = textureAtlas.findRegion(path);
if (horizontalMovingDirection == MovementDirection.Right)
{
runningFrames[i].flip(true, false);
}
}
}
//code to draw and animate in a straight line by overriding the draw and act methods.
}
Am I doing something wrong here? Why am I not getting the touch in the actor?