0

I am working on a game. Its all working well, the only thing I need is for my animation to pause when I press the home or on/off button on my phone. As far as I log correct, the pause script (as found here) works well. It keeps incrementing the time and applies it to the animation. However when I resume my game, the animations all resume on the left side of the screen instead of the position it originally was in when I paused it in. This is my stripped code. I really hope someone can help me.

public class GameView extends Activity{
    ImageView peanut;
    TranslateAnim movePeanut;
    AnimationSet animSet = new AnimationSet(true);



    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gameviewlayout);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;

        movePeanut = new TranslateAnim(width,0,0,0);
        movePeanut.setDuration(5000);
        movePeanut.setRepeatCount(-1);
        movePeanut.setRepeatMode(Animation.REVERSE);

        animSet.addAnimation(movePeanut);
        animSet.setFillAfter(true);
        peanut = (ImageView) findViewById(R.id.imgview1);
        peanut.startAnimation(animSet);


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        movePeanut.resume();



    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        movePeanut.pause();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

    }
    public void finish (){
        Intent intent = new Intent (GameView.this, MainActivity.class);
        startActivity(intent);
    }

    public class TranslateAnim extends TranslateAnimation{

        public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta,
                float toYDelta) {
            super(fromXDelta, toXDelta, fromYDelta, toYDelta);
            // TODO Auto-generated constructor stub
        }

        private long mElapsedAtPause=0;
        private boolean mPaused=false;

        @Override
        public boolean getTransformation(long currentTime, Transformation outTransformation) {
            if(mPaused && mElapsedAtPause==0) {
                mElapsedAtPause=currentTime-getStartTime();
            }
            if(mPaused)
                setStartTime(currentTime-mElapsedAtPause);

            boolean returnValue = super.getTransformation(currentTime, outTransformation);


            return returnValue;
        }

        public void pause() {
            mElapsedAtPause=0;
            mPaused=true;
        }

        public void resume() {
            mPaused=false;
        }
    }
}
Community
  • 1
  • 1
  • Here's an answer that suggests a solution for pausing and resuming TranslateAnimations: http://stackoverflow.com/a/10028575/379245 – BVB Feb 21 '14 at 23:27
  • Yes well as you can see I implemented the very same code. That is not where the problem is I think.. – Luuk van Aggelen Feb 22 '14 at 16:09
  • I just wish someone could help me – Luuk van Aggelen Feb 27 '14 at 09:09
  • Here's an idea to try: in onPause save the current coordinates of the view and how much time was left on the animation, let animation complete when the app is paused, then in onResume place the view at those coordinates and start another animation with the length of the saved time. – BVB Feb 27 '14 at 17:45
  • Thanks for the reply. The code I currently have is sort off that.. This is so frustrating, I have spend over 100 hours in fixing this and its never working. Thanks for the help. – Luuk van Aggelen Mar 22 '14 at 20:38

0 Answers0