Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Setting Source Rectangle

I'm having a bit of trouble with setting the source rectangle for a sprite I'm having. The sprite itself is split up in 4x4 frames just as the normal character sets, and the "@step" variable is equal to either 0, 1, 2 or 3. I have debugged it to make sure that this variable never exceeds 3 or goes below 0, and it does not.
Code:
  #--------------------------------------------------------------------------

  # * Set Graphic

  #--------------------------------------------------------------------------

  def set_graphic

    return unless self.visible

    # Obtain width and height of frames

    width   = self.bitmap.width / 4

    height  = self.bitmap.height / 4

    self.ox = width / 2

    self.oy = height

    # Direction case

    case @dir

    when Left

      self.src_rect.set(@step * width,0,width,height)

    when Right

      self.src_rect.set(@step * width,height,width,height)

    when Jump

      self.src_rect.set(@step * width,height*2,width,height)

    end

  end
If I do not set a source rectangle with the code, it shows the sprite perfectly fine, but the whole portion of it. I don't know what I'm doing wrong. Might be just something extremely simple that I just can't pinpoint. I have also tried to remove the ox and oy value changes, but all in all I don't see how that'd be a logical fix, anyhow.

Any ideas?

Thanks in advance.
 
If this class is not inheriting method from the hidden Sprite class, I recommend that, instead of using self.src_rect, you actually set the src_rect on the sprite. If it is inheriting from the Sprite class this should theoretically work. Also, have you print-tested to see if this method is being called? And if it is, have you tried placing a print test inside one or more of the case statements, to see if they ever evaluate as true?
 
@Glitchfinder: It inherits RPG::Sprite, and yes, I did state that removing everything inside the method would cause the whole graphic to appear (without being cut the way I want it), so the method basically makes my whole sprite invisible for no apparent reason.

@Dargor: Also I stated in my first post that I tried doing this without the ox and oy lines, to no effect.

EDIT: I fixed the problem. It seemed to be a problem with where I created an instance of the class. No idea why, though. Either way, problem is solved. Thanks.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top