Touch Sprite

  ... discussions about development with the GLES1 branch of AndEngine.

Touch Sprite

Postby squeeb » Wed May 23, 2012 4:08 am

i have an intent that opens a url...
it opens whenever you touch the screen
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         protected void onTap(final int pX, final int pY)
  3.         {
  4.                
  5.                 Intent intent = new Intent(Intent.ACTION_VIEW);                  
  6.                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  7.                 intent.setData(android.net.Uri.parse("http://www.google.com"));
  8.                 startActivity(intent);
  9.         }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

i want to open it when you touch a sprite...
anyone have any idea?
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Touch Sprite

Postby marquinhos » Wed May 23, 2012 5:20 am

When creating the sprite just override onAreaTouched and register the touch area of your sprite in the scene.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final Sprite sprite = new Sprite(float spriteX, float spriteY, TextureRegion spriteTextureRegion) {
  2.             @Override
  3.             public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  4.                 int eventAction = pSceneTouchEvent.getAction();
  5.                 switch (eventAction) {
  6.                     case TouchEvent.ACTION_DOWN:
  7.                         Intent intent = new Intent(Intent.ACTION_VIEW);
  8.                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  9.                         intent.setData(android.net.Uri.parse("http://www.google.com"));
  10.                         startActivity(intent);
  11.                         break;
  12.                     case TouchEvent.ACTION_MOVE:
  13.                         break;
  14.                     case TouchEvent.ACTION_UP:
  15.                         break;
  16.                 }
  17.                 return true;
  18.             }
  19.         };
  20.         scene.attachChild(sprite);
  21.         scene.registerTouchArea(sprite);
  22.         scene.setTouchAreaBindingEnabled(true);
Parsed in 0.052 seconds, using GeSHi 1.0.8.4
marquinhos
 
Posts: 3
Joined: Tue Apr 17, 2012 8:59 pm

Re: Touch Sprite

Postby squeeb » Wed May 23, 2012 6:44 pm

