Guest User

Untitled

a guest
Nov 28th, 2012
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.game.matematika.dasar;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5.  
  6. import org.andengine.engine.camera.Camera;
  7. import org.andengine.engine.handler.IUpdateHandler;
  8. import org.andengine.engine.options.EngineOptions;
  9. import org.andengine.engine.options.ScreenOrientation;
  10. import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
  11. import org.andengine.entity.scene.IOnSceneTouchListener;
  12. import org.andengine.entity.scene.Scene;
  13. import org.andengine.entity.sprite.AnimatedSprite;
  14. import org.andengine.entity.sprite.Sprite;
  15. import org.andengine.input.touch.TouchEvent;
  16. import org.andengine.opengl.texture.ITexture;
  17. import org.andengine.opengl.texture.bitmap.BitmapTexture;
  18. import org.andengine.opengl.texture.region.TextureRegion;
  19. import org.andengine.opengl.texture.region.TextureRegionFactory;
  20. import org.andengine.opengl.texture.region.TiledTextureRegion;
  21. import org.andengine.ui.activity.SimpleBaseGameActivity;
  22. import org.andengine.util.adt.io.in.IInputStreamOpener;
  23.  
  24. import android.hardware.Sensor;
  25. import android.hardware.SensorEvent;
  26. import android.hardware.SensorEventListener;
  27. import android.hardware.SensorManager;
  28.  
  29. public class MatematikaDasarActivity extends SimpleBaseGameActivity implements
  30.         IOnSceneTouchListener, SensorEventListener {
  31.     private static final float lebar = 240;
  32.     private static final float tinggi = 320;
  33.     private Camera cam;
  34.     private TextureRegion trBG, trLakon;
  35.     private ITexture itBG, itLakon;
  36.     private Scene scene;
  37.     private boolean touch = false;
  38.     private float posX, posY;
  39.     private Sprite sBG, sLakon;
  40.     private SensorManager smGoyang;
  41.     private TiledTextureRegion ttrLakon;
  42.     private AnimatedSprite asLakon;
  43.  
  44.     @Override
  45.     public EngineOptions onCreateEngineOptions() {
  46.         this.cam = new Camera(0, 0, lebar, tinggi);
  47.         return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
  48.                 new RatioResolutionPolicy(lebar, tinggi), this.cam);
  49.     }
  50.  
  51.     @Override
  52.     protected void onCreateResources() {
  53.         try {
  54.             itBG = new BitmapTexture(getTextureManager(),
  55.                     new IInputStreamOpener() {
  56.  
  57.                         @Override
  58.                         public InputStream open() throws IOException {
  59.                             return getAssets().open("gfx/bg.jpg");
  60.                         }
  61.                     });
  62.             itLakon = new BitmapTexture(getTextureManager(),
  63.                     new IInputStreamOpener() {
  64.  
  65.                         @Override
  66.                         public InputStream open() throws IOException {
  67.                             return getAssets().open("gfx/lakon.png");
  68.                         }
  69.                     });
  70.             itBG.load();
  71.             itLakon.load();
  72.  
  73.         } catch (IOException ioe) {
  74.             ioe.printStackTrace();
  75.         }
  76.         this.trBG = TextureRegionFactory.extractFromTexture(itBG);
  77.         this.trLakon = TextureRegionFactory.extractFromTexture(itLakon);
  78.  
  79.         this.ttrLakon = TextureRegionFactory.extractTiledFromTexture(itLakon,
  80.                 3, 2);
  81.     }
  82.  
  83.     @Override
  84.     protected Scene onCreateScene() {
  85.         this.mEngine.registerUpdateHandler(new IUpdateHandler() {
  86.  
  87.             @Override
  88.             public void reset() {
  89.  
  90.             }
  91.  
  92.             @Override
  93.             public void onUpdate(float f) {
  94.                 if (touch) {
  95.                     sLakon.setPosition(posX, posY);
  96.                 }
  97.             }
  98.         });
  99.  
  100.         this.smGoyang = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  101.         this.smGoyang.registerListener(this,
  102.                 smGoyang.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  103.                 SensorManager.SENSOR_DELAY_GAME);
  104.  
  105.         scene = new Scene();
  106.         sBG = new Sprite(0, 0, this.trBG, this.getVertexBufferObjectManager());
  107.         sLakon = new Sprite(0, 0, this.trLakon,
  108.                 this.getVertexBufferObjectManager());
  109.  
  110.         this.asLakon = new AnimatedSprite(0, 0, 96, 50, this.ttrLakon,
  111.                 this.getVertexBufferObjectManager());
  112.         this.asLakon.animate(new long[] { 100, 100 }, 0, 3, true);
  113.  
  114.         sBG.setPosition(posX, posY);
  115.         scene.attachChild(sBG);
  116.         scene.attachChild(sLakon);
  117.  
  118.         scene.attachChild(asLakon);
  119.  
  120.         scene.setOnSceneTouchListener(this);
  121.         scene.setTouchAreaBindingOnActionMoveEnabled(true);
  122.         return scene;
  123.     }
  124.  
  125.     @Override
  126.     public boolean onSceneTouchEvent(Scene s, TouchEvent te) {
  127.         if (te.getAction() == TouchEvent.ACTION_MOVE) {
  128.             posX = (sLakon.getX() + sLakon.getWidth()) / 2;
  129.             posY = (sLakon.getY() + sLakon.getHeight()) / 2;
  130.             touch = true;
  131.         } else {
  132.             posX = 0;
  133.             posY = 0;
  134.         }
  135.         return touch;
  136.     }
  137.  
  138.     @Override
  139.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  140.     }
  141.  
  142.     @Override
  143.     public void onSensorChanged(SensorEvent event) {
  144.         synchronized (this) {
  145.             switch (event.sensor.getType()) {
  146.             case Sensor.TYPE_ACCELEROMETER:
  147.                 if (event.values[0] > 0 && sLakon.getX() < 240) {
  148.                     posX = sLakon.getX() + event.values[0];
  149.                     posY = sLakon.getY() + event.values[1];
  150.                 } else if (event.values[0]  240) {
  151.                     posX = -(sLakon.getX() + event.values[0]);
  152.                     posY = sLakon.getY() + event.values[1];
  153.                 }
  154.                 break;
  155.             default:
  156.                 break;
  157.             }
  158.         }
  159.     }
  160. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×