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

📄 totfc.java

📁 Critter_Crunch_J2ME 游戏源
💻 JAVA
字号:
/*******************************************************************************
 **
 ** Class: TOTFC (TOTFC.java)
 **
 ** The "TOTFC" class extends the MIDlet and encapsulates the entire
 ** application.
 **
 ******************************************************************************/



// TOTFC header.
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

// the TOTFC class itself.
public class TOTFC extends MIDlet implements Runnable {
   
   /****************************************************************************
    ** Constants
    ***************************************************************************/

   //#if DefaultConfiguration || Nokia_6600 || Nokia_6600_Unobfuscated || Razr
   public static final int FRAMES_PER_SECOND = 12;                // desired FPS.
   //#else
//#    public static final int FRAMES_PER_SECOND = 10;                // desired FPS.
   //#endif
   public static final int SECOND            = 1000;              // millisecond ticks in one second.
   public static final int MILLIS_PER_TICK   = SECOND / 
                                               FRAMES_PER_SECOND; // calculate frames for each second.
   
   public static String    APP_NAME;                              // name of the application.
   public static String    APP_VERSION;                           // version of the application.
   
   
   
   /****************************************************************************
    ** Variables
    ***************************************************************************/

   public        volatile Thread  m_thread;                         // main game thread.
   public        volatile boolean m_paused;                         // flag for keeping track whether the above thread is paused.
   public                 long    m_start_time;                     // start of main game loop.
   public                 long    m_time_taken;                     // amount of time taken to execute a game loop tick.
   public                 Engine  m_engine;                         // reference to the Engine object.
   public static          boolean s_cheats;                         // property for turning on/off the cheats.
   public static          boolean s_sound;                          // property for turning on/off the sound.
   public static          boolean s_vibrate;                        // property for turning on/off the vibrate function.
   public static final    Runtime s_runtime = Runtime.getRuntime(); // start time of the application.

   
   
   /****************************************************************************
    ** 'tors
    ***************************************************************************/

   // manual quit function.
   public final void exit() {
      
      try { 
         Engine.destroySounds();
         destroyApp(false); 
      } 
      catch(Exception e) { System.out.println("Trouble exiting game: " + e.toString()); }
      
      notifyDestroyed();
   }
   
   
   
   /****************************************************************************
    ** Extended Methods
    ***************************************************************************/
   
   // MIDlet -- MIDlet application entry point.
   protected void startApp() throws MIDletStateChangeException { 
      
      if(m_thread == null) {

         // initialize all classes.
         Engine.poke();
         Level.poke();
         Menus.poke();
         Particles.poke();
         Entity.poke();
         Critter.poke();
         Avatar.poke();

         // load MIDlet properties.
         s_cheats = getProperty("Enable-Cheat");
         s_sound = getProperty("Sound-Support");
         s_vibrate = getProperty("Vibrate-Support");
         APP_VERSION = getAppProperty("MIDlet-Version" ).toUpperCase();
         APP_NAME = getAppProperty("MIDlet-Name");

         // initialzie MIDlet.
         m_engine = new Engine(this);
         Display.getDisplay(this).setCurrent(m_engine); 
         m_thread = new Thread(this);
         m_thread.start();

         Engine.gc();
         Engine.printLine("Heap at TOTFC ctor exit (after Engine constructor): " + s_runtime.freeMemory());
      }
   }

   // MIDlet -- pause MIDlet application call.
   protected void pauseApp() { m_paused = true; }
   
   // MIDlet -- exit MIDlet application call.
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { }
   
   
   
   /****************************************************************************
    ** Implemented Methods
    ***************************************************************************/
   
   // thread's run method.
   public void run() {
      
      Thread currentThread = Thread.currentThread();
      
      try {
         while (currentThread == m_thread) {
            
            m_start_time = System.currentTimeMillis();
            
            // process the game per iteration.
            if(!m_paused) {
               
               if(m_engine != null) {
                  m_engine.process(m_start_time);
               }
               
               m_time_taken = (System.currentTimeMillis() - m_start_time);
               
               if(m_time_taken < MILLIS_PER_TICK) {
                  synchronized(this) {
                     wait(MILLIS_PER_TICK - m_time_taken);
                  }
               } else {
                  Thread.yield();
               }
            }
            // yield thread if paused.
            else {
               Thread.yield();
            }
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
   
   
   
   /****************************************************************************
    ** Miscellaneous Methods
    ***************************************************************************/

   public boolean getProperty(String property) {
      
      String value = getAppProperty(property);
      
      if(value == null || value.equals("OFF")) return false;
      else                                     return true;
   }
}

⌨️ 快捷键说明

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