1

当我点击它时,我想使精灵移动,但我有一个问题......精灵总是去左上角,我试图放置随机值,但它没有给出结果

ballITexture = TextureRegionFactory.extractFromTexture(BaseActivity.getSharedInstance().ballTexture);
final Random rand = new Random();
sprite = new Sprite(0, 0, ballITexture, BaseActivity.getSharedInstance().getVertexBufferObjectManager()){
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
               randomMove(rand.nextInt(4), pTouchAreaLocalX, pTouchAreaLocalY, this);

            };

mCamera = BaseActivity.getSharedInstance().mCamera;
sprite.setPosition(mCamera.getWidth() / 2 - sprite.getWidth() / 2, mCamera.getHeight() / 2 - sprite.getHeight() / 2);
moveable = true;

这是 randomMove 方法:

private void randomMove(int direction, float pTouchAreaLocalX, float pTouchAreaLocalY, Sprite sprite){

    switch(direction){
            case 0:
                sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, 1, pTouchAreaLocalY, 1));
                break;
            case 1:
                sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, 1, pTouchAreaLocalY, -1));
                break;
            case 2:
                sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, -1, pTouchAreaLocalY, 1));
                break;
            case 3:
                sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, -1, pTouchAreaLocalY, -1));
                break;
        }

}

我的问题是,为什么当我触摸它时,我的精灵总是在左上角,而不是在随机方向移动?如何解决?

4

1 回答 1

1

忘记这一点,我将不正确的参数传递给 MoveModifier,我在其中放置 1 和 -1 值应该是相机的任何像素

private void randomMove(int direction, float pTouchAreaLocalX, float pTouchAreaLocalY, Sprite sprite){

        sprite.registerEntityModifier(new MoveModifier(1, pTouchAreaLocalX, rand.nextInt(mCamera.getWidth()), pTouchAreaLocalY, rand.nextInt(mCamera.getHeight())));
于 2012-10-28T15:55:11.307 回答