演员没有接触 libGDX
Actor not taking touch libGDX
我正在尝试处理舞台上 Actor
之一的触摸。以下是我写的代码:
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();
}
}
现在在 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);
}
}
自定义 actor class 使用 sprite 图像初始化 actor。
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.
}
我是不是做错了什么?为什么我没有接触到演员?
我想出了解决办法。问题是 Listeners
只有在我们为 Actor
设置了界限后才能工作。我没有设置任何界限,所以它没有进行接触。现在我在 draw()
方法中设置了边界(这样它会在每次演员移动时更新)并且它工作得很好。
@Override
public void draw(Batch batch, float parentAlpha)
{
super.draw(batch, parentAlpha);
setBounds(currentAnimationBounds.x, currentAnimationBounds.y, currentAnimationBounds.width, currentAnimationBounds.height);
//Rest of the code
}
我正在尝试处理舞台上 Actor
之一的触摸。以下是我写的代码:
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();
}
}
现在在 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);
}
}
自定义 actor class 使用 sprite 图像初始化 actor。
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.
}
我是不是做错了什么?为什么我没有接触到演员?
我想出了解决办法。问题是 Listeners
只有在我们为 Actor
设置了界限后才能工作。我没有设置任何界限,所以它没有进行接触。现在我在 draw()
方法中设置了边界(这样它会在每次演员移动时更新)并且它工作得很好。
@Override
public void draw(Batch batch, float parentAlpha)
{
super.draw(batch, parentAlpha);
setBounds(currentAnimationBounds.x, currentAnimationBounds.y, currentAnimationBounds.width, currentAnimationBounds.height);
//Rest of the code
}