⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explosion.java

📁 J2ME超级坦克大战源码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public final class  Explosion extends Sprite implements Actor{
    
    /**
     * Explosion should know about the battle field.
     */
    private static BattleField battleField;
    
    /**
     * Explosion should know about the layer manager.
     */
    private static LayerManager layerManager;
    
    /**
     * Small explosion. when bullets hit wall.
     */
    public static final int SMALL = 0;
    
    /**
     * Big explosion, when tank explodes.
     */
    public static final int BIG = 1;
    
    /**
     * The width of the explosion image.
     */
    private static final int WIDTH = 24;
    
    /**
     * The height of the explosion image.
     */
    private static final int HEIGHT = 24;
    
    /**
     * Explosition sequence.
     */
    private static final int[][] FRAME_SEQ = new int[][] {
        { 0, 1, 1,2,2 },
        { 0, 1, 1, 2, 2, 3, 3, 4,4,5,5 },
    };
    
    /**
     * The pool size of explosion.
     */
    private static final int POOL_SIZE = 10;
    
    /**
     * the explosion pool.
     */
    private static Explosion[] EXPLOSIONS_POOL;
    
    /**
     * Initialized the explostion pool.
     */
    static {
        EXPLOSIONS_POOL = new Explosion[POOL_SIZE];
        for (int i = 0; i < POOL_SIZE; ++i)
            EXPLOSIONS_POOL[i] = new Explosion(SMALL);
    }
    
   
    private Explosion(int strength) {
        super(ResourceManager.getInstance().getImage(ResourceManager.EXPLODE),
                WIDTH, HEIGHT);
        defineReferencePixel(WIDTH / 2, HEIGHT / 2);
        setVisible(false);
        setStrength(strength);
    }
    
    
    private void setStrength(int strength) {
        setFrameSequence(FRAME_SEQ[strength]);
    }
    
   
    public static Explosion explode(int x, int y, int strength) {
        for (int i = 0; i < POOL_SIZE; ++i) {
            Explosion explosion = EXPLOSIONS_POOL[i];
            if (!explosion.isVisible()) {
                explosion.setRefPixelPosition(x, y);
                explosion.setFrame(0);
                explosion.setStrength(strength);
                explosion.setVisible(true);
                if(ResourceManager.isPlayingSound){
                    ResourceManager.playSound(ResourceManager.EXPLODE_SOUND);
                }
                return explosion;
            }
        }
        return null;
    }
    
    
    
    public void tick() {
        if (!isVisible())
            return;
        nextFrame();
        if (getFrame() == 0){
            setVisible(false);
        }
    }
    
    
    public static void stopAllExplosions() {
        for (int i = 0; i < POOL_SIZE; i++)
            EXPLOSIONS_POOL[i].setVisible(false);
    }
    
    
    public static void setLayerManager(LayerManager manager) {
        layerManager = manager;
        if (layerManager != null){
            for (int i = 0; i < POOL_SIZE; i++)
                layerManager.append(EXPLOSIONS_POOL[i]);
        }
    }
    
    
    public static void setBattleField(BattleField field) {
        battleField = field;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -