?? movingobject.java
字號:
package com.thinkenjoy.feitian;
import com.thinkenjoy.tools.ImageSet;
/**
*
* 移動物體的基類,任何可移動的物體都繼承此類 此類也可直接表示一些簡單的可移動物體,如子彈
*/
public class MovingObject {
//#if NOK_7260
//# /**
//# * 游戲屏幕寬,高
//# */
//# public static final int canvasWidth = 128;
//# public static final int canvasHeight = 128;
//#else
//#if NOK_7370 || MOT_E2
//# public static final int canvasWidth = 240;
//# public static final int canvasHeight = 320;
//#else
//#if NOK_6230i
//# public static final int canvasWidth = 208;
//# public static final int canvasHeight = 208;
//#else
//#if NOK_6101
//# public static final int canvasWidth = 128;
//# public static final int canvasHeight = 160;
//#else
/**
* 游戲屏幕寬,高
*/
public static final int canvasWidth = 176;
public static final int canvasHeight = 208;
//#endif
//#endif
//#endif
//#endif
/**
* 某種移動物體的編號,即objID
*/
protected int type;
/**
* 能量,即血
*/
public int energy;
/**
* X軸上的速度
*/
protected int speedX;
/**
* Y軸上的速度
*/
protected int speedY;
/**
* 威力
*/
protected int hitPower;
/**
* 是不是已被破壞掉
*/
protected boolean destroy;
/**
* 記錄移動物體的范圍
*/
protected Rectangle curLoc;
//處理動畫幀
public int currentFrame;
public int currentState;
public ImageSet imageSet;
protected long lastFrameChange;
private int totalCycles;
//處理鏈表
protected MovingObject owner;
private MovingObject nextLinked;
private MovingObject prevLinked;
public MovingObject(int objType, int hitPow, int speedX, int speedY,
int locX, int locY, int width, int height) {
energy = 1;
destroy = true; //初始生成時將破壞設為true,避免還沒有具體初始化就處理
curLoc = new Rectangle();
initial(objType, hitPow, speedX, speedY, locX, locY, width, height);
}
public void initial(int objType, int hitPow, int speedX, int speedY,
int locX, int locY, int width, int height) {
type = objType;
this.hitPower = hitPow;
this.speedX = speedX;
this.speedY = speedY;
curLoc.setBounds(locX, locY, width, height);
destroy = false;
}
/**
* 邏輯處理,移動操作
*
*/
public void cycle() {
move(speedX, speedY);
}
/**
* 處理動畫
*
*/
public void animateCycle() {
// 當足夠的時間過后,改變幀
if (imageSet.getTotalFrames(currentState) > 1
&& imageSet.getAnimTime(currentState) > 0) {
long deltaTime = System.currentTimeMillis() - lastFrameChange;
if (deltaTime > imageSet.getAnimTimePerFrame(currentState)) {
currentFrame++;
lastFrameChange = System.currentTimeMillis();
if (currentFrame >= imageSet.getTotalFrames(currentState)) {
currentFrame = 0;
totalCycles++;
}
}
}
}
/**
* @return 精靈活躍的總周期數. 在偵測精靈的活躍期是否結束時特別有用
*/
public final int getTotalCycles() {
return totalCycles;
}
/**
* 重置其范圍,是否已消亡等
*
*/
public void reset() {
currentFrame = 0;
totalCycles = 0;
lastFrameChange = 0;
destroy = true;
}
/**
* 獲取此移動物體的類型
* @return
*/
public int getType() {
return type;
}
/**
* 設置此移動物體的類型
* @param objType
*/
public void setType(int objType) {
type = objType;
}
/**
* 獲得此移動物體的殺傷力
* @return
*/
public int getHitPower() {
return hitPower;
}
/**
* 設置此移動物體的殺傷力
* @param newPower
*/
public void setHitPower(int newPower) {
hitPower = newPower;
}
/**
* X軸上移動
* @param moveoffset
*/
public void moveX(int moveoffset) {
curLoc.moveX(moveoffset);
}
/**
* Y軸上移動
* @param moveoffset
*/
public void moveY(int moveoffset) {
curLoc.moveY(moveoffset);
}
/**
* 移動
* @param moveoffsetX
* @param moveoffsetY
*/
public void move(int moveoffsetX, int moveoffsetY) {
curLoc.moveX(moveoffsetX);
curLoc.moveY(moveoffsetY);
}
/**
* 設置位置
* @param x
* @param y
*/
public void setLocation(int x, int y) {
curLoc.setX(x);
curLoc.setY(y);
}
/**
* 獲得當前的X坐標
* @return
*/
public int getLocationX() {
return curLoc.getX();
}
/**
* 獲得當前的Y坐標
* @return
*/
public int getLocationY() {
return curLoc.getY();
}
/**
* 設置此移動物體的寬度
* @param width
*/
public void setWidth(int width) {
curLoc.setWidth(width);
}
/**
* 獲得此移動物體的寬度
* @return
*/
public int getWidth() {
return curLoc.getWidth();
}
/**
* 設置此移動物體的高度
* @param height
*/
public void setHeight(int height) {
curLoc.setHeight(height);
}
/**
* 獲得此移動物體的高度
* @return
*/
public int getHeight() {
return curLoc.getHeight();
}
/**
* 設置X軸上的速度
* @param speed
*/
public void setSpeedX(int speed) {
speedX = speed;
}
/**
* 獲得X軸上的速度
* @return
*/
public int getSpeedX() {
return speedX;
}
/**
* 設置Y軸上的速度
* @param speed
*/
public void setSpeedY(int speed) {
speedY = speed;
}
/**
* 獲得Y軸上的速度
* @return
*/
public int getSpeedY() {
return speedY;
}
/**
* 消毀
* @param isDestroy
*/
public void destroy(boolean isDestroy) {
destroy = isDestroy;
if (isDestroy)
reset();
}
/**
* 判斷是否已被消毀
* @return
*/
public boolean isDestroy() {
return destroy;
}
/**
* 得到此移動物體對應的矩形
* @return
*/
public Rectangle getBounds() {
return curLoc;
}
/**
* 獲得鏈表的下一個值
* @return
*/
public final MovingObject getNextLinked() {
return nextLinked;
}
/**
* 設置鏈表的下一個值
* @param nextLinked
*/
public final void setNextLinked(MovingObject nextLinked) {
this.nextLinked = nextLinked;
}
/**
* 等到鏈表的前一個值
* @return
*/
public final MovingObject getPrevLinked() {
return prevLinked;
}
/**
* 設置鏈表的前一個值
* @param prevLinked
*/
public final void setPrevLinked(MovingObject prevLinked) {
this.prevLinked = prevLinked;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -