francis Escolar
francis Escolar

Reputation: 9

Animation - how to?

I am trying to make a game with three balls going down to the bottom from the top.

I set the duration of the ball in my animation.setDuration to 5000.

But I want to add the speed by 100 whenever the score adds ten.

How do I do this? should I Make a loop?

Btw the code is incomplete because I made it short and concise.

//when Score is 10 I want the speed to minus 100 so that it will go faster therefore its going to be 4900 //when Score is 20 I want the speed to minus 100 therefore its going to be 4800 //when Score is 30 I want the speed to minus 100 therefore its going to be 4700

public class MainActivity extends Activity implements Runnable {

    private ImageView ball1;
    private ImageView ball2;
    private ImageView ball3;
    private TextView mtvscore;
    private int Score = 00;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ball1 = (ImageView) findViewById(R.id.ball1);
        ball2 = (ImageView) findViewById(R.id.ball2);
        ball3 = (ImageView) findViewById(R.id.ball3);
        mtvscore = (TextView) findViewById(R.id.score);
        mtvscore.setText("" + Score);
        mhandle = new Handler();
        mthread = new Thread(this);
        mthread.start();



    private void startBallAnimation() {
        final TranslateAnimation animation = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.86f
        );

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mhandle.post(new Runnable() {
                    @Override
                    public void run() {
                        setBallColors();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(final Animation animation) {
                if(ball1color== WORDS_TO_COLORS.get(word1text) && ball2color== WORDS_TO_COLORS.get(word2text) &&
                        ball3color== WORDS_TO_COLORS.get(word3text)){
                    Score++;
                    sound.playscoreupsound();
                }else {
                    Score--;
                    sound.playgameosound();
                }

                mhandle.post(new Runnable() {
                    @Override
                    public void run() {
                        mtvscore.setText(String.valueOf(Score));
                        setBallColors();
                        restartBallAnimation(animation);
                    }
                });
            }
        });

        restartBallAnimation(animation);
    }

    private void restartBallAnimation(final Animation animation) {
        animation.setDuration(5000);
        ball1.startAnimation(animation);
        ball2.startAnimation(animation);
        ball3.startAnimation(animation);
    }

    private void setBallColors() {
        ball1color = COLORS.get(random.nextInt(COLORS.size()));
        ball2color = COLORS.get(random.nextInt(COLORS.size()));
        ball3color = COLORS.get(random.nextInt(COLORS.size()));

        ball1.setImageDrawable(ballDrawable(ball1color));
        ball2.setImageDrawable(ballDrawable(ball2color));
        ball3.setImageDrawable(ballDrawable(ball3color));
    }




    @Override
    public void run() {
        startBallAnimation();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 64

Answers (1)

PaulCK
PaulCK

Reputation: 1268

Keep in mind that if your score >= 500, you can not see your animation because it is too fast.

private int Score = 0;
private int speed = 5000;

/*
In Java, both two operands (Score  and 10) are integers, so the result will be integer

when
Score = 1 , (5000- (1/10) * 100 ) -> (5000- 0 * 100 ) = 5000
Score = 5, (5000- (5/10) * 100 ) -> (5000- 0 * 100 ) = 5000
Score = 10, (5000- (10/10) * 100 ) -> (5000- 1 * 100 ) = 4900
Score = 25, (5000- (25/10) * 100 ) -> (5000- 2 * 100 ) = 4800
Score = 30, (5000- (30/10) * 100 ) -> (5000- 3 * 100 ) = 4700
Score = 35, (5000- (35/10) * 100 ) -> (5000- 3 * 100 ) = 4700
*/

private void restartBallAnimation(final Animation animation) {
    animation.setDuration(speed - (Score/10) * 100 );
    ball1.startAnimation(animation);
    ball2.startAnimation(animation);
    ball3.startAnimation(animation);
}

Upvotes: 1

Related Questions