路是爬出來的

          [導入]游戲中動畫的基礎

                  這篇文章是純粹的個人看法。

                  游戲的基礎是動畫,想來大家都知道。這幾天公司的項目都忙完了。很是無聊,所以就上網找了些資源,并寫兩個動畫的例子。在此貼出來,讓大家把磚頭砸我吧。^_^    

                  j2me midp2.0有個game的包是用來設計有游戲用的。它提供了游戲設計的基礎控件,比如雙緩沖,精靈,圖層控制器等基礎設施,這些設施可以方便我們的設計,比如雙緩沖可以讓游戲執行流暢,精靈等,可以更好的控制角色。

                 說白了。動畫的效果其實就是一幅幅圖片按照指定的時間一幅幅的換圖片而已。

                 好了??创a吧。

          java 代碼


           


          1.    

          2. package org.wuhua.game.timer;  

          3.   

          4. import java.util.Timer;  

          5. import java.util.TimerTask;  

          6.   

          7. /** 

          8.  *  對Timer的包裝 

          9.  * @author wuhua 

             
             

          10.  */  

          11. public class TimerTaskManager {  

          12.     private Timer _timer;  

          13.   

          14.     static TimerTaskManager instace;  

          15.   

          16.     public static TimerTaskManager getInstace() {  

          17.         if (instace == null)  

          18.             instace = new TimerTaskManager();  

          19.         return instace;  

          20.     }  

          21.   

          22.     public TimerTask add(Runnable runnable, long period) {  

          23.         TimerTask task = new RunnableTimerTask(runnable);  

          24.         long delay = period;  

          25.         getTimer().schedule(task, delay, period);  

          26.         return task;  

          27.     }  

          28.   

          29.     void close() {  

          30.         if (_timer != null) {  

          31.             _timer.cancel();  

          32.             _timer = null;  

          33.         }  

          34.     }  

          35.   

          36.     private Timer getTimer() {  

          37.         if (_timer == null)  

          38.             _timer = new Timer();  

          39.         return _timer;  

          40.     }  

          41.   

          42.     static class RunnableTimerTask extends TimerTask {  

          43.         private Runnable _runnable;  

          44.   

          45.         RunnableTimerTask(Runnable runnable) {  

          46.             _runnable = runnable;  

          47.         }  

          48.   

          49.         public void run() {  

          50.             _runnable.run();  

          51.         }  

          52.     }  

          53. }  



          java 代碼


           


          1.    

          2. package org.wuhua.game;  

          3.   

          4. import java.io.IOException;  

          5. import java.util.TimerTask;  

          6.   

          7. import javax.microedition.lcdui.Canvas;  

          8. import javax.microedition.lcdui.Graphics;  

          9. import javax.microedition.lcdui.Image;  

          10.   

          11. import org.wuhua.game.timer.TimerTaskManager;  

          12.   

          13.    

          14.   

          15. /** 

          16.  * 動畫的主類 

          17.  * @author wuhua 

             
             

          18.  */  

          19. public class Game extends Canvas implements Runnable{  

          20.   

          21.     private Image source;  

          22.     private Image action[] = new Image[10];  

          23.     private int bgcolor = 0x209C00;  

          24.     private TimerTask task;  

          25.     private static int next;  

          26.     Game(){  

          27.         try {  

          28.             source = Image.createImage("/action.png");  

          29.         } catch (IOException e) {  

          30.                

          31.             e.printStackTrace();  

          32.         }  

          33.         //切割圖片  

          34.         for(int i=0; i<5; i++){  

          35.             action[i] = Image.createImage(source, 96*i, 096600);  

          36.         }  

          37.           

          38.         for(int j=5; j<10; j++){  

          39.             action[j] = Image.createImage(source, 96*(j-5), 10296800);  

          40.         }  

          41.           

          42.         //這個是用來執行動作的計時器。原理是要求經過0.2毫秒動一次  

          43.         task = TimerTaskManager.getInstace().add(this150);   

          44.     }  

          45.     protected void paint(Graphics g) {  

          46.         fillScreen(g);  

          47.          paintAction(g);  

          48.   

          49.     }  

          50.     private void fillScreen(Graphics g) {  

          51.         g.setColor(0xFFFFFF);  

          52.         g.fillRect(00this.getWidth(), this.getHeight());  

          53.           

          54.     }  

          55.     private void paintAction(Graphics g) {  

          56.         if(next == 10)  

          57.             next =0;  

          58.         //如果繪制的圖片是出雷電的時候,讓人物停留在那里。這樣的效果會好點  

          59.         if(next>=5){  

          60.             g.drawImage(action[4], 10*40, Graphics.LEFT|Graphics.TOP);  

          61.         }  

          62.         g.drawImage(action[next], 10*next, 0, Graphics.LEFT|Graphics.TOP);  

          63.           

          64.            

          65.         next++;  

          66.           

          67.     }  

          68.     public void run() {  

          69.         repaint();  

          70.           

          71.     }  

          72.       

          73.       

          74.   

          75. }  



          文章來源: http://wuhua.javaeye.com/blog/36346

          posted on 2006-12-30 08:42 路是爬出來的 閱讀(104) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 盈江县| 崇礼县| 景宁| 迁西县| 泗阳县| 博野县| 满城县| 江山市| 美姑县| 连南| 宁国市| 浏阳市| 朝阳区| 花莲市| 黑水县| 武义县| 木兰县| 朝阳市| 尚义县| 昌宁县| 武夷山市| 兴和县| 梁河县| 宣城市| 隆子县| 辽宁省| 琼中| 利辛县| 攀枝花市| 邢台市| 兰溪市| 广东省| 黔西县| 景谷| 七台河市| 丹寨县| 永善县| 芦山县| 金阳县| 荔浦县| 英德市|