演员不接触libGDX

时间:2015-05-23 09:51:13

标签: android 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班级:

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类使用精灵图像初始化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.
}

我在这里做错了吗?为什么我没有接触演员?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题是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
}