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.

moving the viewport around on key input

I'm a total beginner with RGSS, what I'm trying to do is I have an image displayed that is really big, but you can only see some of it in the viewport. I want to make it where when you hold UP on your keyboard, the part of the image that you see in the viewport will go up, basically like pressing up will scroll the image up. this is done in a menu. here is what I have as my window

class Window_Image < Window_Base
def initialize
super(10, 10, 620, 460)
self.contents = Bitmap.new(width - 32, height - 32)
@map = RPG::Cache.picture("image.png")
@rect = Rect.new(20, 20, 600, 440)
@view = Viewport.new(@rect)
@sprite = Sprite.new(@view)
@sprite.z = 0
@sprite.bitmap = @map
@view.z = 5001
@view.oy = 2000
@view.ox = 1500
refresh
end

def refresh
if Input.repeat?(Input::UP)
@view.ox = @view.ox + 100
end
self.contents.clear
self.contents.font.color = normal_color
self.contents.font.size = 20
end
end

This part is my (failed) attempt at doing what I want it to do, I don't know if I'm even on the right track or where it should really go, i Just kind of guessed.

if Input.repeat?(Input::UP)
@view.ox = @view.ox + 100
end
 
First, refresh is kind of the wrong method to put this in (kind of, because depending on how you call it, it'll work just fine - normally, it won't be called every frame though). I pointed this out because the difference between every-frame-called update and on-need-called refresh is rather important.

Other than that, you're going a heavily wrong way here (again, I guess it WOULD work somehow, but... not quite ^^). First of all: You don't need to display your sprites in windows. Therefore, just put it in a seperate container within your scene... which could look like this:
[rgss]class Scene_PictureViewer # replace this line with the class definition line from your scene
  #--------------------------------------------------------------------------
  alias pictureview_initialize initialize
  def initialize
    pictureview_initialize
    @view = Viewport.new(20, 20, 600, 440)
    @picture = Sprite.new(@view)
    @picture.bitmap = RPG::Cache.picture("image.png")
    # here go picture x, y, z values, scale, whatnot...
  end
  #--------------------------------------------------------------------------
  alias pictureview_update update
  def update
  if Input::press?(Input::UP) and @picture.y > 460 # checks if image would be showing the back of the viewport
    @picture.y -= 1 # modify this number for scrolling speed, or include fancy accelerated movement
  end
  # might wanna add other movement directions as well...
  pictureview_update
  #--------------------------------------------------------------------------
end
[/rgss]

I aliased this script, so technically, you could insert it in your project, replace the title line and it should work. Then again, I wrote it without any means to test it, so no guarantees here. Either way, it should show you how you can easily and way more efficiently do it.
 

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