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

📄 grass.java

📁 一个j2me的手机程序
💻 JAVA
字号:
第七章



/*TiledLayer类

  象前面提到的,TiledLayer与Sprite类很相似,只是TiledLayer类包含多列,每一列被一套独立的图象帧绘制。另一个区别是TiledLayer类中缺乏大多数功能性相关的方法;TiledLayer没有转换,参考象素,甚至帧顺序。

  当然, 同时管理多个图象使事情变得复杂,我将用TiledLayer的子类Grass来说明TiledLayer类的使用。该类在屏幕背景上显示一排前后随风舞动的绿草。为了使画面更加有趣,某些列拥有舞动的绿草,而另一些是低矮的草丛,仅仅是一层覆盖地面的绿色。

  创建TiledLayer的第一步是决定你需要的格子的行列数。如果你不想让你的图层看上去是简单的矩形,那不是问题,因为没有用到的网格默认设置为空,这就避免了它们和别的图象一样排列。在我的样例中,如Listing 7所示,我只安排了一行,而列数是根据屏幕的宽度计算得到。

  一旦你设置了要用到的行数和列数,你就可以用一个饰片来填充网格,方法是setCell(int col, int row, int tileIndex) ,“Sprite类”章中解释了tileIndex参数。如果你希望某些网格被活动图象填充,你需要通过调用createAnimatedTile(int staticTileIndex)方法创建一个活动饰片,该方法将返回装饰片的索引给你的新的饰片。你尽可以多做几个活动饰片,但要记住,如果你想让网格同时显示同样的动画,必须每一个饰片都要能在多个网格中使用。

  在我的例子中,我只创建了一个活动饰片并且一直重用它,因为我想让我的所有活动绿草同时摇曳。网格被设置在Listing 7 Grass的创建方法中。为让饰片动起来,你无须使用象Sprite用到的事先定义好的帧顺序,所以你必须通过setAnimatedTile(int animatedTileIndex, int staticTileIndex)方法设置帧。这个方法设置了当前所有包含给定活动饰片的帧,从而,所有包含该活动饰片的网格相对应的animatedTileIndex将被变成当前参数staticTileIndex指定的图象。为了使动画更改变得简单,增加自己的帧顺序功能是必要的;请参考Grass.advace(int tickCount)方法看这个功能是如何实现的。

  Listing 7. Grass.java*/ 



import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
* This class draws the background grass.
*
* @author Carol Hamer
*/
public class Grass extends TiledLayer {

//---------------------------------------------------------
// Dimension fields
// (constant after initialization)

/**
* The width of the square tiles that make up this layer.
*/
static final int TILE_WIDTH = 20;

/**
* This is the order that the frames should be displayed
* for the animation.
*/
static final int[] FRAME_SEQUENCE = { 2, 3, 2, 4 };

/**
* This gives the number of squares of grass to put along
* the bottom of the screen.
*/
static int COLUMNS;

/**
* After how many tiles does the background repeat.
*/
static final int CYCLE = 5;

/**
* The fixed Y coordinate of the strip of grass.
*/
static int TOP_Y;

//---------------------------------------------------------
// Instance fields

/**
* Which tile you are currently on in the frame sequence.
*/
private int mySequenceIndex = 0;

/**
* The index to use in the static tiles array to get the
* animated tile.
*/
private int myAnimatedTileIndex;

//---------------------------------------------------------
// Gets / sets

/**
* Takes the width of the screen and sets my columns
* to the correct corresponding number.
*/
static int setColumns(int screenWidth) {
   COLUMNS = ((screenWidth / 20) + 1)*3;
   return(COLUMNS);
}

//---------------------------------------------------------
// Initialization

/**
* Constructor initializes the image and animation.
*/
public Grass() throws Exception {
super(setColumns(JumpCanvas.DISP_WIDTH), 1,
Image.createImage("/images/grass.png"),
TILE_WIDTH, TILE_WIDTH);
TOP_Y = JumpManager.DISP_HEIGHT - TILE_WIDTH;
setPosition(0, TOP_Y);
myAnimatedTileIndex = createAnimatedTile(2);
for(int i = 0; i < COLUMNS; i++) {
if((i % CYCLE == 0) || (i % CYCLE == 2)) {
setCell(i, 0, myAnimatedTileIndex);
} else {
setCell(i, 0, 1);
}
}
}

//---------------------------------------------------------
// Graphics

/**
* Sets the grass back to its initial position.
*/
void reset() {
setPosition(-(TILE_WIDTH*CYCLE), TOP_Y);
mySequenceIndex = 0;
setAnimatedTile(myAnimatedTileIndex, FRAME_SEQUENCE[mySequenceIndex]);
}

/**
* Alter the background image appropriately for this frame.
* @param left Whether the player is moving left.
*/
void advance(int tickCount) {
if(tickCount % 2 == 0) { // Slow the animation down a little.
mySequenceIndex++;
mySequenceIndex %= 4;
setAnimatedTile(myAnimatedTileIndex, FRAME_SEQUENCE[mySequenceIndex]);
}
}

}
 

⌨️ 快捷键说明

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