src_rect.height problem

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I have some problem over src_rect.height. I am using an image as a representation of the said sprite:

def initialize super(nil) self.bitmap = Cache.picture('human_vein') src_rect.height = $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100 endNote that this is created in a new class inherited in a sprite class. There is nothing wrong in this code, however, my problem is how it handles it in the update method.

def update super src_rect.height = $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100 endInstead of decreasing downwards, it decreases upwards. Say you use a Heart as an image, instead of it decreasing from above, it decreases from below. 

Is there a way to reverse this function so it would decrease downwards?

My problem is graphed this way:

 

cremnophobia

Veteran
Veteran
Joined
Dec 10, 2013
Messages
216
Reaction score
97
Primarily Uses
src_rect.y = …?
 
Last edited by a moderator:

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
I think the problem is, that the pivot point is top left. So you would need to change the y position of the rect depending on the loss of the src_height.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
So it means y = src_rect.height or I should use the y instead of the height?
 

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
You would need a max height for your src_rect.

And the position then would look something like this:

src_rect.y = @max_height - src_rect.height
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
How would I set up the max height correctly? Because the max height should be my max HP...

Code:
class Life < Sprite def initialize    super(nil)    self.bitmap = Cache.picture('human_vein')    src_rect.height = $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100  end  #--------------------------------------------------------------------------  # ● Update  #--------------------------------------------------------------------------  def update    super    src_rect.height = actor_life  end  #--------------------------------------------------------------------------  # ● actor_life  #--------------------------------------------------------------------------  def actor_life    return $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100  endend
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Actually that is correct. When drawing things in a program, the Y axis is on the top of the screen instead of the bottom. Seem weird? It does at first, but the logic behind it makes sense. Say you wanted to save an image in our favorite image format, such as png. When you think to save it, you'd think to start saving from the top left pixel and move right and down, correct? That's how they are saved. Then, say you wanted to re-draw that saved image on the screen. If the y axis were on the bottom of the screen, and we drew each line with y going up by for for each row (see spoiler below for example), the image would be drawn upside-down, because we saved top to bottom, and draw it bottom to top.

for y in 0..50 do for x in 0..50 do draw_pixel(x, y, color) endend 
If you want to change your sprite so that it gets shorter, but the bottom stays where it is, you'll need to change both the src_rect's height and y coordinate. Something like this:

def update super src_rect.height = $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100 src_rect.y = 100 - src_rect.heightendNote: I'm not 100% sure if the above will work bug-free for you, I just assumed you're using a 100-pixel high image.

When the src_rect's height it at 100, the src_rect's y will be 0 (100 - 100). As the height decreases, the y will increase because we are subtracting the height from 100. Try it out and see if that works.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
The above example solves the decrease from above issue, but there will still be a problem.

While you got your reversed effect, the sprite's Y itself will not be changed with just that code alone.

What this means?

That your image will actually seem to move upwards as the image gets smaller in height.

To counter this, you will need to adjust the sprite's Y itself too, not just the src_rect's Y position.

