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

📄 clothes.java

📁 J2ME手机游戏50例
💻 JAVA
字号:
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 Clothes{
	//服装种类
	public static final int CLOTHES_HAIR		= 0;	//头发
	public static final int CLOTHES_GILET		= 1;	//背心
	public static final int CLOTHES_SKIRT 	= 2;	//裙子
	public static final int CLOTHES_TYPE_NUM	= 3;	//种类总数
	
	private Sprite m_aClothesSp[];
	private int m_naBodyIndex[];						//MM身上所穿服装的索引
	private int m_naSelIndex[];						//当前选择服装的索引
	private int m_nCurSel	= CLOTHES_HAIR;				//当前选择的种类
	public Clothes(){
		try{
			//读取不同的服装
			m_aClothesSp = new Sprite[CLOTHES_TYPE_NUM];
			m_naBodyIndex = new int[CLOTHES_TYPE_NUM];
			m_naSelIndex = new int[CLOTHES_TYPE_NUM];
			
			Image imgHair = Image.createImage("/demo/hair.png");
			m_aClothesSp[CLOTHES_HAIR] = new Sprite(imgHair,32,51);
			m_aClothesSp[CLOTHES_HAIR].defineReferencePixel(16, 25);
			
			Image imgClothes = Image.createImage("/demo/clothes.png");
			m_aClothesSp[CLOTHES_GILET] = new Sprite(imgClothes, 35, 47);
			m_aClothesSp[CLOTHES_GILET].defineReferencePixel(17, 23);
			
			Image imgSkirt = Image.createImage("/demo/skirt.png");
			m_aClothesSp[CLOTHES_SKIRT] = new Sprite(imgSkirt, 42, 47);
			m_aClothesSp[CLOTHES_SKIRT].defineReferencePixel(21, 23);
		}
		catch (Exception ex){}
	}
	//控制根据键盘的输入,选择当前的服装
	public void Input( int keyStates ){
		if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ){
			m_nCurSel --;
			if( m_nCurSel < 0 )
				m_nCurSel = CLOTHES_TYPE_NUM - 1;
		}
		if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ){
			m_nCurSel ++;
			if( m_nCurSel >= CLOTHES_TYPE_NUM )
				m_nCurSel = 0;
		}
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 ){
			m_naSelIndex[m_nCurSel] --;
			if( m_naSelIndex[m_nCurSel] < 0 ){
				m_naSelIndex[m_nCurSel] = 
					m_aClothesSp[m_nCurSel].getFrameSequenceLength() - 1;
			}
		}
		if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 ){
			m_naSelIndex[m_nCurSel] ++;
			if( m_naSelIndex[m_nCurSel] >= 
				m_aClothesSp[m_nCurSel].getFrameSequenceLength() )
				m_naSelIndex[m_nCurSel] = 0;
		}
		if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ){
			//选择MM身上的服装
			m_naBodyIndex[m_nCurSel] = m_naSelIndex[m_nCurSel];
		}
	}
	//显示所有服装,参数x,y是MM位置的参考点
	public void Paint(Graphics g, int x, int y){
		int ax, ay;
		
		//显示MM身上的服装
		ax = x - 2;
		ay = y - 36;
		m_aClothesSp[CLOTHES_HAIR].setFrame(m_naBodyIndex[CLOTHES_HAIR]);
		m_aClothesSp[CLOTHES_HAIR].setRefPixelPosition(ax, ay);
		m_aClothesSp[CLOTHES_HAIR].paint(g);
		
		ax = x - 3;
		ay = y;
		m_aClothesSp[CLOTHES_GILET].setFrame(m_naBodyIndex[CLOTHES_GILET]);
		m_aClothesSp[CLOTHES_GILET].setRefPixelPosition(ax, ay);
		m_aClothesSp[CLOTHES_GILET].paint(g);
		
		ax = x - 3;
		ay = y + 37;
		m_aClothesSp[CLOTHES_SKIRT].setFrame(m_naBodyIndex[CLOTHES_SKIRT]);
		m_aClothesSp[CLOTHES_SKIRT].setRefPixelPosition(ax, ay);
		m_aClothesSp[CLOTHES_SKIRT].paint(g);
		
		//显示其他服装
		x = 40;
		y = 30;
		for( int n = 0; n < m_aClothesSp.length; n ++ ){
			m_aClothesSp[n].setFrame(m_naSelIndex[n]);
			m_aClothesSp[n].setRefPixelPosition(x, y);
			m_aClothesSp[n].paint(g);
			if( n == m_nCurSel ){
				g.setColor(0xFFFFFF00);
				g.drawRect(x-25, y-25, 50, 50 );
			}
			y = y + 50;
		}
	}
}

⌨️ 快捷键说明

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