Shaker.java

/*
 * Copyright © 2012 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;

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);
    }
}