JohnRaja
JohnRaja

Reputation: 2357

Thread.sleep for animation (Andengine)

I am using sprite and Animated sprite. I used Animated sprite for 16 frames. For left,right,up,down and last 4 frames blast animation.

//animSprite.setCurrentTileIndex(leftCount);

When animated Sprite collide with sprite .. I want to show last 4 frames for 5 sec. How to make using andengine. I tried with Thread.sleep but its not working.

//For update i am using

scene.registerUpdateHandler(new IUpdateHandler() {
            @Override 
            public void reset() {

            }

            @Override
            public void onUpdate(final float pSecondsElapsed) {    
              checkCollision();
}

I able get last 4 frames number in logcat. But screen not updated..

How to refresh the scene .

checkCollision()

{

if(sprite.collidesWith(animSprite))

  try
                   {

                    for (int i = 0; i < 4; i++) {                                         

                        animSprite.setCurrentTileIndex(animBlast);                    

                        Log.v("balst",""+animBlast);

                         animBlast++;

                         if (animBlast> 15) {

                             animBlast= 12;

                         }

                         Thread.sleep(10);

                    }

                   }
                   catch (Exception e) {

                       // e.printStackTrace();

        }
}

Upvotes: 0

Views: 2698

Answers (2)

Eloff
Eloff

Reputation: 688

You should not use Thread.sleep() in the update thread, since that will block all other updates and redrawing. Instead look into the AnimatedSprite.animate() methods as Egor said.

If you want something similar to sleep, then use the TimerHandlers in AndEngine. Change false to true to get a repeating timer.

TimerHandler my_timer = new TimerHandler(10, false,
    new ITimerCallback() {
        @Override
        public void onTimePassed(final TimerHandler pTimerHandler) {
            // Do your thing    
        }
});
scene.registerUpdateHandler(my_timer);

Upvotes: 2

Egor
Egor

Reputation: 40193

For AnimatedSprite you have an sprite.animate(long[] pFrameDurations, int pFirstTileIndex, int pLastTileIndex, int loopCount) method, that does a loopCount of loops of frame changes from pFirstTileIndex to pLastTileIndex with frame durations also defined. I feel this is what you're looking for.

Upvotes: 2

Related Questions