Updating Sprites/Objects - Listeners

  ... tutorials on how to use AndEngine.

Updating Sprites/Objects - Listeners

Postby Mimminito » Tue Aug 17, 2010 6:43 pm

This tutorial is on Sprites/Objects and how you can apply a listener to them so that they update they way you want!

So we are going to cover the following:

1. Basic creation of a Sprite.
2. Attaching a Listener to a Sprite/Object so it can be updated
3. Handling Touch Input with regards to Sprites

So, lets get started. First, you will want to create a new Sprite. So, make sure you have your Texture and Texture Region setup properly, and then create the following function in your class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void createSprite()
  2. {
  3.         final Sprite sprite = new Sprite(100, 100, mYourSpritesTextureRegion);
  4.         this.mEngine.getScene().getTopLayer().addEntity(sprite);
  5. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Remember to change the mYourSpritesTextureRegion Texture Region to the one you are using for your Sprite!
Once you have done this, call this method from your onLoadScene() method, and test out the application. You should see a Sprite on your screen at X=100 and Y=100.

Now, we want to be able to do something with this Sprite. So, lets make it's position update when we Touch and Drag the Sprite!
We are going to have to edit the createSprite() method so that it creates a listener for the Sprite, which will be called when we touch it. Here is the updated code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void createSprite()
  2. {
  3.         final Scene scene = this.mEngine.getScene();
  4.        
  5.         final Sprite sprite = new Sprite(100, 100, mYourSpritesTextureRegion)
  6.         {
  7.                 @Override
  8.                 public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
  9.                                 float pTouchAreaLocalX, float pTouchAreaLocalY)
  10.                 {
  11.                        
  12.                 }
  13.         };     
  14.         scene.registerTouchArea(sprite);
  15.        
  16.         scene.getTopLayer().addEntity(sprite);
  17. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


What we have done, is added an IOnAreaTouchedListener to the Sprite, and registered this within the Scene. So now when we touch the Sprite, the onAreaTouched() method will be called, and we can handle our Sprite updating within there.
So, to make the Sprite follow your finger when you have touched it, add the following code into the Sprites onAreaTouched() method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. sprite.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


If you were to run this code now, the results would look a little weird, as the Sprites position is being allocated with regards to its top left corner. So update the code to the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. sprite.setPosition(pSceneTouchEvent.getX() - this.getWidth()/2, pSceneTouchEvent.getY() - this.getHeight()/2);
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Run the code again, and you should now be able to touch and drag your Sprites around your screen!!!

Hope this helps some people out, I know some people are a little confused as to how to updated a Sprites location etc, so this should help out.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Updating Sprites/Objects - Listeners

Postby russick89 » Tue Aug 17, 2010 6:56 pm

Thankyou very much!!! just what i was looking for!
russick89
 
Posts: 11
Joined: Mon Aug 16, 2010 7:17 pm

Re: Updating Sprites/Objects - Listeners

Postby Mimminito » Tue Aug 17, 2010 6:59 pm

No problem.

if you wanted to say, have a player sprite on your screen, which moved when you touched another part of the screen, then use the onSceneTouchEvent() method (Override it within your class, and Implement the IOnSceneTouchEvent Listener). Then, make sure you have your player Sprite stored within your class, and you can set its position much like I have done in my, but when you touch the scene instead of dragging the sprite.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Updating Sprites/Objects - Listeners

Postby jrd0 » Tue Aug 17, 2010 7:15 pm

Good post Mimminito, just to add if you are wanting to overload a few functions you can extend the Sprite class like so:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private class MySprite extends Sprite {
  2.                
  3.                 public MySprite(float pX, float pY, TextureRegion pTextureRegion) {
  4.                         super(pX, pY, pTextureRegion);
  5.                         // TODO Auto-generated constructor stub
  6.                 }
  7.  
  8.                 @Override
  9.                 protected void onManagedUpdate(float pSecondsElapsed) {
  10.                         // TODO Auto-generated method stub
  11.                         super.onManagedUpdate(pSecondsElapsed);
  12.                 }
  13.  
  14.                 @Override
  15.                 public boolean onAreaTouched(TouchEvent pSceneTouchEvent) {
  16.                         setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
  17.                         return true;
  18.                 }
  19.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


and add the texture like normal

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         final MySprite mySprite = new MySprite(100, 100, texReg);
  2.                 scene.registerTouchArea(mySprite);
  3.                 scene.getTopLayer().addEntity(mySprite);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I use onManagedUpdate() for timed movements etc
jrd0
 
Posts: 37
Joined: Thu Jul 08, 2010 1:01 pm

Re: Updating Sprites/Objects - Listeners

Postby Mimminito » Tue Aug 17, 2010 7:18 pm

Yeah good addition.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Updating Sprites/Objects - Listeners

Postby nicolas » Sun Aug 22, 2010 7:19 am

i have code :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  3.                         if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
  4.                                 this.runOnUpdateThread(new Runnable() {
  5.                                         @Override
  6.                                         public void run() {
  7.                                                 face.setPosition(pSceneTouchEvent.getX()- face.getWidth() / 2, pSceneTouchEvent.getY()- face.getWidth() / 2);
  8.                                         }
  9.                                 });
  10.                         }
  11.                         return true;
  12.         }
  13.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


