级别: 论坛版主
UID: 1652
精华: 2
发帖: 156
威望: 117 点
金钱: 167 RMB
贡献值: 0 点
注册时间: 2005-07-28
最后登录: 2007-01-12
楼主  发表于: 2005-10-04 12:58

 手把手教你写第三个手机游戏

图片:
大家看了前面我写的教程,回去在自己电脑上运行了吗?
是不是很有意思呢?或者说太单调,而不会动觉得乏味呢?

别急,现在教大家如何在手机屏上显示图片,而且让她动起来:P
本程序需要图片文件,大家可以用自己的图片文件放到你自己建立的项目的目录下的
/RES目录下. 比如你建立了girl这个项目,那么在你安装的WTK22目录下的apps里有个叫girl的目录.   ..\WTK22\apps\girl\res\   注意图片的大小最好不要过大,因为屏幕
本身尺寸就不是很大,建议 50X50左右,这样会得到好的效果:P

PS:希望大家看了我的教程,能回个贴,大家回了贴就是给我最大的动力.使我有继续写
下去的动力.   谢谢   为了中国的游戏事业大家一起加油吧~~~~~~!!!
----------------------------------------------------------------------------------------------------------

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ufo extends MIDlet implements CommandListener
{
     private ufocanvas canvas;
     public void startApp()
     {
           if(canvas==null)
           {
                 canvas=new ufocanvas(Display.getDisplay(this));
                 Command exitCommand=new Command("Exit",Command.EXIT,0);
                 canvas.addCommand(exitCommand);
                 canvas.setCommandListener(this);
           }
           canvas.start();
     }
     public void pauseApp()
     {
     }
     public void destroyApp(boolean b)
     {
           canvas.stop();
     }
     public void commandAction(Command c,Displayable s)
     {
           if(c.getCommandType()==Command.EXIT)
           {
                 destroyApp(true);
                 notifyDestroyed();
           }
     }
}


------------------------------------------------------------------------------------------------------


import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
import java.util.*;
public class ufocanvas extends GameCanvas implements Runnable
{
     private Display display;
     private boolean sleep;
     private long framedelay;
     private Random rand;
     private Sprite ufoSprite;
     private int ufoXmove,ufoYmove;
     private Thread t;
     private int frameWidth,frameHeight;
     private int imageWidth,imageHeight;
     public ufocanvas(Display d)
     {
           super(true);
           display=d;
           framedelay=33;
     }
     public void start()
     {
           display.setCurrent(this);
           try
           {
                 ufoSprite=new Sprite(Image.createImage("/1.png"));
                 ufoSprite.setPosition(0,0);
           }
           catch(Exception e)
           {
                 e.printStackTrace();
                 System.err.println("File loading is erro...");
           }
           frameWidth=getWidth();
           frameHeight=getHeight();
           imageWidth=ufoSprite.getWidth();
           imageHeight=ufoSprite.getHeight();
           ///////////////////////////////////////////////////////
           //System.out.println(imageWidth+" "+imageHeight);
           ufoXmove=ufoYmove=2;
           sleep=false;
           rand=new Random();
           t=new Thread(this);
           t.start();      
     }
     public void run()
     {
           Graphics g=getGraphics();
           while(!sleep)
           {
                 updraw();
                 draw(g);
                 try
                 {
                       Thread.sleep(framedelay);
                 }
                 catch(Exception e)
                 {
                       e.printStackTrace();
                 }
           }
     }
     public void stop()
     {
           sleep=true;
     }
     public void updraw()
     {
           ufoSprite.move(ufoXmove,ufoYmove);
           if(ufoSprite.getX()+imageWidth>=frameWidth)
                 ufoXmove=-ufoXmove;
           if(ufoSprite.getY()+imageHeight>=frameHeight)
                 ufoYmove=-ufoYmove;
           if(ufoSprite.getX()<=0)
                 ufoXmove=-ufoXmove;
           if(ufoSprite.getY()<=0)
                 ufoYmove=-ufoYmove;
     }
     public void draw(Graphics g)
     {
           g.setColor(0,0,0);
           g.fillRect(0,0,frameWidth,frameHeight);
           ufoSprite.paint(g);
           flushGraphics();
     }
}

