这篇文章是纯粹的个人看法。
游戏的基础是动画,想来大家都知道。这几天公司的项目都忙完了。很是无聊,所以就上网找了些资源,并写两个动画的例子。在此贴出来,让大家把砖头砸我吧。^_^
j2me midp2.0有个game的包是用来设计有游戏用的。它提供了游戏设计的基础控件,比如双缓冲,精灵,图层控制器等基础设施,这些设施可以方便我们的设计,比如双缓冲可以让游戏执行流畅,精灵等,可以更好的控制角色。
说白了。动画的效果其实就是一幅幅图片按照指定的时间一幅幅的换图片而已。
好了。看代码吧。
java 代码
-
- package org.wuhua.game.timer;
-
- import java.util.Timer;
- import java.util.TimerTask;
-
-
-
-
-
- public class TimerTaskManager {
- private Timer _timer;
-
- static TimerTaskManager instace;
-
- public static TimerTaskManager getInstace() {
- if (instace == null)
- instace = new TimerTaskManager();
- return instace;
- }
-
- public TimerTask add(Runnable runnable, long period) {
- TimerTask task = new RunnableTimerTask(runnable);
- long delay = period;
- getTimer().schedule(task, delay, period);
- return task;
- }
-
- void close() {
- if (_timer != null) {
- _timer.cancel();
- _timer = null;
- }
- }
-
- private Timer getTimer() {
- if (_timer == null)
- _timer = new Timer();
- return _timer;
- }
-
- static class RunnableTimerTask extends TimerTask {
- private Runnable _runnable;
-
- RunnableTimerTask(Runnable runnable) {
- _runnable = runnable;
- }
-
- public void run() {
- _runnable.run();
- }
- }
- }
java 代码
-
- package org.wuhua.game;
-
- import java.io.IOException;
- import java.util.TimerTask;
-
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
-
- import org.wuhua.game.timer.TimerTaskManager;
-
-
-
-
-
-
-
- public class Game extends Canvas implements Runnable{
-
- private Image source;
- private Image action[] = new Image[10];
- private int bgcolor = 0x209C00;
- private TimerTask task;
- private static int next;
- Game(){
- try {
- source = Image.createImage("/action.png");
- } catch (IOException e) {
-
- e.printStackTrace();
- }
-
- for(int i=0; i<5; i++){
- action[i] = Image.createImage(source, 96*i, 0, 96, 60, 0);
- }
-
- for(int j=5; j<10; j++){
- action[j] = Image.createImage(source, 96*(j-5), 102, 96, 80, 0);
- }
-
-
- task = TimerTaskManager.getInstace().add(this, 150);
- }
- protected void paint(Graphics g) {
- fillScreen(g);
- paintAction(g);
-
- }
- private void fillScreen(Graphics g) {
- g.setColor(0xFFFFFF);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
-
- }
- private void paintAction(Graphics g) {
- if(next == 10)
- next =0;
-
- if(next>=5){
- g.drawImage(action[4], 10*4, 0, Graphics.LEFT|Graphics.TOP);
- }
- g.drawImage(action[next], 10*next, 0, Graphics.LEFT|Graphics.TOP);
-
-
- next++;
-
- }
- public void run() {
- repaint();
-
- }
-
-
-
- }
|