Want to make Hp gauge bar, but stack on object.width

I tried to make gauge (like hp) by using object.width but the object.width resized to center of the object not the edge.
I tried on scaling but it is hard to calculate when the scale got 0.xx , I got the warning (not error).

Can anybody give me some advice? [import]uid: 65906 topic_id: 15854 reply_id: 315854[/import]

try the setReferencePoint(display.TopLeftReferencePoint)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15854 reply_id: 58595[/import]

@JayantV

I have tried your way at the beginning but it doesn’t work.
!? Are you talking about scaling?? or .width?

For scaling, it works but as I said before when the scale is below than 0 but still not minus, I got the warning.

For the width, I tried to set reference as you mentioned but it still shrinks to the center. But if you mention is for .width, Maybe my code has some error.(my gauge is external file)
[import]uid: 65906 topic_id: 15854 reply_id: 58605[/import]

local hp = display.newRect(200,20)  
hp:setReferencePoint(display.TopLeftReferencePoint)  
hp.x = 10  
hp.y = 30  
  
local function touchScreen(e)  
  
if (e.phase == "began") then  
  
hp.width = hp.width -5  
hp:setReferencePoint(display.TopLeftReferencePoint)  
hp.x = 10  
  
end  
  
end  
Runtime:addEventListener("touch" , touchScreen)  

Try that. I haven’t checked it or anything, but it should get you started. [import]uid: 51654 topic_id: 15854 reply_id: 58606[/import]

what is my way?

when you resize, the object will resize from the center, you need to position it again.

I guess you need to share your code to know what you are doing wrong.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15854 reply_id: 58607[/import]

Thank you everyone, it works with code from 003naveen.
But I still have a question that is it safe for memory to use this code. I want to try this way for a long time but I think it may effect memory or inner performance since it looks like function for initialize than updating (:setReferencePoint(display.TopLeftReferencePoint)), that’s why I ignored it.

Can anybody explain to me? [import]uid: 65906 topic_id: 15854 reply_id: 58659[/import]

Hello guys,

Yet another issue I am having. I was trying to make a “health bar” type graphic and I found this perfect post. Unfortunately my code do not seems to work correctly. Basically the same problem as the first message in this post. It seems that:
setReferencePoint(display.TopLeftReferencePoint)

Is not doing anything?! The rectangle always grow from the center. I am sure I am doing something wrong. Here my code if anybody can found something wrong with, I will be grateful.

[lua]local promotionBar = display.newRect(205,283,100,10) – I TRY USING "display.newRect(100,10) BUT IT GIVES ME AN ERROR (WANTS 4 ARGUMENTS)
–promotionBar.x = 205
–promotionBar.y = 283
promotionBar:setFillColor (255,255,0,255,128)
promotionBar:setReferencePoint(display.TopLeftReferencePoint)
– later in the initVariables section (using director) I have:

promotionBar.width = 50 – 50% just a test
promotionBar.x = 205
promotionBar.y = 283

promotionBar:setReferencePoint(display.TopLeftReferencePoint) – THIS SEEMS TO DO NOTHING?![/lua]

Thank you so much.

Mo [import]uid: 49236 topic_id: 15854 reply_id: 60482[/import]

you place the setReferencePoint *before* you set the x and y co-ordinates

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15854 reply_id: 60539[/import]

OMG! I would never thought of that in a millions years…works like a charm.

THANKS A LOT JayantV. I appreciate it.

Mo [import]uid: 49236 topic_id: 15854 reply_id: 60543[/import]

Hey Jayant,

Taking the boat I am going here also to ask help. I would like to hear from you if possible please.

Would you mind helping me “understand” if I got what you said above for @lemsim regarding his code is right?

So…as you told him, he should firstly place the chunk “setReferencePoint” AFTER set the “x” and “y” coordinates, right?

So I understood it like this:

– We first need to set the coordinates (x,y) of the “object” for ONLY AFTER this set the “setReferencePoint” of that object because if not so the things happen without the chunck/line “setReferencePoint” has a reference for “where” to set the Reference Point on the display and by consequence it gets an error. –

Am I right in my learning thoughts?

Hope you understand myself.
Best Regards,
Rodrigo. [import]uid: 89165 topic_id: 15854 reply_id: 60564[/import]

Hi Rodrigo,
Si, you are correct.

When you create an object the first time like this

 local rect = display.newRect(0,0,100,100)  

the referencePoint is set to the TopLeft and then if you do anything, it changes to Center, why, no idea, but it does. So,

 local rect = display.newRect(0,0,100,100)  
 rect:scale(0.5,0.5)  

Now if you try to get the x and the y, what do you think you will get?

 print(rect.x,rect.y)  

if you want the reference to be in TopLeft co-ordinates, then you need to do is

 rect:setReferencePoint(display.TopLeftReferencePoint)  
 print(rect.x,rect.y)  

I hope that helped you clear your doubts.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15854 reply_id: 60569[/import]

Hey JayantV,

Just a sentence for you:

“Thank You Very MUCH”! like a friend.

Now, after your beautifully explanation I understood about it nicely! And when we really understand it is more difficult to forget! :slight_smile:

PS: I am studying hard btw. As you may know, I am newer in Corona and Lua “worlds” and so I must study because my biggest feeling is to be good at it to produce some apps (my goal are Apps and not games by this time) with my ideas and hard work…and I will. Even I need years to learn,to be good, to produce something util, I will get on and keep it and I am very far from Giving it up at all.

Wishing the best and hope to keep getting awesome helps from you.
Regards,
Rodrigo.
[import]uid: 89165 topic_id: 15854 reply_id: 60574[/import]

Guys, thanks for the sample code above.
But i realize it doesn’t work well if i need to ‘animate’ the HP reduction
e.g.

transition.to(hpBar, {time = 200, width = (hpCurrent / hpMax \* hpWidth)})

even if i try to use onComplete property to reset the reference point, the outcome look ugly
is there a way to manipulate the object width in “TopLeft” reference point?

Thanks for the advice [import]uid: 10373 topic_id: 15854 reply_id: 116101[/import]

@weilies

Hi,

When you declare your variable [lua]hpBar[/lua], are you setting its own Reference Point there? I mean, are you setting the reference point of it after set something else for this variable (as x and y coordinates for example)? Use this API below to set the reference point of the hpBar to topLeft after you`re done with the sets of it: (example below)
[lua]local hpBar = display.newRect(0,0,50,50)
hpBar.x = display.contentWidth * 0.5
hpBar.y = display.contentHeight *0.5
. . .
hpBar:setReferencePoint(display.TopLeftReferencePoint) – so here (at the final set for the variable hpBar) you set it reference point.[/lua]
Did you have something like this already? Not working?
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 15854 reply_id: 116119[/import]