Here is how I do it when I need it (ignore the "fill rate", it's just an example):

class SpritedBar1 < Sprite_Base def initialize super(nil) self.x = 100; self.y = 200 # <-- Just some hardcoded values. # This will save the original Y position of the sprite itself, # which we will use to adjust the Y pos correctly later on. @ori_y = self.y # <--/ self.bitmap = Cache.picture('equipped') # <-- The pic. # The magic of correctly setting the fill value. No need for any hard-coded # value here, we got our bitmap set up before this, so we can use that to # determine the max height easily. | src_rect.height = self.bitmap.height - $game_variables[12].to_f/10 # <--/ # And the magic to move the displayed bitmap's Y position. src_rect.y = self.bitmap.height - src_rect.height # <--/ end def update super update_fill_rate end def update_fill_rate # Add a condition to the update method, pretty please? >.> if src_rect.height != self.bitmap.height - $game_variables[12].to_f/10 src_rect.height = self.bitmap.height - $game_variables[12].to_f/10 src_rect.y = self.bitmap.height - src_rect.height # The line below is the missing thing that no one mentioned before. # This sets the sprite's Y position depending of the current src_rect.y # value, so it will not look like your picture is moving upwards. self.y = @ori_y + src_rect.y # <--/ end end endYou will need to use the sprite's original Y always when you are changing the Y position of it!Ohh, and almost forgot...

The above code is to do the actual "fill rate", it won't account for a background of a HP bar, for example.

If you want to use this as some kind of HP bar, you will need to use a different sprite for the background of your HP bar.

Place it at the same position as your "fill rate pic", and make it's z value 1 less than the fill rate sprite's.
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Somehow, there's a problem. The image does appear but then, it goes the same way, it is still on the reverse mode. This is the implementation:

Code:
class Heart_Life < Sprite  #--------------------------------------------------------------------------  # ● Initialization Process  #--------------------------------------------------------------------------  def initialize    super(nil)    create_case("bg", 0, 300, 1)    create_back_sprite("back", 0, 300, 2)    self.bitmap = Cache.picture("heart")    self.z = 3    self.x = 0    self.y = 300    @ori_y = self.y                                       src_rect.height = self.bitmap.height - $game_party.members[0].hp.to_f/10    src_rect.y = self.bitmap.height - src_rect.height # <--/      end  #--------------------------------------------------------------------------  # ● Update  #--------------------------------------------------------------------------  def update    super    src_rect.height = actor_life  end    def update_fill_rate    if src_rect.height != self.bitmap.height - $game_party.members[0].hp.to_f/10      src_rect.height = self.bitmap.height - $game_party.members[0].hp.to_f/10      src_rect.y = self.bitmap.height - src_rect.height      self.y = @ori_y + src_rect.y # <--/    end  end      #--------------------------------------------------------------------------  # ● actor_life  #--------------------------------------------------------------------------  def actor_life    return $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100  end  #--------------------------------------------------------------------------  # ● create_case  #--------------------------------------------------------------------------  def create_case(spr, x, y, z)    @case = Sprite.new    @case.bitmap = Cache.picture(spr)    @case.x = x    @case.y = y    @case.z = z  end  #--------------------------------------------------------------------------  # ● create_back_sprite  #--------------------------------------------------------------------------  def create_back_sprite(spr, x, y, z)    @back = Sprite.new    @back.x = x    @back.y = y    @back.z = z    @back.bitmap = Cache.picture(spr)  end    #--------------------------------------------------------------------------  # ● Dispose  #--------------------------------------------------------------------------  def dispose    super    @back.bitmap.dispose    @case.bitmap.dispose  end    end
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Quote

The above example solves the decrease from above issue, but there will still be a problem.

While you got your reversed effect, the sprite's Y itself will not be changed with just that code alone.
What this means?
That your image will actually seem to move upwards as the image gets smaller in height.

To counter this, you will need to adjust the sprite's Y itself too, not just the src_rect's Y position.
Here is how I do it when I need it (ignore the "fill rate", it's just an example):

class SpritedBar1 < Sprite_Base def initialize super(nil) self.x = 100; self.y = 200 # <-- Just some hardcoded values. # This will save the original Y position of the sprite itself, # which we will use to adjust the Y pos correctly later on. @ori_y = self.y # <--/ self.bitmap = Cache.picture('equipped') # <-- The pic. # The magic of correctly setting the fill value. No need for any hard-coded # value here, we got our bitmap set up before this, so we can use that to # determine the max height easily. | src_rect.height = self.bitmap.height - $game_variables[12].to_f/10 # <--/ # And the magic to move the displayed bitmap's Y position. src_rect.y = self.bitmap.height - src_rect.height # <--/ end def update super update_fill_rate end def update_fill_rate # Add a condition to the update method, pretty please? >.> if src_rect.height != self.bitmap.height - $game_variables[12].to_f/10 src_rect.height = self.bitmap.height - $game_variables[12].to_f/10 src_rect.y = self.bitmap.height - src_rect.height # The line below is the missing thing that no one mentioned before. # This sets the sprite's Y position depending of the current src_rect.y # value, so it will not look like your picture is moving upwards. self.y = @ori_y + src_rect.y # <--/ end end endYou will need to use the sprite's original Y always when you are changing the Y position of it!

Ohh, and almost forgot...
The above code is to do the actual "fill rate", it won't account for a background of a HP bar, for example.
If you want to use this as some kind of HP bar, you will need to use a different sprite for the background of your HP bar.
Place it at the same position as your "fill rate pic", and make it's z value 1 less than the fill rate sprite's.
This doesn't work. your condition in the update_fill method is wrong. What is in variable 12? It's never been mentioned before. you were right about the picture moving up, though, so I fixed that:

class SpritedBar1 < Sprite_Base def initialize super(nil) self.x = 100; self.y = 200 @ori_y = self.y self.bitmap = Cache.picture('bar') end def update super update_fill_rate end def update_fill_rate src_rect.height = $game_party.members[0].hp.to_f / $game_party.members[0].mhp * 100 src_rect.y = 100 - src_rect.height self.y = @ori_y - src_rect.height endendI don't find much of a need to add a condition before changing these values because we're not redrawing it each time, just updating what part Ace draws to the screen.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
As I wrote, my fill rate was just an example.


Replacing it takes just 3 seconds top, right? :p


Same with the condition.


And why would you update it, when it is not needed? It is still a performance question, as minor as it is, and I take those seriously, as you can tell.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Ahhh there it goes, it works :) Thanks both of you, I learned a lot here. I was laughing because my src_rect.height was doing it wrong.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,863
Messages
1,017,053
Members
137,571
Latest member
grr
Top