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

📄 scene.java

📁 J2ME编程的50个例子,适合掌上系统的编程
💻 JAVA
字号:
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;

public class Scene {
	public TiledLayer m_TLayer; 					//场景
	//构造方法
	Scene(Random rand, int scrWidth, int scrHeight){
		//读取场景的tile图像
		try{
			Image image = Image.createImage("/demo/map.png");
			m_TLayer = new TiledLayer( 10, 25, image, 20, 20 );
			int x = scrWidth/2 - m_TLayer.getWidth()/2;
			int y = scrHeight - m_TLayer.getHeight();
			m_TLayer.setPosition( x, y );
		}
	    catch(Exception e){}
	}
	public void LoadMap(){
		try{
			//读取地图文件
			InputStream is = getClass().getResourceAsStream("/demo/map.txt");
			int ch = -1;
			for( int nRow = 0; nRow < m_TLayer.getRows(); nRow ++ )
			{
				for( int nCol = 0; nCol < m_TLayer.getColumns(); nCol ++ )
				{
					ch = -1;
					while( ( ch < 0 || ch > 3 ) ){
						ch = is.read();
						if( ch == -1 )
							return;
						ch = ch - '0';
					}
					m_TLayer.setCell( nCol, nRow, ch );
				}
			}	
		}
		catch(IOException ioe){}
		catch(Exception e){}
	}
	//逻辑操作,让场景向下运动
	//参数scrWidth、scrHeight分别是屏幕的宽和高
	public void Logic( int scrWidth, int scrHeight ){
		int x = m_TLayer.getX();
		int y = m_TLayer.getY();
		y = y + 1;
		if( y >= 0 ){	//如果运动到头了,则重新开始
			x = scrWidth/2 - m_TLayer.getWidth()/2;
			y = scrHeight - m_TLayer.getHeight();
		}
		m_TLayer.setPosition( x, y );
	}
}

⌨️ 快捷键说明

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