?? sprite.java
字號:
import javax.microedition.lcdui.*;
/**
* 文件名: WestGameCanvas.java <br>
* 作者: 汪霞 <br>
* 創建日期: 2005-12-1 <br>
* 文件功能: <br>
* 版本:1.0 <br>
* 編譯環境:CLDC1.0, MIDP1.0 <br>
* 實現環境:NOKIA60 <br>
* 注釋: <br>
* 修改者姓名: <br>
* 版權聲明:Copyright (c) 2000 GameFactory .All rights reserved. <br>
*/
public class Sprite
{
protected int x, y; //在屏幕上,需要繪制圖片的位置
protected boolean visible; //設置當前圖片的顯示與否
int id;
// 動畫起始索引
private int sequenceIndex;
// 自定義動畫順序
private boolean customSequenceDefined;
// 動畫源圖像
Image sourceImage;
// 動畫幀數
int numberFrames;
// 幀的左上角相對于源圖像的x坐標數組
int frameCoordsX[];
// 幀的左上角相對于源圖像的y坐標數組
int frameCoordsY[];
// 幀的寬度
int srcFrameWidth;
// 幀的高度
int srcFrameHeight;
// 動畫幀的順序數組
int frameSequence[];
// 碰撞檢測塊的左上角x坐標 (相對于一幀)
int collisionRectX;
// 碰撞檢測塊的左上角y坐標 (相對于一幀)
int collisionRectY;
// 碰撞檢測塊的寬度
int collisionRectWidth;
// 碰撞檢測塊的高度
int collisionRectHeight;
// 用 單幀圖像 生成sprite的constructor
public Sprite(int id, Image image)
{
// 調用layer類構造函數,只有一幀的sprite,圖像的大小就是幀的大小
visible = true;
// 調用初始化幀相關事項的函數,對sequenceIndex不做更改(false)
initializeFrames(image, image.getWidth(), image.getHeight(), false);
// 調用初始化碰撞檢測塊的函數
initCollisionRecBounds();
}
// 用 多幀圖像 生成sprite的constructor
public Sprite(int id, Image image, int frameWidht, int frameHeight)
{
// 調用layer類構造函數,初始化幀的大小
visible = true;
// 參數合法化檢測
if (frameWidht < 1 || frameHeight < 1
|| image.getWidth() % frameWidht != 0
|| image.getHeight() % frameHeight != 0)
{
System.out.println("imgWidth=" + image.getWidth() + ", imgHeight="
+ image.getHeight());
System.out.println("frameWidth=" + frameWidht + ", frameHeight="
+ frameHeight);
throw new IllegalArgumentException();
}
else
{
// 調用初始化幀相關事項的函數,對sequenceIndex不做更改(false)
initializeFrames(image, frameWidht, frameHeight, false);
// 調用初始化碰撞檢測塊的函數
initCollisionRecBounds();
this.id = id;
}
}
// 設定動畫起始索引
public void setFrame(int sequenceIndex)
{
// 動畫起始索引范圍限定
if (sequenceIndex < 0 || sequenceIndex >= frameSequence.length)
throw new IndexOutOfBoundsException();
else
{
this.sequenceIndex = sequenceIndex;
}
}
// 取得動畫起始索引
public int getFrame()
{
return sequenceIndex;
}
// 取得動畫順序數組的長度
public int getFrameSequenceLength()
{
return frameSequence.length;
}
// 設定動畫順序數組
public void setFrameSequence(int[] sequence)
{
// 動畫順序數組如果為空
if (null == sequence)
{
// 設定動畫起始索引為0
sequenceIndex = 0;
// 設定自定義動畫順序為fasle
customSequenceDefined = false;
// 按幀的自然順序,設定動畫順序數組
frameSequence = new int[numberFrames];
for (int i = numberFrames - 1; i >= 0; i--)
frameSequence[i] = i;
return;
}
// 出錯處理
if (sequence.length < 1)
throw new IllegalArgumentException();
// 出錯處理:動畫索引范圍檢測
for (int i = sequence.length - 1; i >= 0; i--)
if (sequence[i] < 0 || sequence[i] >= numberFrames)
throw new ArrayIndexOutOfBoundsException();
// 設定自定義動畫順序為true
customSequenceDefined = true;
// 拷貝形參數組 到 動畫順序數組
frameSequence = new int[sequence.length];
System.arraycopy(sequence, 0, frameSequence, 0, sequence.length);
// 設定動畫起始索引為0
sequenceIndex = 0;
}
// 設定碰撞檢測塊范圍 (相對于一幀)
public void defineCollisionRectangle(int x, int y, int w, int h)
{
// 設定碰撞檢測塊各值(左上角是相對于幀的)
collisionRectX = x;
collisionRectY = y;
collisionRectWidth = w;
collisionRectHeight = h;
}
// 與sprite的碰撞檢測
public final boolean collidesWith(Sprite s)
{
// 如果有一方不可見,忽略檢測,返回false
if (!this.visible || !s.visible)
return false;
// 由形參sprite的位置和碰撞檢測塊計算四邊范圍
int thatLeft = s.x + s.collisionRectX;
int thatTop = s.y + s.collisionRectY;
int thatRight = thatLeft + s.collisionRectWidth;
int thatBottom = thatTop + s.collisionRectHeight;
// 由本sprite的位置和碰撞檢測塊計算四邊范圍
int thisLeft = x + collisionRectX;
int thisTop = y + collisionRectY;
int thisRight = thisLeft + collisionRectWidth;
int thisBottom = thisTop + collisionRectHeight;
// 返回碰撞檢測函數的值,true為碰,false為沒碰
return intersectRect(thatLeft, thatTop, thatRight, thatBottom,
thisLeft, thisTop, thisRight, thisBottom);
}
// 與圖片的碰撞檢測
public final boolean collidesWith(Image image, int imgX, int imgY)
{
if (!visible)
return false;
// 由image的位置和寬高計算四邊范圍
int thatLeft = imgX;
int thatTop = imgY;
int thatRight = thatLeft + image.getWidth();
int thatBottom = thatTop + image.getHeight();
// 由本sprite的位置和碰撞檢測塊計算四邊范圍
int thisLeft = x + collisionRectX;
int thisTop = y + collisionRectY;
int thisRight = thisLeft + collisionRectWidth;
int thisBottom = thisTop + collisionRectHeight;
// 返回碰撞檢測函數的值,true為碰,false為沒碰
return intersectRect(thatLeft, thatTop, thatRight, thatBottom,
thisLeft, thisTop, thisRight, thisBottom);
}
// 碰撞檢測處理(針對 矩形范圍)
private boolean intersectRect(int r1Left, int r1Top, int r1Right,
int r1Bottom, int r2Left, int r2Top, int r2Right, int r2Bottom)
{
// 碰撞檢測原理:碰撞的充分必要條件(一下所做的比較是是坐標x,y值的比較)
// rec2的左邊必然小于rec1的右邊
// rec2的右邊必然大于rec1的左邊
// rec2的上邊必然小于rec1的下邊
// rec2的下邊必然大于rec1的上邊
return r2Left < r1Right && r2Right > r1Left && r2Top < r1Bottom
&& r2Bottom > r1Top;
}
// 初始化幀相關事項
private void initializeFrames(Image image, int frameWidth, int frameHeight,
boolean maintainCurFrame)
{
// 計算源圖像大小
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// 計算源圖像行和列的幀數
int numHorizontalFrames = imageWidth / frameWidth;
int numVerticalFrames = imageHeight / frameHeight;
// 更新舊的源圖像
sourceImage = image;
// 更新幀的高寬值
srcFrameWidth = frameWidth;
srcFrameHeight = frameHeight;
// 計算源圖像中幀的總數
numberFrames = numHorizontalFrames * numVerticalFrames;
// 如果maintainCurFrame為false就不改變sequenceIndex
if (maintainCurFrame)
sequenceIndex = 0;
// 更新幀相對于源圖像的x,y坐標數組
frameCoordsX = new int[numberFrames];
frameCoordsY = new int[numberFrames];
// 如果不是自定義順序,更新動畫幀的順序數組
if (!customSequenceDefined)
frameSequence = new int[numberFrames];
// 數組索引值
short currentFrame = 0;
// 這里還可以用單層for循環來完成,不過會用到乘法,而下面的算法只有加法
for (short yy = 0; yy < imageHeight; yy += frameHeight)
{
for (short xx = 0; xx < imageWidth; xx += frameWidth)
{
//************************************************************
// System.out.println("xx=" + xx + ", yy+" + yy);
// 紀錄幀相對于源圖像的x坐標
frameCoordsX[currentFrame] = xx;
// 紀錄幀相對于源圖像的y坐標
frameCoordsY[currentFrame] = yy;
// 如果不是自定義順序,那就按自然順序更新動畫幀的順序數組
if (!customSequenceDefined)
frameSequence[currentFrame] = currentFrame;
// 數組索引值自加一
currentFrame++;
}
}
}
// 初始化碰撞檢測塊
private void initCollisionRecBounds()
{
// 檢測塊的邊界是相對于幀的左上角
collisionRectX = 0;
collisionRectY = 0;
collisionRectWidth = srcFrameWidth;
collisionRectHeight = srcFrameHeight;
}
// 取得源圖像包含幀的總和
public int getRawFrameCount()
{
return numberFrames;
}
// 計算下一幀的索引值
public void nextFrame()
{
sequenceIndex = (sequenceIndex + 1) % frameSequence.length;
}
// 計算上一幀的索引值
public void prevFrame()
{
if (0 == sequenceIndex)
sequenceIndex = frameSequence.length - 1;
else
sequenceIndex--;
}
// 畫出動畫
public final void paint(Graphics g)
{
// 出錯檢測
if (null == g)
throw new NullPointerException();
// 如果本sprite可見,就可以畫
if (visible)
{
//************************************************************
// System.out.println("x=" + x + ", y=" + y + ", srcFrameWidth="
// + srcFrameWidth + ", srcFrameHeight=" + srcFrameHeight);
// 設定畫筆區域
g.setClip(x, y, srcFrameWidth, srcFrameHeight);
//************************************************************
// System.out.println("frameCoordsX="
// + frameCoordsX[frameSequence[sequenceIndex]]
// + ", frameCoordxY="
// + frameCoordsY[frameSequence[sequenceIndex]]);
// 用動畫索引(sequenceIndex),從動畫幀的順序數組(frameSequence),取得幀號
// 用幀號取得該幀相對于源圖像的位置(frameCoordsX,frameCoordsY)
// 向畫筆的x,y位置,在平移(frameCoordsX,frameCoordsY),畫源圖像
g.drawImage(sourceImage, x
- frameCoordsX[frameSequence[sequenceIndex]], y
- frameCoordsY[frameSequence[sequenceIndex]], Graphics.TOP
| Graphics.LEFT);
}
}
// 設定動畫順序數組
public void setFrameSequenceWX(int[] sequence)
{
// 動畫順序數組如果為空
if (null == sequence)
{
// 設定動畫起始索引為0
setFrame(0);
// 按幀的自然順序,設定動畫順序數組
frameSequence = new int[numberFrames];
for (int i = numberFrames - 1; i >= 0; i--)
frameSequence[i] = i;
return;
}
// 出錯處理
if (sequence.length < 1)
throw new IllegalArgumentException();
// 出錯處理:動畫索引范圍檢測
for (int i = sequence.length - 1; i >= 0; i--)
if (sequence[i] >= numberFrames)
throw new ArrayIndexOutOfBoundsException();
// 拷貝形參數組 到 動畫順序數組
frameSequence = new int[sequence.length];
System.arraycopy(sequence, 0, frameSequence, 0, sequence.length);
// 設定動畫起始索引為0
setFrame(0);
}
/**
* 需要繪制圖片的位置
*/
public void setPosition(int x, int y)
{
this.x = x;
this.y = y;
}
public void move(int dx, int dy)
{
x += dx;
y += dy;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -