2012-04-05 48 views
0

在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」是正是這麼做的...圖像移動到屏幕上的指定位置的自定義方法。

+0

爲什麼不加一個武器類,而不是隻使用一個枚舉,並具有延伸的ImageView以及 – bvulaj 2012-04-05 14:20:45

+0

呀,這其實在這個枚舉只代表了角色持有的武器種類 – 2012-04-05 14:50:52

回答

0

不同的方法可用於: 使默認精靈的副本,並繪製在它上面的劍(如果一個很好的方法,你就不需要更新精靈往往

或者你可以只呈現在精靈的頂部的劍你畫人物精靈後:

render_loop() 
_>draw_characterSprite() 
__>draw_equipment() 
+0

我的確想過用不同的武器繪製不同的精靈,但是後來我會以每個角色5或6個額外的圖像結束。 – 2012-04-05 14:53:38

+0

你只有一個額外的圖像,你將使用它作爲緩衝區來保存預先繪製的角色與其設備。爲什麼你需要5或6? – Grims 2012-04-05 15:02:25

相關問題