希望大家能回贴,回贴是给我写下去的最大动力!
JAVA狂热爱好者
级别: 总版主
UID: 1
精华: 4
发帖: 909
威望: 586 点
金钱: 4752 RMB
贡献值: 0 点
注册时间: 2005-03-21
最后登录: 2009-05-27
1楼  发表于: 2005-10-04 21:36
再顶。
知识共享,共同进步。
级别: 新手上路
UID: 608
精华: 0
发帖: 99
威望: 72 点
金钱: 150 RMB
贡献值: 0 点
注册时间: 2005-05-14
最后登录: 2008-03-25
2楼  发表于: 2005-11-16 13:38
顶起!!!
级别: 新手上路
UID: 1641
精华: 0
发帖: 16
威望: 6 点
金钱: 47 RMB
贡献值: 0 点
注册时间: 2005-07-27
最后登录: 2008-06-05
3楼  发表于: 2005-11-17 17:40
册那,老卵
级别: 新手上路
UID: 9435
精华: 0
发帖: 7
威望: 8 点
金钱: 70 RMB
贡献值: 0 点
注册时间: 2006-09-10
最后登录: 2007-01-15
4楼  发表于: 2007-01-15 18:17
谢谢! 你真得太好了,能留个qq 号码吗
级别: 新手上路
UID: 27413
精华: 0
发帖: 3
威望: 4 点
金钱: 30 RMB
贡献值: 0 点
注册时间: 2007-09-22
最后登录: 2007-09-23
5楼  发表于: 2007-09-23 02:19
ufo.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ufo extends MIDlet implements CommandListener
{
    private ufocanvas canvas;
    public void startApp()
    {
          if(canvas==null)
          {
                canvas=new ufocanvas(Display.getDisplay(this));
                Command exitCommand=new Command("Exit",Command.EXIT,0);
                canvas.addCommand(exitCommand);
                canvas.setCommandListener(this);
          }
          canvas.start();
    }
    public void pauseApp()
    {
    }
    public void destroyApp(boolean b)
    {
          canvas.stop();
    }
    public void commandAction(Command c,Displayable s)
    {
          if(c.getCommandType()==Command.EXIT)
          {
                destroyApp(true);
                notifyDestroyed();
          }
    }
}
ufocanvas.java
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
import java.util.*;
public class ufocanvas extends GameCanvas implements Runnable
{
    private Display display;
    private boolean sleep;
    private long framedelay;
    private Random rand;
    private Sprite ufoSprite;
    private int ufoXmove,ufoYmove;
    private Thread t;
    private int frameWidth,frameHeight;
    private int imageWidth,imageHeight;
    public ufocanvas(Display d)
    {
          super(true);
          display=d;
          framedelay=33;
    }
    public void start()
    {
          display.setCurrent(this);
          try
          {
                ufoSprite=new Sprite(Image.createImage("/1.png"));
                ufoSprite.setPosition(0,0);
          }
          catch(Exception e)
          {
                e.printStackTrace();
                System.err.println("File loading is erro...");
          }
          frameWidth=getWidth();
          frameHeight=getHeight();
          imageWidth=ufoSprite.getWidth();
          imageHeight=ufoSprite.getHeight();
          ///////////////////////////////////////////////////////
          //System.out.println(imageWidth+" "+imageHeight);
          ufoXmove=ufoYmove=2;
          sleep=false;
          rand=new Random();
          t=new Thread(this);
          t.start();     
    }
    public void run()
    {
          Graphics g=getGraphics();
          while(!sleep)
          {
                updraw();
                draw(g);
                try
                {
                      Thread.sleep(framedelay);
                }
                catch(Exception e)
                {
                      e.printStackTrace();
                }
          }
    }
    public void stop()
    {
          sleep=true;
    }
    public void updraw()
    {
          ufoSprite.move(ufoXmove,ufoYmove);
          if(ufoSprite.getX()+imageWidth>=frameWidth)
                ufoXmove=-ufoXmove;
          if(ufoSprite.getY()+imageHeight>=frameHeight)
                ufoYmove=-ufoYmove;
          if(ufoSprite.getX()<=0)
                ufoXmove=-ufoXmove;
          if(ufoSprite.getY()<=0)
                ufoYmove=-ufoYmove;
    }
    public void draw(Graphics g)
    {
          g.setColor(0,0,0);
          g.fillRect(0,0,frameWidth,frameHeight);
          ufoSprite.paint(g);
          flushGraphics();
    }
}



运行的时候出现以下错误代码是为什么?

警告:没有初始化 WMA 消息路由支持
警告:初始化蓝牙 (JSR 82) 支持失败
java.io.IOException
    at javax.microedition.lcdui.ImmutableImage.getImageFromStream(+15)
    at javax.microedition.lcdui.ImmutableImage.<init>(+20)
    at javax.microedition.lcdui.Image.createImage(+8)
    at ufocanvas.start(+18)
    at ufo.startApp(+58)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+270)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+116)
File loading is erro...
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
    at ufocanvas.start(+70)
    at ufo.startApp(+58)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+270)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+116)
级别: 新手上路
UID: 27413
精华: 0
发帖: 3
威望: 4 点
金钱: 30 RMB
贡献值: 0 点
注册时间: 2007-09-22
最后登录: 2007-09-23
6楼  发表于: 2007-09-23 02:24
不好意思。知道为什么了。原来是图片没改名。
级别: 新手上路
UID: 38572
精华: 0
发帖: 7
威望: 8 点
金钱: 70 RMB
贡献值: 0 点
注册时间: 2008-07-12
最后登录: 2009-02-22
7楼  发表于: 2008-07-15 22:57
你的贴我都顶
级别: 新手上路
UID: 10762
精华: 0
发帖: 17
威望: 18 点
金钱: 170 RMB
贡献值: 0 点
注册时间: 2006-10-15
最后登录: 2008-08-12
8楼  发表于: 2008-08-06 17:17
我用MyEclpse开发滴