在Java中,我創建了以下類來擴展ImageView以創建角色......我想知道是否可以添加另一個角色持有的物品圖像(請參閱方法「addWeapon()」),它將當我的角色移動時移動(參見方法「warriorMover(..)」)。基本上,我想知道是否有可能使「武器形象」成爲主角的「特徵/部分」?如何添加圖像作爲其他圖像的特徵?
public class WarriorEntity extends ImageView
{
private kingsColor kingColor;
private weapons weaponId;
private int kingSide;
private int warriorId;
private int nLeft;
private int nTop;
public WarriorEntity(Context context)
{
super(context);
}
public void defineWarrior(kingsColor a1, weapons a2, int a3, int a4, int a5, int a6)
{
kingColor = a1;
weaponId = a2;
kingSide = a3;
warriorId = a4;
nLeft = a5;
nTop = a6;
switch (kingColor)
{
case blue:
this.setImageResource(R.drawable.bluewarrior);
break;
case green:
this.setImageResource(R.drawable.greenwarrior);
break;
case purple:
this.setImageResource(R.drawable.purplewarrior);
break;
case red:
this.setImageResource(R.drawable.redwarrior);
break;
case white:
this.setImageResource(R.drawable.whitewarrior);
break;
case yellow:
this.setImageResource(R.drawable.yellowwarrior);
break;
}
this.setId(warriorId);
this.setOnClickListener(WarriorClick);
setImagexy(this, nLeft, nTop, 20, 30);
}
public void addWeapon()
{
switch (weaponId)
{
case bowandarrow :
break;
case mace :
break;
case spear :
break;
case sword :
// - Here I would want some code to add the weapon image which as if it were part
of the same image as the character... moves/reacts with the character.
break;
}
}
public void warriorMover(int leftX, int topY)
{
setImagexy(this, leftX + 8, topY - 3, 20, 30);
}
「setImagexy」是正是這麼做的...圖像移動到屏幕上的指定位置的自定義方法。
爲什麼不加一個武器類,而不是隻使用一個枚舉,並具有延伸的ImageView以及 – bvulaj 2012-04-05 14:20:45
呀,這其實在這個枚舉只代表了角色持有的武器種類 – 2012-04-05 14:50:52