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.

Disabling keys?

Okay, I searched before posting and found some codes but I'm not sure how to use them and if they do what I want them to, so I thought I should post instead.
And also, I'm not sure if this is the right forum to post, so if it isn't, I'm terribly sorry.

Here is my problem:
I want to make a game using my own characters, tilesets and etc.
But I want the characters to only be able to walk right and left. Not like, up and down.
I don't know if I'm being clear, but it's like emogame, if you ever played it, you'll know.

Anyway, is there a code or something I can do to disable the up and down keys?
Sorry if this is a stupid question, but I don't know. :-\
 

khmp

Sponsor

Code:
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. Its functions include event starting
#  determinants and map scrolling. Refer to "$game_player" for the one
#  instance of this class.
#==============================================================================

class Game_Player < Game_Character
  Disable_Up_Down = 1 # Switch ID for enabling/disabling up/down movement.
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Remember whether or not moving in local variables
    last_moving = moving?
    # If moving, event running, move route forcing, and message window
    # display are all not occurring
    unless moving? or $game_system.map_interpreter.running? or
           @move_route_forcing or $game_temp.message_window_showing
      # Move player in the direction the directional button is being pressed
      case Input.dir4
      when 2
        move_down if !$game_switches[Disable_Up_Down]
      when 4
        move_left
      when 6
        move_right
      when 8
        move_up if !$game_switches[Disable_Up_Down]
      end
    end
    # Remember coordinates in local variables
    last_real_x = @real_x
    last_real_y = @real_y
    super
    # If character moves down and is positioned lower than the center
    # of the screen
    if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
      # Scroll map down
      $game_map.scroll_down(@real_y - last_real_y)
    end
    # If character moves left and is positioned more let on-screen than
    # center
    if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
      # Scroll map left
      $game_map.scroll_left(last_real_x - @real_x)
    end
    # If character moves right and is positioned more right on-screen than
    # center
    if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
      # Scroll map right
      $game_map.scroll_right(@real_x - last_real_x)
    end
    # If character moves up and is positioned higher than the center
    # of the screen
    if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
      # Scroll map up
      $game_map.scroll_up(last_real_y - @real_y)
    end
    # If not moving
    unless moving?
      # If player was moving last time
      if last_moving
        # Event determinant is via touch of same position event
        result = check_event_trigger_here([1,2])
        # If event which started does not exist
        if result == false
          # Disregard if debug mode is ON and ctrl key was pressed
          unless $DEBUG and Input.press?(Input::CTRL)
            # Encounter countdown
            if @encounter_count > 0
              @encounter_count -= 1
            end
          end
        end
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Same position and front event determinant
        check_event_trigger_here([0])
        check_event_trigger_there([0,1,2])
      end
    end
  end
end

Just place that code in an empty section above main. There's a Constant "Disable_Up_Down". Set the number equal to this to whatever Switch ID you want to disable up and down movement for the player. Now if you always want it disabled just say so I'll just cut the code out completely.

Good luck with it childstar! :afro:
 

poccil

Sponsor

This can be reformulated as a request to prevent the player from moving up or down, as in "Super Mario Bros."

To achieve this, edit lines 214-224 of the script section Game_Player as follows:

Code:
      case Input.dir4
      when 4
        move_left
      when 6
        move_right
      end
    end
 

khmp

Sponsor

Me and poccil's changes only relate to Game_Player so only input when the user is in Scene_Map is affected. All input is polled through the various scenes and then other objects in that scene are told of the updates in input if need be.
 

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