⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spirit.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
字号:
import java.io.DataInputStream;
import java.io.InputStream;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
//import com.nokia.mid.ui.DirectUtils;

public class Spirit {
	// 得到动作编辑器编辑动作的数据
    public void getTypeData(String indexbinName,String pngName,String indexcombin)
    {
        try{      
        	image = Image.createImage(pngName);

            rsImages = new Image[]{image};

            InputStream inputStream = this.getClass().getResourceAsStream(indexcombin);
            DataInputStream datainputstream = new DataInputStream(inputStream);
            
            int n_num = datainputstream.read();

            this.com = new short[n_num][];
            for(int i=0; i<n_num; i++)
            {
                int n_dat = datainputstream.read();                
                
                com[i] = new short[n_dat*5];
                for(int j=0; j<n_dat; j++)
                    for(int k=0; k<5; k++)
                        com[i][j*5+k] = (short)datainputstream.read();
            }

            inputStream = this.getClass().getResourceAsStream(indexbinName);
            datainputstream = new DataInputStream(inputStream);
            
            //the new data of the object
            int ns_action = datainputstream.read();//动作数

            this.rect = new short[ns_action*2];
            this.data = new short[ns_action][][];
            this.frame = new short[ns_action][];

            for(int j=0; j<ns_action; j++)
            {
                int n_bin = datainputstream.read();//每个动作的帧数 
                this.rect[j*2+0] = (short)datainputstream.read();
                this.rect[j*2+1] = (short)datainputstream.read();
                                
                this.data[j] = new short[n_bin][];
                this.frame[j] = new short[n_bin*6];

                for(int i=0; i<n_bin; i++){
                    int n = datainputstream.read();//每帧的图块数                   
                    
                    for (int h = 0; h < 6; h++) {
						this.frame[j][i * 6 + h] = (short) datainputstream
								.read();// 0-absolute xpos,1-absolute ypos,2 -
										// frameX,3 - fameY,4 - frameW, 5 -
										// frameH
					}
                    
                    this.data[j][i] = new short[n*5];
                    for(int k=0; k<n*5; k += 5)
                    {                  
                        this.data[j][i][k+0] = (short)datainputstream.read();//0-picid
                        this.data[j][i][k+1] = (short)datainputstream.read();//0-blockid
                        this.data[j][i][k+2] = (short)datainputstream.read();//x-pos
                        this.data[j][i][k+3] = (short)datainputstream.read();//y-pos
                        this.data[j][i][k+4] = (short)datainputstream.read();//transform                                          
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();            
        }
    }
	
	public void draw(Graphics g,int xPos,int yPos) {
        try{
            nextFrame();

            for(int i=0; i<data[action][currentFrame].length; i+=5) {
                short picIndex = data[action][currentFrame][i+0];
                short blockIndex = data[action][currentFrame][i+1];
                
                short offx = com[picIndex][blockIndex*5+1];
                
                short offy = com[picIndex][blockIndex*5+2];
                
                short width = com[picIndex][blockIndex*5+3];
                short height = com[picIndex][blockIndex*5+4];
            
                int newx = (xPos >>8) + (data[action][currentFrame][i+2] - frame[action][currentFrame*6+0]);
                int newy = (yPos >>8) + (data[action][currentFrame][i+3] - frame[action][currentFrame*6+1]);
                
                int anchor = data[action][currentFrame][i+4];
                
                if(isImageRotate) {
                    int newx1 = rect[action*2+0] - frame[action][currentFrame*6+0];
                    int newx2 = 0;
                    
                    switch(anchor) {
                        case 0:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - width;
                            anchor = 2;
                            break;
                        case 1:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - width;
                            anchor = 3;
                            break;
                        case 2:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - width;
                            anchor = 0;
                            break;
                        case 3:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - width;
                            anchor = 1;
                            break;
                        case 4:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - height;
                            anchor = 5;
                            break;
                        case 5:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - height;
                            anchor = 4;
                            break;
                        case 6:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - height;
                            anchor = 7;
                            break;
                        case 7:
                            newx2 = rect[action*2+0] - data[action][currentFrame][i+2] - height;
                            anchor = 6;
                            break;
                    }
                    newx = (xPos >>8) + newx2 - newx1;
                }
                
                int off_width = 0;
                int off_height = 0;
                //int nokia_anchor = 0;

                switch(anchor) {
                    case 0://TRANS_NONE
                        g.setClip(newx,newy,width,height);
                        off_width = offx;
                        off_height = offy;
                        //nokia_anchor = 0;
                        //g.drawImage(image,newx-off_width,newy-off_height,0x4|0x10);
                        //return;
                        break;
                    case 1://TRANS_MIRROR_ROT180
                        g.setClip(newx,newy,width,height);
                        off_width = offx;
                        off_height = rsImages[picIndex].getHeight() - offy - height;
                        //nokia_anchor = 0x4000;
                        break;
                    case 2://TRANS_MIRROR
                        g.setClip(newx,newy,width,height);
                        off_height = rsImages[picIndex].getWidth() -offx-width;
                        off_height = offy;
                        //nokia_anchor = 0x2000;
                        break;
                    case 3://TRANS_ROT180
                        g.setClip(newx,newy,width,height);
                        off_width = rsImages[picIndex].getWidth() - offx - width;
                        off_height = rsImages[picIndex].getHeight() - offy - height;
                        //nokia_anchor = 180;
                        break;
                    case 4://TRANS_MIRROR_ROT270
                        g.setClip(newx,newy,height,width);
                        off_width = offy;
                        off_height = offx;
                        //nokia_anchor = 0x2000|270;
                        break;
                    case 5://rotate 90
                        g.setClip(newx,newy,height,width);
                        off_width = rsImages[picIndex].getHeight() - offy - height;
                        off_height = offx;
                        //nokia_anchor = 270;
                        break;
                    case 6://rotate270
                        g.setClip(newx,newy,height,width);
                        off_width = offy;                       
                        off_height = rsImages[picIndex].getWidth() - offx - width;
                        //nokia_anchor = 90;
                        break;
                    case 7://TRANS_MIRROR_ROT90
                        g.setClip(newx,newy,height,width);
                        off_width = rsImages[picIndex].getHeight() - offy - height;
                        off_height = rsImages[picIndex].getWidth() - offx - width;
                        //nokia_anchor = 0x2000|90;
                        break;
                }

               if(anchor == 0)
                	g.drawImage(image,newx-off_width,newy-off_height,0x4|0x10);
                else
                //DirectUtils.getDirectGraphics(g).drawImage(image,newx - off_width,newy - off_height,0x4|0x10,nokia_anchor);
                g.drawRegion(image, offx, offy, width, height, anchor,
							newx, newy, GameConstant.TOPLEFT);

                g.setClip(0,0,GameConstant.iSCREEN_WIDTH,GameConstant.iSCREEN_HEIGHT);
            }
        }catch(Exception e4){
        	e4.printStackTrace();}
    }
	
	public void clean() {
		for (int idx = 0; idx < rsImages.length; idx++) {
			rsImages[idx] = null;
		}
		rsImages = null;
		image = null;
		com = null;
		rect = null;
		data = null;
		frame = null;
	}
	
	public void nextFrame() {
        currentFrame++;
        currentFrame %= data[action].length;
    }
    
    public void preFrame() {
        currentFrame--;
        currentFrame %= data[action].length;
    }
    
    public boolean firstFrame() {
        return currentFrame == 0;
    }
    
    public boolean lastFrame() {
        if(data == null)return false;
        return currentFrame == data[action].length - 1;
    }
    
    public void setAction(byte action) {
		this.action = action;
	}
    
    public byte getAction() {
		return action;
	}
    
    public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}

	private Image[] rsImages;

	private Image image;

    /**当前动作*/
	private byte action = 0;

	/**当前动作帧*/
	private int currentFrame = -1;

	/**图片是否旋转*/
	private boolean isImageRotate;

	/**图片的所有信息*/
	private short[][] com = null;

	/**图片的碰撞框*/
	private short[] rect = null;

	/**图片的数据*/
	private short[][][] data = null;

	/**图片每帧的数据*/
	private short[][] frame = null;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -