「キャラクタを複数だす。」
キャラクタを複数出す方法として、キャラクタのクラスを作ることをお勧めします。
キャラクタクラス |
説明 |
class Character {
// chaNo:キャラクタ種類
// 0:ピクシー,1:シルフ,2:サラマンダ,3:ドライアート,
//4:ウインディーネ
// 5:歩兵,6:騎士,7:魔道師,8:僧侶,9:弓兵
private int chaNo;
private int x, y;//配列マップ上のx,y
private int px,py;//描画マップ上のpx,py
// st:強さ,hp:ヒットポイント,ido:移動量
private int st,hp,ido;
static Image chaImage;
slg2 slg2;
Character(Image chaImage,int chaNo,
int x, int y, Applet applet) {
this.chaImage=chaImage;
this.chaNo = chaNo;
this.x = x;this.y = y;slg2 = (slg2) applet;
switch (chaNo) {
case 0: //0:ピクシー
this.st =10;this.hp =10;this.ido=5;
break;
case 1: //1:シルフ
this.st =10;this.hp =10;this.ido=7;
break;
case 2: //2:サラマンダ
this.st =20;this.hp =10;this.ido=3;
break;
case 3: //3:ドライアート
this.st =0;this.hp =10;this.ido=3;
break;
case 4: //4:ウインディーネ
this.st =10;this.hp =10;this.ido=3;
break;
case 5: //5:歩兵
this.st =10;this.hp =10;this.ido=4;
break;
case 6: //6:騎士
this.st =20;this.hp =10;this.ido=3;
break;
case 7: //7:魔道師
this.st =20;this.hp =10;this.ido=2;
break;
case 8: //8:僧侶
this.st =0;this.hp =10;this.ido=2;
break;
case 9: //9:弓兵
this.st =20;this.hp =10;this.ido=3;
break;
}
}
// キャラクタ種の取得
public int getChr() {return chaNo;}
// キャラクタの強さ
public int getSt() {return st;}
// キャラクタのヒットポイント
public int getHp() {return hp;}
// キャラクタの移動量
public int getIdo() {return ido;}
// x座標置のセット
public void setPointX(int x){this.x=x;}
// y座標置のセット
public void setPointY(int y){this.y=y;}
// x座標置の取得
public int getPointX(){return x;}
// y座標置の取得
public int getPointY(){return y;}
//本体描画
public void paint(Graphics g,int center_x,int center_y){
if ((x>center_x-5)&(x<center_x+5)
&(y>center_y-5)&(y<center_y+5)){
px = (x - center_x + 5) * slg2.Size;
py = (y - center_y + 5) * slg2.Size;
g.drawImage(slg2.CharImage[chaNo], px, py, slg2);
}
}
} |
個別パラメータ |
キャラ |
強さ |
生命 |
移動量 |
X |
Y |
chaNo:0 |
10 |
10 |
5 |
8 |
9 |
14 |
17 |
chaNo:1 |
10 |
10 |
7 |
9 |
9 |
14 |
15 |
chaNo:2 |
20 |
10 |
3 |
6 |
10 |
12 |
16 |
chaNo:3 |
0 |
10 |
3 |
6 |
10 |
12 |
16 |
chaNo:4 |
10 |
10 |
3 |
6 |
8 |
12 |
18 |
chaNo:5 |
10 |
10 |
4 |
19 |
6 |
23 |
13 |
chaNo:6 |
10 |
10 |
3 |
18 |
9 |
20 |
14 |
chaNo:7 |
20 |
10 |
2 |
18 |
7 |
23 |
15 |
chaNo:8 |
0 |
10 |
2 |
18 |
5 |
22 |
14 |
chaNo:9 |
20 |
10 |
3 |
21 |
7 |
22 |
10 |
|
【一言メモ】
キャラクタ毎にHPやSTを設定できるようにしておくと、戦闘による経験値やアイテムゲットによるレベルアップをプログラム化しやすいです。
今回の例では入れていませんが、例えばキャラクタクラス側に、
public void levelup(){
this.st++;this.hp++;
}
を追加し、制御側から
character(指定).levelup();
とすることで、指定キャラのHP,STをアップすることが出来ます。
|
|
上のキャラクタクラスを初期化(インスタンス化)することで、キャラクタ毎に操作できるようになります。
//{キャラ番号,x軸,y軸}
int charposi[][]={
{0,14,17},{0,8,9},{1,9,9},{1,14,15},
{2,9,12},{2,11,14},{3,12,16},{3,6,10},
{4,6,8},{4,12,18},{5,19,6},{5,23,13},
{6,18,9},{6,20,14},{7,23,15},{7,18,7},
{8,22,14},{8,18,5},{9,22,10},{9,21,7}
};
// キャラクタの配列初期化
character =new Character[20];
for(int i=0;i<20;i++){
character[i] = new Character(CharImage[charposi[i][0]],charposi[i][0],charposi[i][1],charposi[i][2],this);
}
|
後は、実際の動作側でそれぞれのキャラクタ動作を行います。
|