i cant seem to get it right...i have been trying but i keep getting errors here is my load scene... where would that go... i am trying to touch the face sprite to go to url

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         @SuppressWarnings("static-access")
  3.                 public Scene onLoadScene() {
  4.                 mScene = new Scene();
  5.                  this.mAccelerationInitializer = new AccelerationInitializer(300, 0, 0, 0);
  6.                  //this.mVelocityInitializer = new VelocityInitializer(50, 0, 0, 0);
  7.                 //add graphics
  8.                  
  9.                
  10.                  this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
  11.                
  12.                  
  13.                  
  14.                  final Shape ground = new Rectangle(0, CAMERA_HEIGHT +2, CAMERA_WIDTH -2, 2);
  15.                         final Shape roof = new Rectangle(0, -50, CAMERA_WIDTH, 2);
  16.                         final Shape left = new Rectangle(-60, 0, 2, CAMERA_HEIGHT);
  17.                         final Shape right = new Rectangle(CAMERA_WIDTH +60, 0, 2, CAMERA_HEIGHT);
  18. //x, y  ,width, length
  19.                         final Shape leftrim = new Rectangle( 183, 523, 1, 1);
  20.                         final Shape rightrim = new Rectangle( 265, 523, 1, 1);
  21.                        
  22.                         final Shape pole = new Rectangle( 210, 585, 30, 200);
  23.                         final Shape rightnet = new Rectangle( 202, 575, 45, 10);
  24.                        
  25.                         final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  26.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  27.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  28.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  29.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  30.                         final FixtureDef rimFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 10f);
  31.  
  32.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightrim, BodyType.StaticBody, rimFixtureDef);
  33.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftrim, BodyType.StaticBody, rimFixtureDef);
  34.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightnet, BodyType.StaticBody, wallFixtureDef);
  35.                         PhysicsFactory.createBoxBody(this.mPhysicsWorld, pole, BodyType.StaticBody, wallFixtureDef);
  36.  
  37.                        
  38.                         final Sprite face;
  39.                         final Body body;
  40.                         //1st=  , 2nd is bounce, 3rd is friction?
  41.                         final FixtureDef spriteFixtureDef = PhysicsFactory.createFixtureDef(40, 0.0f, 5.0f);
  42.                         face = new Sprite(200, 50, this.mFace);
  43.                         //final FixtureDef spriteFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  44.                         body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, spriteFixtureDef);
  45.                        
  46.  
  47.                        
  48.  
  49.                        
  50.                        
  51.                         this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
  52.                        
  53.                        
  54.                         mScene.attachChild(ground);
  55.                         mScene.attachChild(roof);
  56.                         mScene.attachChild(left);
  57.                         mScene.attachChild(right);
  58.                         mScene.attachChild(rightrim);
  59.                         mScene.attachChild(leftrim);
  60.                         mScene.attachChild(rightnet);
  61.                         mScene.attachChild(pole);
  62.                         mBackgroundSprite = new Sprite(0, 0, this.mBackground);
  63.                         mScene.attachChild(mBackgroundSprite);
  64.                        
  65.                        
  66.                         mhooppolesprite = new Sprite(180, 520, this.mhooppoletexture);
  67.                          mScene.attachChild(mhooppolesprite);
  68.  
  69.                         mScene.registerUpdateHandler(this.mPhysicsWorld);
  70.                        
  71. /*I PUT IT HERE AND CHANGED IT TO MAKE SENSE WITH MY CODE BUT COULDNT GET IT TO WORK... I GOT IT TO COMPILE BUT NOTHING HAPPENED WHEN I TOUCHED THE SPRITE.
  72.                 */
  73.                         mScene.registerTouchArea(face);
  74.                         mScene.setTouchAreaBindingEnabled(true);       
  75.                         this.mScene.attachChild(face);
  76.  
  77.     }
  78.  
  79.  
  80.                  sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
  81.                 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
  82.          
  83.                
  84.                 this.mEngine.registerUpdateHandler(new IUpdateHandler() {
  85.                     @Override
  86.                                 public void onUpdate(float pSecondsElapsed) {
  87.                         updatemSpritePosition();
  88.                     }
  89.          
  90.                     @Override
  91.                                 public void reset() {
  92.                         // TODO Auto-generated method stub
  93.                     }
  94.                 });
  95.        
  96.                
  97.                 return mScene;
  98.         }
Parsed in 0.055 seconds, using GeSHi 1.0.8.4
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Touch Sprite

Postby marquinhos » Wed May 23, 2012 7:55 pm

