饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機開發網

             :: 首頁 :: 聯系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks

          ?http://googledev.blog.hexun.com/8315619_d.html




          ?這篇文章是純粹的個人看法。
          游戲的基礎是動畫,想來大家都知道。這幾天公司的項目都忙完了。很是無聊,所以就上網找了些資源,并寫兩個動畫的例子。在此貼出來,讓大家把磚頭砸我吧。^_^
          j2me midp2.0有個game的包是用來設計有游戲用的。它提供了游戲設計的基礎控件,比如雙緩沖,精靈,圖層控制器等基礎設施,這些設施可以方便我們的設計,比如雙緩沖可以讓游戲執行流暢,精靈等,可以更好的控制角色。
          說白了。動畫的效果其實就是一幅幅圖片按照指定的時間一幅幅的換圖片而已。
          好了。看代碼吧。

          ?

          java 代碼

          1. package org.wuhua.game.timer;

          2. import java.util.Timer;
          3. import java.util.TimerTask;

          4. /**
          5. * 對Timer的包裝
          6. * @author wuhua
          7. */
          8. public class TimerTaskManager {
          9. private Timer _timer;

          10. static TimerTaskManager instace;

          11. public static TimerTaskManager getInstace() {
          12. if (instace == null )
          13. instace = new TimerTaskManager();
          14. return instace;
          15. }

          16. public TimerTask add(Runnable runnable, long period) {
          17. TimerTask task = new RunnableTimerTask(runnable);
          18. long delay = period;
          19. getTimer().schedule(task, delay, period);
          20. return task;
          21. }

          22. void close() {
          23. if (_timer != null ) {
          24. _timer.cancel();
          25. _timer = null;
          26. }
          27. }

          28. private Timer getTimer() {
          29. if (_timer == null )
          30. _timer = new Timer();
          31. return _timer;
          32. }

          33. static class RunnableTimerTask extends TimerTask {
          34. private Runnable _runnable;

          35. RunnableTimerTask(Runnable runnable) {
          36. _runnable = runnable;
          37. }

          38. public void run() {
          39. _runnable.run();
          40. }
          41. }
          42. }
          java 代碼

          1. package org.wuhua.game;

          2. import java.io.IOException;
          3. import java.util.TimerTask;

          4. import javax.microedition.lcdui.Canvas;
          5. import javax.microedition.lcdui.Graphics;
          6. import javax.microedition.lcdui.Image;

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



          8. /**
          9. * 動畫的主類
          10. * @author wuhua
          11. */
          12. public class Game extends Canvas implements Runnable{

          13. private Image source;
          14. private Image action[] = new Image[ 10 ];
          15. private int bgcolor = 0x209C00 ;
          16. private TimerTask task;
          17. private static int next;
          18. Game(){
          19. try {
          20. source = Image.createImage("/action.png");
          21. } catch (IOException e) {

          22. e.printStackTrace();
          23. }
          24. //切割圖片
          25. for ( int i= 0 ; i< 5 ; i++){
          26. action[i] = Image.createImage(source, 96*i, 0, 96, 60, 0);
          27. }

          28. for ( int j= 5 ; j< 10 ; j++){
          29. action[j] = Image.createImage(source, 96*(j-5), 102, 96, 80, 0);
          30. }

          31. //這個是用來執行動作的計時器。原理是要求經過0.2毫秒動一次
          32. task = TimerTaskManager.getInstace().add(this, 150);
          33. }
          34. protected void paint(Graphics g) {
          35. fillScreen(g);
          36. paintAction(g);

          37. }
          38. private void fillScreen(Graphics g) {
          39. g.setColor(0xFFFFFF);
          40. g.fillRect(0, 0, this.getWidth(), this.getHeight());

          41. }
          42. private void paintAction(Graphics g) {
          43. if (next == 10 )
          44. next =0;
          45. //如果繪制的圖片是出雷電的時候,讓人物停留在那里。這樣的效果會好點
          46. if (next>= 5 ){
          47. g.drawImage(action[4], 10*4, 0, Graphics.LEFT|Graphics.TOP);
          48. }
          49. g.drawImage(action[next], 10*next, 0, Graphics.LEFT|Graphics.TOP);


          50. next++;

          51. }
          52. public void run() {
          53. repaint();

          54. }



          55. }




          爬蟲工作室 -- 專業的手機軟件開發工作室
          3G視線 -- 專注手機軟件開發
          posted on 2007-03-23 17:55 3G工作室 閱讀(1160) 評論(0)  編輯  收藏 所屬分類: j2me
          主站蜘蛛池模板: 德清县| 宜昌市| 温泉县| 武陟县| 景洪市| 东丽区| 彩票| 兰州市| 越西县| 甘孜| 通化县| 黄骅市| 乐山市| 博兴县| 措勤县| 繁昌县| 舟山市| 盖州市| 威信县| 乳山市| 襄汾县| 都江堰市| 开化县| 乡城县| 霸州市| 韶关市| 朝阳县| 滨州市| 武冈市| 荣昌县| 太保市| 梨树县| 虞城县| 遵义市| 米易县| 宜州市| 云梦县| 巩义市| 高阳县| 拜城县| 克东县|