Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.anddev.andengine.engine.Engine;
- import org.anddev.andengine.engine.camera.Camera;
- import org.anddev.andengine.engine.handler.IUpdateHandler;
- import org.anddev.andengine.engine.options.EngineOptions;
- import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
- import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
- import org.anddev.andengine.entity.scene.Scene;
- import org.anddev.andengine.entity.scene.background.ColorBackground;
- import org.anddev.andengine.entity.sprite.Sprite;
- import org.anddev.andengine.entity.util.FPSLogger;
- import org.anddev.andengine.opengl.texture.TextureOptions;
- import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
- import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
- import org.anddev.andengine.opengl.texture.region.TextureRegion;
- import org.anddev.andengine.ui.activity.BaseGameActivity;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- public class MoveSprite extends BaseGameActivity implements SensorEventListener {
- // ===========================================================
- // Constants
- // ===========================================================
- private static final int CAMERA_WIDTH = 800;
- private static final int CAMERA_HEIGHT = 480;
- // ===========================================================
- // Fields
- // ===========================================================
- private Sprite sprite;
- private Camera mCamera;
- private BitmapTextureAtlas mTexture;
- private TextureRegion mSpriteTextureRegion;
- private SensorManager sensorManager;
- private int accellerometerSpeedX;
- private int accellerometerSpeedY;
- private int sX, sY; // Sprite coordinates
- // ===========================================================
- // Constructors
- // ===========================================================
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- // ===========================================================
- // Methods for/from SuperClass/Interfaces
- // ===========================================================
- public Engine onLoadEngine() {
- 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));
- }
- public void onLoadResources() {
- this.mTexture = new BitmapTextureAtlas(32, 32,
- TextureOptions.BILINEAR_PREMULTIPLYALPHA);
- this.mSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory
- .createFromAsset(this.mTexture, this, "gfx/face_box.png", 0, 0);
- this.mEngine.getTextureManager().loadTexture(this.mTexture);
- }
- @SuppressWarnings("static-access")
- public Scene onLoadScene() {
- sensorManager = (SensorManager) this
- .getSystemService(this.SENSOR_SERVICE);
- sensorManager.registerListener(this, sensorManager
- .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
- sensorManager.SENSOR_DELAY_GAME);
- this.mEngine.registerUpdateHandler(new FPSLogger());
- this.mEngine.registerUpdateHandler(new IUpdateHandler() {
- public void onUpdate(float pSecondsElapsed) {
- updateSpritePosition();
- }
- public void reset() {
- // TODO Auto-generated method stub
- }
- });
- final Scene scene = new Scene();
- scene.setBackground(new ColorBackground(0.2f, 0.3f, 0.4f));
- sX = (CAMERA_WIDTH - this.mSpriteTextureRegion.getWidth()) / 2;
- sY = (CAMERA_HEIGHT - this.mSpriteTextureRegion.getHeight()) / 2;
- sprite = new Sprite(sX, sY, this.mSpriteTextureRegion);
- sprite.setScale(1);
- scene.attachChild(sprite);
- return scene;
- }
- public void onLoadComplete() {
- }
- public void onSensorChanged(SensorEvent event) {
- synchronized (this) {
- switch (event.sensor.getType()) {
- case Sensor.TYPE_ACCELEROMETER:
- accellerometerSpeedX = (int) event.values[1];
- accellerometerSpeedY = (int) event.values[0];
- break;
- }
- }
- }
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- //
- }
- // ===========================================================
- // Methods
- // ===========================================================
- private void updateSpritePosition() {
- if ((accellerometerSpeedX != 0) || (accellerometerSpeedY != 0)) {
- // Set the Boundary limits
- int tL = 0;
- int lL = 0;
- int rL = CAMERA_WIDTH - (int) sprite.getWidth();
- int bL = CAMERA_HEIGHT - (int) sprite.getHeight();
- // Calculate New X,Y Coordinates within Limits
- if (sX >= lL)
- sX += accellerometerSpeedX;
- else
- sX = lL;
- if (sX <= rL)
- sX += accellerometerSpeedX;
- else
- sX = rL;
- if (sY >= tL)
- sY += accellerometerSpeedY;
- else
- sY = tL;
- if (sY <= bL)
- sY += accellerometerSpeedY;
- else
- sY = bL;
- // Double Check That New X,Y Coordinates are within Limits
- if (sX < lL)
- sX = lL;
- else if (sX > rL)
- sX = rL;
- if (sY < tL)
- sY = tL;
- else if (sY > bL)
- sY = bL;
- sprite.setPosition(sX, sY);
- }
- }
- // ===========================================================
- // Inner and Anonymous Classes
- // ===========================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement