?? herosprite.java
字號:
/**********************************************************
File name:HeroSprite.java
Author:夏文濤
Version:Beta1.0
Download:http://www.codefans.net
Data:2007/10/16
Description:
英雄精靈處理,包括人類的能力數(shù)據(jù)處理,等級處理,英雄移動,
所攜帶的物品(分為工具和裝備),所裝備的物品,英雄狀態(tài)處理,
物品及裝備的使用和刪除.
*********************************************************/
package com.Izual.MetalMax;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class HeroSprite extends Sprite implements IData {
private String heroName = "Izual"; /*英雄名稱*/
private int heroLv = 1; /*英雄等級*/
private static int heroMaxHp = 80; /*英雄最大HP*/
private static int heroHp = 80; /*英雄現(xiàn)有的HP*/
private int heroFightLv = 1; /*英雄戰(zhàn)斗等級*/
private int heroFixLv = 1; /*英雄修理等級*/
private int heroDriveLv = 1; /*英雄駕駛等級*/
private static int heroStr = 10; /*英雄的力量*/
private int heroInt = 10; /*英雄的智力*/
private static int heroSpeed = 20; /*英雄的速度*/
private static int heroPhStr = 15; /*英雄的體力*/
private int heroExp = 0; /*英雄的經(jīng)驗值*/
private int heroMoney = 11150; /*英雄的金錢*/
private static int heroAt = heroStr; /*英雄的攻擊力,等于英雄的力量*/
private static int heroDf = (heroSpeed + heroPhStr) / 2; /*英雄的防御力等于英雄速度和英雄體力之和的一半*/
private static int heroBasicAt = heroStr; /*英雄的基礎(chǔ)攻擊力*/
private static int heroBasicDf = (heroSpeed + heroPhStr) / 2; /*英雄的技術(shù)防御力*/
private int heroLvNeed[] = { 0, 20, 60, 100, 180, 1000 }; /*英雄升級所需經(jīng)驗*/
private int heroItem[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; /*英雄現(xiàn)有的人類工具*/
private int heroEquItem[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; /*英雄現(xiàn)有的人類裝備*/
private int heroEqu[] = { 0, 0, 0, 0, 0, 0 }; /*英雄的已裝備的人類裝備,每位對應(yīng)一個物品種類*/
private int heroState = 0; /*英雄的狀態(tài)*/
int nowdown[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; /*英雄向下走的動畫數(shù)組*/
int nowleft[] = { 4, 4, 5, 5, 6, 6, 7, 7 }; /*英雄向左走的動畫數(shù)組*/
int nowright[] = { 8, 8, 9, 9, 10, 10, 11, 11 }; /*英雄向右走的動畫數(shù)組*/
int nowup[] = { 12, 12, 13, 13, 14, 14, 15, 15 }; /*英雄向上走的動畫數(shù)組*/
int sright[] = {8}; /*英雄被老爸拉走時的動畫數(shù)組 - -!*/
int mDir = -1; /*英雄的方向*/
int mSpeed = 2; /*英雄的速度,即每步移動的像素值*/
public static boolean mapChanged = false; /*地圖是否切換*/
public int pX = MapManager.heroPx, pY = MapManager.heroPy; /*英雄的坐標*/
private int screenWidth = MapManager.screenWidth, /*地圖大小*/
screenHeight = MapManager.screenHeight;
/*構(gòu)造函數(shù),根據(jù)圖片和大小構(gòu)造英雄精靈*/
public HeroSprite(Image img, int w, int h) {
super(img, w, h);
setPosition(pX, pY);
}
/*設(shè)置英雄方向為向下*/
public void setDown() {
setFrameSequence(nowdown);
}
/*設(shè)置英雄方向為向左*/
public void setLeft() {
setFrameSequence(nowleft);
}
/*設(shè)置英雄方向為向右*/
public void setRight() {
setFrameSequence(nowright);
}
/*設(shè)置英雄方向為向上*/
public void setUp() {
setFrameSequence(nowup);
}
/*************************************************
Function: moveUp(boolean)
Description: 人類精靈向上移動的方法
Calls: setFreameSequenve(int[]),nextFrame(),move(int,int).
Called By: MetalMaxCanvas.java
Input: doit:是否真正移動,碰撞檢測時會用到.
Output: 無
Return: 無
Others: 無
*************************************************/
public void moveUp(boolean doit) throws IOException {
/*如果英雄的方向不是向上,則設(shè)置方向為上,動畫為向上的動畫*/
if (mDir != UP) {
mDir = UP;
setFrameSequence(nowup);
}
/*下一禎動畫*/
nextFrame();
/*如果真正移動,則進行移動,更新英雄坐標,否則向相反方向移動一步*/
if (doit) {
move(0, -mSpeed);
this.pY = Math.max(0, pY - mSpeed);
} else {
move(0, mSpeed);
}
}
/*英雄向下移動方法*/
public void moveDown(boolean doit) throws IOException {
if (mDir != DOWN) {
mDir = DOWN;
setFrameSequence(nowdown);
}
nextFrame();
if (doit) {
move(0, mSpeed);
this.pY = Math.min(screenHeight, pY + mSpeed);
} else {
move(0, -mSpeed);
}
}
/*英雄向左移動方法*/
public void moveLeft(boolean doit) throws IOException {
if (mDir != LEFT) {
mDir = LEFT;
setFrameSequence(nowleft);
}
nextFrame();
if (doit) {
move(-mSpeed, 0);
this.pX = Math.max(0, pX - mSpeed);
} else {
move(mSpeed, 0);
}
}
/*英雄向右移動方法*/
public void moveRight(boolean doit) throws IOException {
if (mDir != RIGHT) {
mDir = RIGHT;
setFrameSequence(nowright);
}
nextFrame();
if (doit) {
move(mSpeed, 0);
this.pX = Math.min(screenWidth, pX + mSpeed);
} else {
move(-mSpeed, 0);
}
}
/*特殊的向左移動,被老爸拉走那一段用 - -!*/
public void moveSL(){
setFrameSequence(sright);
move(-mSpeed, 0);
}
/*獲取事件ID,已經(jīng)廢除*/
/*
public int eventActionExist() {
int mSpeed = 16;
int x = 0;
int y = 0;
int ID = 0;
if (MetalMaxCanvas.way == MetalMaxCanvas.UP_PRESSED) {
if ((pY - mSpeed) % 16 == 0) {
x = pX / 16;
y = (pY - mSpeed) / 16;
ID = MapManager.getEvent(y, x);
// System.out.println("up" + ID);
return ID;
} else
return 0;
}
if (MetalMaxCanvas.way == MetalMaxCanvas.DOWN_PRESSED) {
if ((pY + FrameHeight) % 16 == 0) {
x = pX / 16;
y = (pY + getHeight() + mSpeed) / 16;
ID = MapManager.getEvent(y, x);
// System.out.println("down" + ID);
return ID;
} else
return 0;
}
if (MetalMaxCanvas.way == MetalMaxCanvas.LEFT_PRESSED) {
if (pX % 16 == 0) {
x = (pX - mSpeed) / 16;
y = pY / 16 + 1;
ID = MapManager.getEvent(y, x);
// System.out.println("left" + ID);
return ID;
} else
return 0;
}
if (MetalMaxCanvas.way == MetalMaxCanvas.RIGHT_PRESSED) {
if ((pX + FrameWidth) % 16 == 0) {
x = (pX + mSpeed + getWidth()) / 16;
y = pY / 16 + 1;
ID = MapManager.getEvent(y, x);
// System.out.println("right" + ID);
return ID;
} else
return 0;
}
return 99;
}
*/
/*更新英雄的坐標*/
public void setXY(int pX, int pY) {
this.pX = pX;
this.pY = pY;
}
/*---------------------以下為get類----------------------------*/
/*獲取英雄攻擊力*/
public static int getHeroAt() {
return heroAt;
}
/*獲取英雄基礎(chǔ)攻擊力*/
public static int getHeroBasicAt() {
return heroBasicAt;
}
/*獲取英雄防御力*/
public static int getHeroDf() {
return heroDf;
}
/*獲取英雄基礎(chǔ)防御力*/
public static int getHeroBasicDf() {
return heroBasicDf;
}
/*獲取英雄現(xiàn)有HP*/
public static int getHeroHp() {
return heroHp;
}
/*獲取英雄經(jīng)驗值*/
public int getHeroExp() {
return heroExp;
}
/*獲取英雄名稱*/
public String getHeroName() {
return heroName;
}
/*獲取英雄等級*/
public int getHeroLv() {
return heroLv;
}
/*獲取英雄金錢*/
public int getHeroMoney() {
return heroMoney;
}
/*獲取英雄升級所需經(jīng)驗*/
public int getHeroLvNeed(int lv) {
return heroLvNeed[lv];
}
/*獲取英雄最大HP*/
public int getHeroMaxHp() {
return heroMaxHp;
}
/*獲取英雄已有的工具*/
public int[] getHeroItem() {
return heroItem;
}
/*獲取英雄的已有裝備物品*/
public int[] getHeroEquItem() {
return heroEquItem;
}
/*獲取英雄已已裝備的物品*/
public int[] getHeroEqu() {
return heroEqu;
}
/*獲取英雄的戰(zhàn)斗等級*/
public int getHeroFightLv() {
return heroFightLv;
}
/*獲取英雄的修理等級*/
public int getHeroFixLv() {
return heroFixLv;
}
/*獲取英雄的駕駛等級*/
public int getHeroDriveLv() {
return heroDriveLv;
}
/*獲取英雄的力量*/
public int getHeroStr() {
return heroStr;
}
/*獲取英雄的智力*/
public int getHeroInt() {
return heroInt;
}
/*獲取英雄的速度*/
public int getHeroSpeed() {
return heroSpeed;
}
/*獲取英雄的體力*/
public int getHeroPhStr() {
return heroPhStr;
}
/*獲取英雄的狀態(tài)*/
public int getHeroState() {
return heroState;
}
/*-------------------以下為set類-----------------------------*/
/*更新英雄的攻擊力*/
public static void setHeroAt(int newHeroAt) {
HeroSprite.heroAt = newHeroAt;
}
/*更新英雄的防御力*/
public static void setHeroDf(int newHeroDf) {
HeroSprite.heroDf = newHeroDf;
}
/*更新英雄的現(xiàn)有HP*/
public static void setHeroHp(int newHeroHp) {
HeroSprite.heroHp = Math.min(newHeroHp, heroMaxHp);
}
/*更新英雄的經(jīng)驗值*/
public void setHeroExp(int newHeroExp) {
this.heroExp = newHeroExp;
}
/*更新英雄的等級*/
public void setHeroLv(int newHeroLv) {
this.heroLv = newHeroLv;
}
/*更新英雄的金錢*/
public void setHeroMoney(int newHeroMoney) {
this.heroMoney = newHeroMoney;
}
/*更新英雄的最大HP*/
public void setHeroMaxHp(int newHeroMaxHp) {
HeroSprite.heroMaxHp = newHeroMaxHp;
}
/*更新英雄的戰(zhàn)斗等級*/
public void setHeroFightLv(int newHeroFightLV) {
this.heroFightLv = newHeroFightLV;
}
/*更新英雄的修理等級*/
public void setHeroFixLv(int newHeroFixLv) {
this.heroFixLv = newHeroFixLv;
}
/*更新英雄的駕駛等級*/
public void setHeroDriveLv(int newHeroDriveLv) {
this.heroDriveLv = newHeroDriveLv;
}
/*更新英雄的力量*/
public void setHeroStr(int newHeroStr) {
HeroSprite.heroStr = newHeroStr;
}
/*更新英雄的智力*/
public void setHeroInt(int newHeroInt) {
this.heroInt = newHeroInt;
}
/*更新英雄的速度*/
public void setHeroSpeed(int newHeroSpeed) {
HeroSprite.heroSpeed = newHeroSpeed;
}
/*更新英雄的體力*/
public void setHeroPhStr(int newHeroPhStr) {
HeroSprite.heroPhStr = newHeroPhStr;
}
/*更新英雄的基礎(chǔ)攻擊力*/
public static void setHeroBasicAt(int newHeroBasicAt) {
HeroSprite.heroBasicAt = newHeroBasicAt;
}
/*更新英雄的基礎(chǔ)防御力*/
public static void setHeroBasicDf(int newHeroBasicDf) {
HeroSprite.heroBasicDf = newHeroBasicDf;
}
/*更新英雄的狀態(tài)*/
public void setHeroState(int newHeroState) {
this.heroState = newHeroState;
}
/*------------------------物品處理---------------------*/
/*英雄裝備物品,設(shè)置相應(yīng)的數(shù)組位(對應(yīng)于人類裝備類型)為人類裝備的編號*/
public void addHeroEqu(int equItemID, int type) {
heroEqu[type] = equItemID;
}
/*增加人類工具*/
public void addHeroItem(int itemID) {
/*找到人類工具數(shù)組中為0的項,更新為增加的物品編號*/
for (int i = 0; i < heroItem.length; i++) {
if (heroItem[i] == 0) {
heroItem[i] = itemID;
break;
}
}
}
/*刪除人類工具*/
public void delHeroItem(int itemID) {
/*找到人類工具數(shù)組中值相等的項,設(shè)置為0*/
for (int i = 0; i < heroItem.length; i++) {
if (heroItem[i] == itemID) {
heroItem[i] = 0;
break;
}
}
}
/*增加人類裝備*/
public void addHeroEquItem(int equItemID) {
for (int i = 0; i < heroEquItem.length; i++) {
if (heroEquItem[i] == 0) {
heroEquItem[i] = equItemID;
break;
}
}
}
/*刪除人類裝備*/
public void delHeroEquItem(int equItemID) {
for (int i = 0; i < heroEquItem.length; i++) {
if (heroEquItem[i] == equItemID) {
heroEquItem[i] = 0;
break;
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -