Map scrolling nightmare...
#1
I was trying to implement a map scrolling system thanks to Gosu's translate method that semiautomatically handles tiles and events offsets making it easier for game developers to handle more ordinary stuff than usual while drawing a map. Sadly you end up watching how my player and his not so heroic team makes the map look larger and darker than expected. I'm not really used dealing with scrolling issues so I thought somebody here could tell me what I'm doing wrong here hopefully... For now solving the issue with the horizontal map scroll should suffice. I only commented those lines that somehow affect map scrolling.

This happens when I go left... (Those two lonely tiles denote the actual end of the map on the righthand side.)

[Image: QXTKpxr.png]

And this is the disaster that takes place if I go right...

[Image: 2LgGaEr.png]


Code:
class Player
  def update
    return if @halt
    @last_x = @screen_x
    @last_y = @screen_y
    return if moved?
    if Gosu.button_down?(Gosu::KbDown)
      @dir = @other_way ? 8 : 2
      return unless pass?
      @y += 0.5#actual_speed
      @screen_y = @y * 16
      update_steps
    elsif Gosu.button_down?(Gosu::KbUp)
      @dir = @other_way ? 2 : 8
      return unless pass?
      @y -= 0.5#actual_speed
      @screen_y = @y * 16
      update_steps
    elsif Gosu.button_down?(Gosu::KbLeft)
      @dir = @other_way ? 6 : 4
      return unless pass?
      @x -= 0.5#actual_speed
      @screen_x = @x * 16
      ### Calculating the horizontal offset, Current direction: Left (4)
      @offset_x = [0, -@screen_x].min if @screen_x < Map.width * 16
      update_steps
    elsif Gosu.button_down?(Gosu::KbRight)
      @dir = @other_way ? 4 : 6
      return unless pass?
      @x += 0.5#actual_speed
      @screen_x = @x * 16
      ### Calculating the horizontal offset, Current direction: Right (6)
      @offset_x = [[-@screen_x + @last_x, -Map.width * 16].max, 0].min if @screen_x > Map.width * 16
      update_steps
    end
    if button_down?(:enter) or button_down?(:return)
      Map.events.each do |event|
        blocked = block_pass?(event)
        next unless blocked
        event.react_now if event.react
        if event.button?
          event.activate
          @halt = true
          return
        end
      end
    end
  end
end

class SceneMap
  def update_scene
    @offset_x = @player.offset_x # saving last horizontal offset
    @offset_y = [-@player.screen_y + @window_height / 1.08, 0].min
    if @map_ticks > 0
      @map_ticks -= 1
      @map_texts.clear if @map_ticks == 0
    end
    @player.update # Calling Player's Update Method and setting a new horizontal offset value
    Map.events.each {|e| e.update }
    @spriteset.update
    if press_quit?
      Game.cancel_se.play
      Game.setup(SceneKUnitsMenu)
      return
    elsif button_down?(:left_ctrl)
      Game.ok_se.play
      Game.setup(SceneKUnitsAchieve)
    elsif button_down?(:caps_lock)
      Game.ok_se.play
      Game.setup(SceneKUnitsFoes)
    elsif button_down?(:left_shift)
      Game.ok_se.play
      Game.setup(SceneKBookPages, id: 1)
    end
  end

  def draw
    @window.clip_to(0,0,@window_width,@window_height) do # Some sort of Viewport singleton command
      @window.translate(@offset_x,@offset_y) do # Scroll loop
        @spriteset.draw
      end
    end
    @map_texts.each {|text| text.draw }
  end
end

You can find the rest of the code in the KUnits script thread, just check out the main post there.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }


Messages In This Thread
Map scrolling nightmare... - by kyonides - 09-09-2017, 02:41 AM
RE: Map scrolling nightmare... - by kyonides - 09-09-2017, 06:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Cursor, Selection, and Scrolling tnsi 7 10,748 01-10-2012, 04:16 AM
Last Post: DerVVulfman



Users browsing this thread: 1 Guest(s)