Android variables become inexorably linked?
Ok this one is just bizarre - I needed two Rects for a Healthbar (border and color filling)
When I calculated the first rectangle (filling) I assigned that value to my bar rectangle - but at runtime when the filling rectangle is modified the bar is also - only by manually setting the parameters of the BarRectangle was I able to keep its shape-- how is this even possible?
Can variables become linked forever through simple assignment-
Here is the entirety of the code:
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
The way I "fixed" it was by clunkily assigning the BarRectangle seperate:
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
);
Can anyone explain to me how assigning a variable in the COnstructor somehow makes it update it's value whenever the other variable is updated?
Now that I've got a reproducable issue - I think this has happened before when it came to assigning character positions, I was using a custom class to hold the values (Vector2) and whenever o开发者_如何学Pythonne got updated the other would too.
The variables Rectangle
and BarRectangle
are reference to an object only. This means that by "simple assignment" like in your code
//Assign Bar Rectangle to Rectangle
BarRectangle = Rectangle;
you do not copy the contents (i.e. fields of your Rect
) but only its reference. Changing one of the fields reflects itself in both since they now refer to the same instance.
In order to create a new instance with the same content (and therefore not share a reference) you can use the following constructor:
BarRectangle = new Rect(Rectangle);
精彩评论