Android : Move ball on andengine

on Wednesday, September 10, 2014


I have seen the example movingBall of andengine but I want to understand how to manage the values ​​that assume the ball. For example if I wanted to move only on the X axis, how should I do? Thanks



@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT);

return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT), camera);
}

@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 0, 2, 1);
this.mBitmapTextureAtlas.load();
}

@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

final float centerX = (MovingBallExample.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (MovingBallExample.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
final Ball ball = new Ball(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());

scene.attachChild(ball);

return scene;
}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

private static class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;

public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
this.mPhysicsHandler.setVelocity(MovingBallExample.DEMO_VELOCITY, MovingBallExample.DEMO_VELOCITY);
}

@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if(this.mX < 0) {
this.mPhysicsHandler.setVelocityX(MovingBallExample.DEMO_VELOCITY);
} else if(this.mX + this.getWidth() > MovingBallExample.CAMERA_WIDTH) {
this.mPhysicsHandler.setVelocityX(-MovingBallExample.DEMO_VELOCITY);
}

if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(MovingBallExample.DEMO_VELOCITY);
} else if(this.mY + this.getHeight() > MovingBallExample.CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-MovingBallExample.DEMO_VELOCITY);
}

super.onManagedUpdate(pSecondsElapsed);
}
}


I posted the code so we can better understand. Thanks


0 comments:

Post a Comment