📄 scene.java
字号:
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;
public class Scene {
public int m_nCurIndex;
private TiledLayer m_LyPass; //可通过的区域
private TiledLayer m_LyNotPass; //不可通过的区域
private TiledLayer m_LyJump; //跳转场景的区域
Scene( ){
try{
//读取tile图像
Image image = Image.createImage("/demo/map.png");
m_LyPass = new TiledLayer( 10, 10, image, 32, 32 );
m_LyNotPass = new TiledLayer( 10, 10, image, 32, 32 );
m_LyJump = new TiledLayer( 10, 10, image, 32, 32 );
}catch (IOException e){}
}
//将各图层加入到层管理器
public void AppendToManager( LayerManager mManager ){
mManager.append( m_LyPass );
mManager.append( m_LyNotPass );
mManager.append( m_LyJump );
}
//进入指定场景
//nIndex是场景的编号,mActor、nNpc分别是MM和NPC对象
public void EnterScene( int nIndex, Actor mActor, Npc mNpc ){
m_nCurIndex = nIndex;
InputStream is = null;
//根据场景,设置MM和NPC的位置及图像
switch( nIndex )
{
case 0:
is = getClass().getResourceAsStream("/demo/map0.txt");
mActor.setRefPixelPosition( 150, 150 );
mNpc.setFrame( 0 );
mNpc.SetText( "洞穴上层有个人知道出口!" );
mNpc.setRefPixelPosition( 130, 120 );
break;
default:
is = getClass().getResourceAsStream("/demo/map1.txt");
mActor.setRefPixelPosition( 200, 160 );
mNpc.setFrame( 1 );
mNpc.SetText( "洞穴出口在下层的右下角。" );
mNpc.setRefPixelPosition( 150, 50 );
break;
}
//读取场景
LoadScene( is );
}
//获取场景的宽度
public int GetWidth(){
return m_LyPass.getWidth();
}
//获取场景的高度
public int GetHeight(){
return m_LyPass.getHeight();
}
//检测MM和场景及NPC的碰撞
public void CollidesWidth( Actor mActor, Npc mNpc ){
if( mActor.collidesWith( m_LyJump, false ) )
{
mActor.MoveBack();
if( m_nCurIndex == 0 )
EnterScene( 1, mActor, mNpc );
else
EnterScene( 0, mActor, mNpc );
}
else if( mActor.collidesWith( m_LyNotPass, false ) )
{
mActor.MoveBack();
}
}
//读取场景文件
private void LoadScene( InputStream is ){
try{
int ch = -1;
for( int nRow = 0; nRow < 10; nRow ++ )
{
for( int nCol = 0; nCol < 10; nCol ++ )
{
ch = -1;
while( ( ch < 0 || ch > 7 ) )
{
ch = is.read();
if( ch == -1 )
return;
ch = ch - '0';
}
if( ch == 4 || ch == 5 )
{//不可通过的区域
m_LyPass.setCell( nCol, nRow, 0 );
m_LyNotPass.setCell( nCol, nRow, ch );
m_LyJump.setCell( nCol, nRow, 0 );
}
else if( ch == 6 || ch == 7 )
{//跳转场景的区域
m_LyPass.setCell( nCol, nRow, 0 );
m_LyNotPass.setCell( nCol, nRow, 0 );
m_LyJump.setCell( nCol, nRow, ch );
}
else
{//可通过的区域
m_LyPass.setCell( nCol, nRow, ch );
m_LyNotPass.setCell( nCol, nRow, 0 );
m_LyJump.setCell( nCol, nRow, 0 );
}
}
}
}
catch (IOException e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -