Shaker.java

/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.explonoid.effects;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Shaker effect for shaking the UI view. 
 * 
 * The member variables magnitudeX and magnitudeY are modified
 * in a TimerTask to control the amount of shaking. For usage, take
 * a look at the Game class' paint() method.
 * 
 * @see com.nokia.example.explonoid.game.Game
 */
public class Shaker {

    public int magnitudeX = 0;
    public int magnitudeY = 0;
    private Timer shaker;

    public void shake(int quakeX, int quakeY) {
        // Interference
        magnitudeX += quakeX;
        magnitudeY += quakeY;
        shaker = new Timer();
        TimerTask shake = new TimerTask() {

            public void run() {
                magnitudeX *= -0.9;
                magnitudeY *= -0.9;
                if (magnitudeX == 0 && magnitudeY == 0) {
                    this.cancel();
                }
            }
        };
        shaker.schedule(shake, 0, 50);
    }
}