我是使用Andengine框架进行Android编程的初学者。我在互联网上学习教程。我不知道我误解了PhysicsHandler中Scence和Velocity属性的根位置。
下面:
如果我的意见是真的。那么,如何设置PhysicsHandler的Velocity属性来跟随Scene的位置?
这是我的例子:
我的代码:
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(Scene pScene,
TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
positionUpX = pSceneTouchEvent.getX();
positionUpY = pSceneTouchEvent.getY();
ball = new Ball(positionDownX, positionDownY, positionUpX,
positionUpY, mFaceTextureRegion,
getVertexBufferObjectManager());
scene.attachChild(ball);
return true;
}
if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
positionDownX = pSceneTouchEvent.getX();
positionDownY = pSceneTouchEvent.getY();
return true;
}
return false;
}
});
private static class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;
public Ball(final float pDownX, final float pDownY, //Start
final float pUpX,final float pUpY, //End
final TiledTextureRegion pTextureRegion,
final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pDownX, pDownY, pTextureRegion, pVertexBufferObjectManager);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
this.mPhysicsHandler.setVelocity(pUpX, pUpY);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if (this.mX < 0) {
this.mPhysicsHandler.setVelocityX(BALL_VELOCITY);
} else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
this.mPhysicsHandler.setVelocityX(-BALL_VELOCITY);
}
if (this.mY < 0) {
this.mPhysicsHandler.setVelocityY(BALL_VELOCITY);
} else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-BALL_VELOCITY);
}
super.onManagedUpdate(pSecondsElapsed);
}
}