Try this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     @Override
  2.             @SuppressWarnings("static-access")
  3.                     public Scene onLoadScene() {
  4.                     mScene = new Scene();
  5.                      this.mAccelerationInitializer = new AccelerationInitializer(300, 0, 0, 0);
  6.                      //this.mVelocityInitializer = new VelocityInitializer(50, 0, 0, 0);
  7.                     //add graphics
  8.                      
  9.                    
  10.                      this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
  11.                    
  12.                      
  13.                      
  14.                      final Shape ground = new Rectangle(0, CAMERA_HEIGHT +2, CAMERA_WIDTH -2, 2);
  15.                             final Shape roof = new Rectangle(0, -50, CAMERA_WIDTH, 2);
  16.                             final Shape left = new Rectangle(-60, 0, 2, CAMERA_HEIGHT);
  17.                             final Shape right = new Rectangle(CAMERA_WIDTH +60, 0, 2, CAMERA_HEIGHT);
  18.     //x, y  ,width, length
  19.                             final Shape leftrim = new Rectangle( 183, 523, 1, 1);
  20.                             final Shape rightrim = new Rectangle( 265, 523, 1, 1);
  21.                            
  22.                             final Shape pole = new Rectangle( 210, 585, 30, 200);
  23.                             final Shape rightnet = new Rectangle( 202, 575, 45, 10);
  24.                            
  25.                             final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  26.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  27.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  28.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  29.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  30.                             final FixtureDef rimFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 10f);
  31.      
  32.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightrim, BodyType.StaticBody, rimFixtureDef);
  33.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftrim, BodyType.StaticBody, rimFixtureDef);
  34.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightnet, BodyType.StaticBody, wallFixtureDef);
  35.                             PhysicsFactory.createBoxBody(this.mPhysicsWorld, pole, BodyType.StaticBody, wallFixtureDef);
  36.      
  37.                            
  38.                             final Sprite face;
  39.                             final Body body;
  40.                             //1st=  , 2nd is bounce, 3rd is friction?
  41.                             final FixtureDef spriteFixtureDef = PhysicsFactory.createFixtureDef(40, 0.0f, 5.0f);
  42.                             face = new Sprite(200, 50, this.mFace){
  43.             @Override
  44.             public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  45.                 int eventAction = pSceneTouchEvent.getAction();
  46.                 switch (eventAction) {
  47.                     case TouchEvent.ACTION_DOWN:
  48.                         Intent intent = new Intent(Intent.ACTION_VIEW);
  49.                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  50.                         intent.setData(android.net.Uri.parse("http://www.google.com"));
  51.                         startActivity(intent);
  52.                         break;
  53.                     case TouchEvent.ACTION_MOVE:
  54.                         break;
  55.                     case TouchEvent.ACTION_UP:
  56.                         break;
  57.                 }
  58.                 return true;
  59.             }
  60.         };
  61.                             //final FixtureDef spriteFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  62.                             body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, spriteFixtureDef);
  63.                            
  64.      
  65.                            
  66.      
  67.                            
  68.                            
  69.                             this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
  70.                            
  71.                            
  72.                             mScene.attachChild(ground);
  73.                             mScene.attachChild(roof);
  74.                             mScene.attachChild(left);
  75.                             mScene.attachChild(right);
  76.                             mScene.attachChild(rightrim);
  77.                             mScene.attachChild(leftrim);
  78.                             mScene.attachChild(rightnet);
  79.                             mScene.attachChild(pole);
  80.                             mBackgroundSprite = new Sprite(0, 0, this.mBackground);
  81.                             mScene.attachChild(mBackgroundSprite);
  82.                            
  83.                            
  84.                             mhooppolesprite = new Sprite(180, 520, this.mhooppoletexture);
  85.                              mScene.attachChild(mhooppolesprite);
  86.      
  87.                             mScene.registerUpdateHandler(this.mPhysicsWorld);
  88.                            
  89.     /*I PUT IT HERE AND CHANGED IT TO MAKE SENSE WITH MY CODE BUT COULDNT GET IT TO WORK... I GOT IT TO COMPILE BUT NOTHING HAPPENED WHEN I TOUCHED THE SPRITE.
  90.                     */
  91.                             mScene.registerTouchArea(face);
  92.                             mScene.setTouchAreaBindingEnabled(true);      
  93.                             this.mScene.attachChild(face);
  94.      
  95.         }
  96.      
  97.      
  98.                      sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
  99.                     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
  100.              
  101.                    
  102.                     this.mEngine.registerUpdateHandler(new IUpdateHandler() {
  103.                         @Override
  104.                                     public void onUpdate(float pSecondsElapsed) {
  105.                             updatemSpritePosition();
  106.                         }
  107.              
  108.                         @Override
  109.                                     public void reset() {
  110.                             // TODO Auto-generated method stub
  111.                         }
  112.                     });
  113.            
  114.                    
  115.                     return mScene;
  116.             }
Parsed in 0.059 seconds, using GeSHi 1.0.8.4
marquinhos
 
Posts: 3
Joined: Tue Apr 17, 2012 8:59 pm


Return to GLES1

Who is online

Users browsing this forum: pawanpavvu and 26 guests