当我点击它时,我想使精灵移动,但我有一个问题......精灵总是去左上角,我试图放置随机值,但它没有给出结果
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;
}
}
我的问题是,为什么当我触摸它时,我的精灵总是在左上角,而不是在随机方向移动?如何解决?