Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:12:45+00:00 2026-05-25T11:12:45+00:00

Ok this one is just bizarre – I needed two Rects for a Healthbar

  • 0

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 one got updated the other would too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:12:45+00:00Added an answer on May 25, 2026 at 11:12 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just could not get this one and googling did not help much either.. First
I have a problem with my WCF Data Service, and this one is just
this one is not homework, it just the fact that i've been out of
this query will just one row as a result myDataContext db = new myDataContext();
I installed from kubuntu's package management this handy pnglite library. It contains just one
I just saw this question and one of the answers looks really appealing to
I just want to clarify one thing. This is not a question on which
I just started getting this error today, seemingly out of nowhere. Any one see
I saw this post: Typos… Just use option strict and explicit please.. during one
Just out of curiosity, asking this Like the expression one below a = (condition)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.