Search code examples
androidandenginelayerspritescene

How to change animation frame on the touch events in a scene :- Andengine


I am considerably new to Andengine and i would like some help.

I am facing difficulty understanding the functioning of AnimatedSprite. I have an animatedsprite sitting on a 2D scene. It is animating at 24FPS. What i want is for my animation to change to a specific frame from the sprite sheet when a touch event happens in one side of the scene. For example, if a touch event happens on the right side of the screen, I would like to register the touch position and according to that change my animatedsprite to a specific frame. Is that possible?

Following is my code for rendering my AnimatedSprite

    mMainCharacter = new  AnimatedSprite(centerCharacterX,centerCharacterY,mLevel1CharacterBackTextureRegion,getVertexBufferObjectManager());

    final long[] frameDurations = new long[24];
    Arrays.fill(frameDurations,400);
    mMainCharacter.animate(frameDurations, 0, 23, true);
    scene.getLastChild().attachChild(mMainCharacter);

Also I would also like to know why we use layers in scenes. If there is any documentation or tutorial that explains touch events and scenes, can someone please provide me a link.

Thanks in advance!


Solution

  • Answered by myself and korn3l here: http://www.andengine.org/forums/post36574.html#p36574

    You can use setCurrentTileIndex(pCurrentTileIndex).

    And on layers:

    layers allows you to control the order in which sprites are rendered on your scene. For example, if you have a tank with it's track as one sprite and it's turret as another sprite, you may want to allocate them layers so the track does not overlap the turret.