Android变量变得无情连接?

时间:2011-09-10 16:19:21

标签: android

好的,这个只是奇怪的 - 我需要两个Rects用于健康栏(边框和颜色填充)

当我计算第一个矩形(填充)时,我将该值分配给我的条形矩形 - 但是在运行时修改填充矩形时,条形图也是 - 只有通过手动设置BarRectangle的参数才能保持其形状 - 这怎么可能?

变量可以通过简单的赋值永远链接 -

以下是整个代码:

  public class HealthBar {

public int MaxValue;
int CurrentValue;
public int TargetValue;

int margin = 10;
Rect Rectangle;
private  Rect BarRectangle;
int screenWidth;

boolean IsPlayer1;

 Paint paint = new Paint(); 

 int Green;
 int Yellow;
 int Red;

 int opacity = 196;

public HealthBar(boolean isPlayer1, DeviceProperties device, int maxvalue) {

    paint.setARGB(196, 0, 255, 0);
    paint.setStyle(Paint.Style.FILL);

Green = Color.argb(opacity, 0, 255, 128);
Yellow = Color.argb(opacity, 255, 255, 128);
Red = Color.argb(opacity, 255, 0, 128);


MaxValue = maxvalue;
CurrentValue = MaxValue;
TargetValue = MaxValue;


    IsPlayer1 = isPlayer1;

    screenWidth = device.Screen.width();

    if(IsPlayer1) {



        Rectangle = new Rect(
                             margin,                    //x
                             device.Screen.height() - 14, //y
                             margin + MaxValue,           //width
                             device.Screen.height() - 2  //height
                             );



    } else {



        Rectangle = new Rect(
                             device.Screen.width() - margin - MaxValue,                     //x
                             device.Screen.height() - 14, //y
                             device.Screen.width() - margin,          //width
                             device.Screen.height() - 2  //height
                             );




    }


//Assign Bar Rectangle to Rectangle
    BarRectangle = Rectangle;
}


public void Damage(int amount)
{
    TargetValue = CurrentValue - amount;
}

public void Update() 
{

    if (CurrentValue > TargetValue)
    {
        CurrentValue -= 1;

        if (IsPlayer1) 
            Rectangle.right = margin + CurrentValue;
        else
            Rectangle.left = screenWidth - margin - CurrentValue;
    }

    if (TargetValue <= MaxValue * 0.33) {
        paint.setColor(Red);
    } else if (TargetValue <= MaxValue * 0.66) {
        paint.setColor(Yellow);
    } else {
        paint.setColor(Green);
    }


}


public void Draw(ResourceManager rm, Canvas c, Paint p) 
{



    c.drawRect(Rectangle, paint);
   c.drawBitmap(rm.getBitmap("healthbar"), null, BarRectangle, p);

}


    }//End HealthBar

我“固定”它的方式是通过笨拙地分配BarRectangle单独的:

    Rectangle = new Rect(
                             margin,                    //x
                             device.Screen.height() - 14, //y
                             margin + MaxValue,           //width
                             device.Screen.height() - 2  //height
                             );


        BarRectangle = new Rect(
                 margin,                    //x
                 device.Screen.height() - 14, //y
                 margin + MaxValue,           //width
                 device.Screen.height() - 2  //height
                 );

有人可以向我解释如何在COnstructor中分配变量,以便在更新其他变量时更新它的值吗?

现在我有一个可重现的问题 - 我认为这是在分配字符位置之前发生的,我使用自定义类来保存值(Vector2),每当有人更新时,另一个也会。

1 个答案:

答案 0 :(得分:2)

变量RectangleBarRectangle仅引用对象。这意味着通过代码中的“简单分配”

//Assign Bar Rectangle to Rectangle
BarRectangle = Rectangle;

您不会复制内容(即Rect的字段),而只会复制其内容。更改其中一个字段会反映自身,因为它们现在引用相同的实例。

为了创建具有相同内容的新实例(因此不共享引用),您可以使用以下构造函数:

BarRectangle = new Rect(Rectangle);