日期:2014-05-20  浏览次数:20728 次

一个Sprite的问题,求救,急!~
package demo;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

public class MainCanvas extends GameCanvas implements Runnable{
private boolean m_bRunning;
public Sprite m_CockSprite;
public Image m_BackImg;
public MainCanvas(){
super(true);
try{
m_BackImg = Image.createImage("/demo/back.png");
Image image = Image.createImage("/demo/cock.png");
m_CockSprite = new Sprite(image,55,55);
m_CockSprite.setFrame(0);

int nPosX = getWidth();
int nPosY = getHeight() - 90;

m_CockSprite.setPosition(nPosX,nPosY);
}
catch(Exception ex){
}
Start();
}

public void Start(){
m_bRunning = true;
Thread thread = new Thread(this);
thread.start();
}

public void run(){
long T1 = System.currentTimeMillis();
long T2 = T1;
while(m_bRunning){
T2 = System.currentTimeMillis();
if(T2 - T1 > 100){
T1 = T2;
Input();
Logic();
Paint();
}
}
}
public void Stop(){
m_bRunning = false;
}

public void Input(){
}

public void Logic(){
int n = m_CockSprite.getFrame();
n++;
if(n >= m_CockSprite.getFrameSequenceLength()){
n = 0;
}
m_CockSprite.setFrame(n);

int nPosX = m_CockSprite.getX();
int nPosY = m_CockSprite.getY();
nPosX -= 3;
if(nPosX < -95)
nPosX = getWidth();
m_CockSprite.setPosition(nPosX,nPosY);
}

public void Paint(){
Graphics g = getGraphics();
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
int nPosX = getWidth()/2 - 176/2;
int nPosY = getHeight()/2 - 208/2;
g.drawImage(m_BackImg, nPosX, nPosY, 0);
m_CockSprite.paint(g);
flushGraphics();
}
}

以上是我在网上搜来的学习资料。
在DEBUG的时候,程序执行到m_CockSprite = new Sprite(image,55,55)的时候不成功,导致后面的NULLPOINTEXCEPTION错误。在网上搜了好多也没看到结果。有没有哪位大哥能相告一下,在此谢过!~

------解决方案--------------------
public Sprite(Image image, int frameWidth, int frameHeight)

Throws:
NullPointerException - if img is null 
IllegalArgumentException - if frameHeight or frameWidth is less than 1 
IllegalArgumentException - if the image width is not an integer multiple of the frameWidth 
IllegalArgumentException - if the image height is not an integer multiple of the frameHeight