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

📄 gametimerthread.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
字号:
// package javagapi;import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.Display;/** * Sends GATimerEvents periodically to a game. */class GameTimerThread extends Thread{    /**     * Used by GASetTimer and GAKillTimer to communicate the new period.     * A killed timer has the period Long.MAX_VALUE.     */    public long period[] = {Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE};    /**     * Used by GASetTimer and GAKillTimer to communicate to the thread the     * fact that the period has been updated.     */    public boolean periodUpdated[] = {false, false, false};    public boolean notQuit = true;    GameShell game;    MIDlet midlet;    /**     * GACheckForRefresh calls repaint() and     * sets this to false. The ensuing call to paint() sets it to true.     * While it is false, the game loop will only steal clock cycles,     * waiting for it to become true, so that the canvas will not repaint     * while the game is handling an event.     */    public boolean paintNotScheduled;    /**     * Allocates a new GameTimerThread object. The purpose of this thread     * is to send all events except GAMenuSettingsEvent and GAStartEvent to     * the game.     * <p>     * As soon as game.gameMain returns false a GATerminateEvent is sent and     * the thread exits after the game has handled this event.     *     * @param g The game that will receive events.     */    public GameTimerThread(GameShell g)    {        game = g;    }    public void setMIDlet(MIDlet m)    {        midlet = m;    }    /**     * Creates and dispatches events to the game. Once the game has begun,     * this thread will actually excecute the game code. There is only one     * GameTimerThread per game.     * <p>     * Please note that this method does a busy wait, i.e. it uses clock cycles     * even when waiting to dispatch the next timer event.     */    public void run()    {        GAEvent curEvent = new GAEvent();        int keyPos = -1;        long sleeptime;        long newT, oldT = System.currentTimeMillis();        int i = 0;        try        {            while (notQuit)            {                newT = System.currentTimeMillis();//                if (paintNotScheduled)//                {                    curEvent.type = GameShell.GATimerEvent;                    curEvent.timerId = 0;                    if (!game.gameMain(curEvent))                        notQuit = false;//                }                sleeptime = period[0]-(System.currentTimeMillis()-newT);                if (sleeptime > 0)                {                    // System.out.println("Went to sleep...");                    try                    {                        // System.out.println("Went to sleep: " + sleeptime);                        // this.sleep(sleeptime);                        this.sleep(sleeptime);                    }                    catch (InterruptedException exc)                    {                        // System.out.println("### Timer " + id + " sleep interrupted.");                    }                }                else                {                    // System.out.println("Didn't go to sleep.");                }            }        }        catch (Exception ex)        {            ex.printStackTrace();        }/*        //System.out.println("MAIN THREAD SENDING TERMINATE EVENT");        curEvent.type = GameShell.GATerminateEvent;        try        {            game.gameMain(curEvent);        }        catch (Exception ex)        {            ex.printStackTrace();        }        Display display = Display.getDisplay(midlet);        MenuReturner mr = new MenuReturner(midlet);        //System.out.println("MAIN THREAD SERIALLY INVOKING MENURETURNER");        display.callSerially(mr); */    }}class MenuReturner implements Runnable{    MIDlet midlet;    public MenuReturner(MIDlet m)    {        midlet = m;    }    public void run()    {        if (midlet instanceof GameMIDlet)            ((GameMIDlet)midlet).returnToMenu();    }}class HighScoreEntry implements Runnable{    MIDlet midlet;    public HighScoreEntry(MIDlet m)    {        midlet = m;    }    public void run()    {        if (midlet instanceof GameMIDlet)            ((GameMIDlet)midlet).enterHighScore();    }}

⌨️ 快捷键说明

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