and other code :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  3.                 if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
  4.                         face.setPosition(pSceneTouchEvent.getX()- face.getWidth() / 2, pSceneTouchEvent.getY()- face.getWidth() / 2);
  5.                 }
  6.                 return true;
  7. }
  8.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Both to code is the same result ... I don't know the difference .
My english is quite bad , sorry .
fan Nicolas --- admin this forum (^^)
nicolas
 
Posts: 22
Joined: Thu Aug 19, 2010 5:40 am
Location: Viet Nam

Re: Updating Sprites/Objects - Listeners

Postby sPOT902 » Wed Aug 25, 2010 8:47 am

I keep getting the error "the local variable sprite may not have been initialized" :S
sPOT902
 
Posts: 37
Joined: Tue Aug 24, 2010 12:48 pm

Re: Updating Sprites/Objects - Listeners

Postby a_g » Wed Aug 25, 2010 7:22 pm

sPOT902 wrote:I keep getting the error "the local variable sprite may not have been initialized" :S


My, not so accurate, explanation would be that you're trying to access "yourself" before you're done creating yourself. Look at jrd0's solution, that'll help you.
a_g
 
Posts: 2
Joined: Tue Aug 24, 2010 11:56 am

Re: Updating Sprites/Objects - Listeners

Postby jrd0 » Wed Aug 25, 2010 9:42 pm

sPOT902 wrote:I keep getting the error "the local variable sprite may not have been initialized" :S


I don't think an error can get more self explanatory than that, maybe you could try initializing the variable?
jrd0
 
Posts: 37
Joined: Thu Jul 08, 2010 1:01 pm

Re: Updating Sprites/Objects - Listeners

Postby syferz » Fri Aug 27, 2010 6:15 pm

jrd0 wrote:
sPOT902 wrote:I keep getting the error "the local variable sprite may not have been initialized" :S


I don't think an error can get more self explanatory than that, maybe you could try initializing the variable?


hmm for learning purposes, why does it not make you do this when you have it use the original code? I'm too new at Android to really understand much, but from my experience with this Tutorial, I had to comment out the @Override just to get it to display this message, before that it said that the ontouch event had to override or impliment a super.

So I'm lost on this, but somehow I can get this event to work without the engine in basic android. I really like the engine and am trying to use it, because it would help me a great deal, not with only this game (spacetrek), but also future games.
syferz
 
Posts: 10
Joined: Fri Aug 27, 2010 6:06 pm

Next

Return to Tutorials

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 39 guests