0

In Java I have created the following class which extends ImageView to create a character... I want to know if it is possible to add another image of the item my character is holding (see method "addWeapon()") which will move when my character moves (see method "warriorMover(..)"). Basically, I was wondering if it is possible to make the "weapon image" sort of a "characteristic/part" of the main character?

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" is a custom method which does exactly that... moves an image to a given location on the screen.

4

1 に答える 1

0

さまざまなアプローチを使用できます。デフォルトのスプライトのコピーを作成し、その上に剣を描画します(スプライトを頻繁に更新する必要がない場合に適したアプローチ)。

または、キャラクタースプライトを描画した後、スプライトの上に剣をレンダリングすることもできます。

render_loop()
_>draw_characterSprite()
__>draw_equipment()
于 2012-04-05T14:21:51.567 に答える