J2ME Sprite框架

时间:2012-08-08 06:03:06

标签: java-me sprite midp lcdui

我正在尝试仅绘制Image的一部分。人类的图像长度为159x22。

在该图像中有8个人体(2个左侧,2个右侧等)。如果我尝试将Frame设置为humanSprite.setFrame(1);,我将收到错误,因为我在Sprite构造函数中指定了Image的大小,因此只有一个帧。

好吧,我试过把它除以8和ll get java.lang.IllegalArgumentException`。

这是班级:

package org.pack.rhynn;

import java.io.IOException;                     
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;                
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;  
import java.util.Random;


public class play extends GameCanvas implements Runnable{


int sleep = 30;
private Image map;
private Sprite mapSprite;

private Image human;
private Sprite humanSprite;
private int humanX = getWidth() /2;
private int humanY = getHeight() /2;

public play(){
    super(false);
}

public void start(){

    try {                           
        map = Image.createImage("/mapas.png");
        human = Image.createImage("/human.PNG");
    } catch (IOException ioex) {                
        System.out.println(ioex);           
    }


    mapSprite = new Sprite(map);                    
    mapSprite.defineReferencePixel(100, 150);                   
    mapSprite.setRefPixelPosition(0, 0);

    humanSprite = new Sprite(human,159,22);                 
    humanSprite.defineReferencePixel(1, 10);                    
    humanSprite.setRefPixelPosition(humanX, humanY);


    Thread thr = new Thread(this);
    thr.start();
}

public void run(){
    while(true){

        updateScreen(getGraphics());

    try{
        Thread.sleep(sleep);

    }catch(Exception e){}
}
    }

private void createBackground(Graphics g){

    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());

}

private void updateScreen(Graphics g){
    createBackground(g);



    mapSprite.setRefPixelPosition(0, 0);                
    mapSprite.paint(g);

    humanSprite.setRefPixelPosition(humanX, humanY);
    humanSprite.setFrame(0);

    humanSprite.setPosition(50,50);
    humanSprite.paint(g);

    flushGraphics();


}



}

1 个答案:

答案 0 :(得分:2)

问题似乎与你的形象有关。所有帧必须具有相同的宽度和高度,图像的最终宽度和高度必须是它们的倍数。

例如,假设您的所有帧都在一行中。如果帧宽为19且您有8帧,则最终图像宽度必须为152。

相关问题