Spark.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;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class Spark
        extends Sprite {

    public static final int COLS = 20;
    public static final int WIDTH = 16;
    public static final int HEIGHT = 16;
    private Timer timer;
    private int dirX = 0;
    private int dirY = 0;

    public Spark(Image spark) {
        super(spark, spark.getWidth() / COLS, spark.getHeight());

        int radius = spark.getHeight() / 2;
        defineReferencePixel(radius, radius);

        timer = new Timer();
        TimerTask fade = new TimerTask() {

            public void run() {
                if (getFrame() < getRawFrameCount() - 1) {
                    move(dirX, dirY);
                    nextFrame();
                }
                else {
                    setVisible(false);
                }
            }
        };
        timer.schedule(fade, 0, 50);
    }

    public void shoot(int posX, int posY, int dirX, int dirY) {
        setPosition(posX, posY);
        this.dirX = dirX;
        this.dirY = dirY;
        setFrame(0);
        setVisible(true);
    }
}