Android 演员不接触libGDX
我正在尝试处理舞台上一个演员的触摸。以下是我编写的代码:Android 演员不接触libGDX,android,libgdx,Android,Libgdx,我正在尝试处理舞台上一个演员的触摸。以下是我编写的代码: public class MyGame extends ApplicationAdapter { private GameStage gameStage; //Game stage is custom stage class. @Override public void create () { gameStage = new GameStage(); Gdx.input.se
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
类中:
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类使用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.
}
公共类MyActor扩展了Actor
{
公共MyActor(int startX、int startY、int startWidth、int startHeight、int endX、int speed)
{
TextureAtlas TextureAtlas=新的TextureAtlas(Gdx.files.internal(Constants.ATLAS_PATH));
TextureRegion[]runningFrames=新的TextureRegion[常数.MOVING_REGION_NAMES.length];
对于(int i=0;i
我做错什么了吗?为什么我没有接触到演员?我想出了解决办法。问题是,
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
}