Andengine,使精灵随机移动

时间:2012-10-28 15:48:52

标签: android random sprite andengine move

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

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;
        }

}

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

1 个答案:

答案 0 :(得分: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())));