Make sprite grow while screen touched

  ... in the unlikely case you discovered a bug, post it here.

Make sprite grow while screen touched

Postby rphello101 » Sun Jun 17, 2012 8:46 am

I'm fairly new to AndEngine. I'm trying to make a sprite grow while the screen is being touched, then fall to the bottom of the screen using the physics extension. I'm having a few problems with it and was hoping someone could help me out. First off, I attempted to use a thread to scale my sprite. I do not think this is the proper way. Would an UpdateHandler or LoopModifier be better? If so, how do I use them? Also, my sprites do not register physics connector when the screen is let up. Why is that? Lastly, I have this annoying single sprite at the position (0,0). How do I get rid of it? My code follows:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestGFXActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener{
  2.     private Camera camera;
  3.     private static final int Camera_Width = 512;
  4.     private static final int Camera_Height = 512;
  5.     final float growRate = (float) .1;
  6.     int centerX, centerY;
  7.     float scale;
  8.     Runnable growCircle;
  9.     ScaleAtModifier entityModifier;
  10.     boolean up;
  11.  
  12.    
  13.     private BitmapTextureAtlas backgroundTextureAtlas;
  14.     private ITextureRegion backgroundTextureRegion, circleTR;
  15.     private PhysicsWorld mPhysicsWorld;
  16.     private Scene scene;
  17.     private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  18.  
  19.         @Override
  20.         public EngineOptions onCreateEngineOptions() {
  21.                 camera = new Camera(0,0,Camera_Width,Camera_Height);
  22.                 EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
  23.                                 new FillResolutionPolicy(), camera);
  24.                 return engineOptions;
  25.         }
  26.  
  27.         @Override
  28.         protected void onCreateResources() {
  29.                 backgroundTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(),
  30.                                 512,512,TextureOptions.DEFAULT);
  31.                 backgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bluelavabackground2.png",0,0);
  32.                 circleTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bubble2.png",0,0);
  33.                 backgroundTextureAtlas.load();
  34.         }
  35.  
  36.         @Override
  37.         protected Scene onCreateScene() {
  38.                 this.mEngine.registerUpdateHandler(new FPSLogger());
  39.                 centerX = (int) ((Camera_Width - this.backgroundTextureRegion.getWidth())/2);
  40.                 centerY = (int) ((Camera_Height - this.backgroundTextureRegion.getHeight())/2);
  41.                
  42.                 scene = new Scene();
  43.                 this.scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
  44.                 scene.setBackground(new Background(1,1,1));
  45.                 this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); //Sets x and y acceleration
  46.                
  47.                 final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
  48.                 final Rectangle ground = new Rectangle(0, Camera_Height - 2, Camera_Width, 2, vertexBufferObjectManager);
  49.                 final Rectangle roof = new Rectangle(0, 0, Camera_Height, 2, vertexBufferObjectManager);
  50.                 final Rectangle left = new Rectangle(0, 0, 2, Camera_Height, vertexBufferObjectManager);
  51.                 final Rectangle right = new Rectangle(Camera_Height - 2, 0, 2, Camera_Width, vertexBufferObjectManager);
  52.                
  53.                 final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
  54.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  55.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  56.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  57.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  58.                
  59.                 this.scene.attachChild(ground);
  60.                 this.scene.attachChild(roof);
  61.                 this.scene.attachChild(left);
  62.                 this.scene.attachChild(right);
  63.  
  64.                 this.scene.registerUpdateHandler(this.mPhysicsWorld);
  65.                
  66.                 final Sprite background = new Sprite(centerX,centerY,this.backgroundTextureRegion, getVertexBufferObjectManager());
  67.                 scene.attachChild(background);
  68.                
  69.                 return scene;
  70.         }
  71.  
  72.         @Override
  73.         public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
  74.                 final Sprite face;
  75.                 final Body body;
  76.                
  77.                 if(this.mPhysicsWorld != null) {
  78.                         face = new Sprite(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), this.circleTR, this.getVertexBufferObjectManager());
  79.                         body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
  80.                         if(pSceneTouchEvent.isActionDown()) {
  81.                                 scale = 1;
  82.                                 up = false;
  83.                                
  84.                                 growCircle = new Grow(face);
  85.                                 new Thread(growCircle).start();
  86.                                 return true;
  87.                         }else if(pSceneTouchEvent.isActionUp()){
  88.                                 Log.e("Tag","Screen Up");
  89.                                 up = true;
  90.                                 this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
  91.                         }
  92.                 }
  93.                 return false;
  94.         }
  95.  
  96.         @Override
  97.         public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
  98.                 // TODO Auto-generated method stub
  99.                
  100.         }
  101.  
  102.         @Override
  103.         public void onAccelerationChanged(AccelerationData pAccelerationData) {
  104.                 final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
  105.                 this.mPhysicsWorld.setGravity(gravity);
  106.                 Vector2Pool.recycle(gravity);
  107.         }
  108.        
  109.         public void onResumeGame() {
  110.                 super.onResumeGame();
  111.  
  112.                 this.enableAccelerationSensor(this);
  113.         }
  114.        
  115.         public void onPauseGame() {
  116.                 super.onPauseGame();
  117.  
  118.                 this.disableAccelerationSensor();
  119.         }
  120.        
  121.         public class Grow implements Runnable{
  122.                 private Sprite sprite;
  123.                
  124.                
  125.                 public Grow(Sprite sprite){
  126.                         Log.e("Tag","Made it to Grow");
  127.                         this.sprite = sprite;
  128.                 }
  129.                 @Override
  130.                 public void run() {
  131.                         while(!up){
  132.                                 Log.e("Tag","Made it to Run");
  133.                                 scene.detachChild(sprite);
  134.                                 entityModifier = new ScaleAtModifier(1 /*Durarion*/, scale/*from*/, scale+growRate/*to*/, 0/*scale center X*/, 0 /*scale center Y*/);
  135.                                 scale+=growRate;
  136.                                 sprite.registerEntityModifier(entityModifier);
  137.                                 scene.attachChild(sprite);
  138.                         }
  139.                 }
  140.         }              
  141. }
Parsed in 0.019 seconds, using GeSHi 1.0.8.4
My game: Expansion. Search for "Pratia" on the Play Store!
rphello101
 
Posts: 38
Joined: Sat Jun 16, 2012 10:01 am

Return to Bugs

Who is online

Users browsing this forum: No registered users and 2 guests