用加速度计和发动机移动球

时间:2014-02-26 04:15:48

标签: andengine accelerometer

我在加速度计和发动机的帮助下编写了一个移动球的代码,但是我接近了力量。

我的问题是使用PhysicsWorld。

我的图书馆是:

andengine.jar

andenginephysicsbox2dextension.jar

我的代码是:

public class SnakeGameActivity extends BaseGameActivity implements IAccelerometerListener
{
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private float mGravityX;
private float mGravityY;
private Sprite  splash ;
private final Vector2 mTempVector = new Vector2();
protected PhysicsWorld mPhysicsWorld;

private Camera mCamera;
private Texture mTexture;
private TextureRegion mSplashTextureRegion;
@Override
public Engine onLoadEngine() {
    // TODO Auto-generated method stub
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, 
    new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

}

@Override
public void onLoadResources() {
    // TODO Auto-generated method stub
    this.mTexture = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.enableAccelerometerSensor(this);
    this.mSplashTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this,  "ball_enemy.png", 0, 0);
    this.mEngine.registerUpdateHandler(new FPSLogger());
    this.mEngine.getTextureManager().loadTexture(this.mTexture);
}

@Override
public Scene onLoadScene() {

     this.mEngine.registerUpdateHandler(new FPSLogger());

        Scene scene = new Scene(1); 

            this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
            final int centerX = (CAMERA_WIDTH - this.mSplashTextureRegion.getWidth()) / 2;
        final int centerY = (CAMERA_HEIGHT - this.mSplashTextureRegion.getHeight()) / 2;
    splash = new Sprite(centerX, centerY, this.mSplashTextureRegion);
    scene.getLastChild().attachChild(splash);
            scene.registerUpdateHandler(this.mPhysicsWorld);
    return scene;
}

@Override
public void onLoadComplete() {
    // TODO Auto-generated method stub

}

public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {

             final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
            this.mPhysicsWorld.setGravity(gravity);
            Vector2Pool.recycle(gravity);

}
}

我的日志错误:

02-25 21:08:22.334: E/ActivityManager(69): Start proc com.android.deskclock for
broadcast com.android.deskclock/.AlarmInitReceiver: pid=255 uid=10019 gids={}
CPU usage from 16953ms to 19171ms later:

非常感谢您的帮助

0 个答案:

没有答案