路是爬出来的

[导入]游戏中动画的基础

        这篇文章是纯粹的个人看法。

        游戏的基础是动画,想来大家都知道。这几天公司的项目都忙完了。很是无聊,所以就上网找了些资源,并写两个动画的例子。在此贴出来,让大家把砖头砸我吧。^_^    

        j2me midp2.0有个game的包是用来设计有游戏用的。它提供了游戏设计的基础控件,比如双缓冲,精灵,图层控制器等基础设施,这些设施可以方便我们的设计,比如双缓冲可以让游戏执行流畅,精灵等,可以更好的控制角色。

       说白了。动画的效果其实就是一幅幅图片按照指定的时间一幅幅的换图片而已。

       好了。看代码吧。

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 路是爬出来的 阅读(91) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: