Giúp đỡ giải thích script

slucis7593slucis7593 Posts: 544Registered
Mọi người giải thích giùm mình tại sao nó lại gọi dòng
csca_balloon_init vậy? :huh:

Còn đây là code
class Game_CharacterBase
  attr_accessor :csca_balloon_file
  attr_accessor :csca_balloon_id
  #------------------------------------------------------------#
  # Alias Method;Initialize                           #
  #------------------------------------------------------------#
  alias :csca_balloon_init :initialize
  def initialize
    @csca_balloon_file = "Balloon"
    @csca_balloon_id = 0
    csca_balloon_init
  end
end

Comments

  • NagiNagi Posts: 589Registered
    Cũng chả có gì alias là dùng để thêm vào dòng lệnh mà không thay đổi cấu trúc của lệnh sẵn có. (chắc thế). Cái cần overwrite thì sẽ overwrite còn cái nào không cần thì sẽ alias chúng để độ tương thích với các script khác là cao nhất. (Hiểu gì nói nấy thôi mấy pro đừng chém.)

    alias tên mới tên cũ
      def tên cũ
        #do somethings
      tên mới
      end
    end
    
    def tên mới
      #do somethings
    end
    


    OK, rồi giờ quay lại trường hợp này, nếu không dùng alias thì nó sẽ như thế nào?
    class Game_CharacterBase
      attr_accessor :csca_balloon_file
      attr_accessor :csca_balloon_id
      #------------------------------------------------------------#
      # Alias Method;Initialize                           #
      #------------------------------------------------------------#
      alias :csca_balloon_init :initialize
      def initialize
        @csca_balloon_file = "Balloon"
        @csca_balloon_id = 0
        csca_balloon_init
      end
    end
    
    class Game_CharacterBase
      #--------------------------------------------------------------------------
      # * Public Instance Variables
      #--------------------------------------------------------------------------
      attr_reader   :id                       # ID
      attr_reader   :x                        # map X coordinate (logical)
      attr_reader   :y                        # map Y coordinate (logical)
      attr_reader   :real_x                   # map X coordinate (real)
      attr_reader   :real_y                   # map Y coordinate (real)
      attr_reader   :tile_id                  # tile ID (invalid if 0)
      attr_reader   :character_name           # character graphic filename
      attr_reader   :character_index          # character graphic index
      attr_reader   :move_speed               # movement speed
      attr_reader   :move_frequency           # movement frequency
      attr_reader   :walk_anime               # walking animation
      attr_reader   :step_anime               # stepping animation
      attr_reader   :direction_fix            # direction fix
      attr_reader   :opacity                  # opacity level
      attr_reader   :blend_type               # blending method
      attr_reader   :direction                # direction
      attr_reader   :pattern                  # pattern
      attr_reader   :priority_type            # priority type
      attr_reader   :through                  # pass-through
      attr_reader   :bush_depth               # bush depth
      attr_accessor :animation_id             # animation ID
      attr_accessor :balloon_id               # balloon icon ID
      attr_accessor :transparent              # transparency flag
      attr_accessor :csca_balloon_file
      attr_accessor :csca_balloon_id
      #--------------------------------------------------------------------------
      # * Object Initialization
      #--------------------------------------------------------------------------
      def initialize
        init_public_members
        init_private_members
        csca_balloon_init
      end
      #--------------------------------------------------------------------------
      # * Initialize Public Member Variables
      #--------------------------------------------------------------------------
      def init_public_members
        @id = 0
        @x = 0
        @y = 0
        @real_x = 0
        @real_y = 0
        @tile_id = 0
        @character_name = ""
        @character_index = 0
        @move_speed = 4
        @move_frequency = 6
        @walk_anime = true
        @step_anime = false
        @direction_fix = false
        @opacity = 255
        @blend_type = 0
        @direction = 2
        @pattern = 1
        @priority_type = 1
        @through = false
        @bush_depth = 0
        @animation_id = 0
        @balloon_id = 0
        @transparent = false
      end
      #--------------------------------------------------------------------------
      # * Initialize Private Member Variables
      #--------------------------------------------------------------------------
      def init_private_members
        @original_direction = 2               # Original direction
        @original_pattern = 1                 # Original pattern
        @anime_count = 0                      # Animation count
        @stop_count = 0                       # Stop count
        @jump_count = 0                       # Jump count
        @jump_peak = 0                        # Jump peak count
        @locked = false                       # Locked flag
        @prelock_direction = 0                # Direction before lock
        @move_succeed = true                  # Move success flag
      end
     def   csca_ballon_init
        @csca_balloon_file = "Balloon"
        @csca_balloon_id = 0
      #do somethings
     end
    


    Ok vậy bạn hiểu vấn đề chưa? Alias nó sẽ rút ngắn cái thời gian mà bạn viết lại code nếu không muốn làm thay đổi cấu trúc của lệnh đó.
  • slucis7593slucis7593 Posts: 544Registered
    :O hóa ra lại có công dụng tốt như vậy, cám ơn bạn Nagi nhé :D
  • Focker_cFocker_c Posts: 1,577Registered
    Hiểu thêm đc 1 cái mặc dù khẳng định Ruby rất là khác so với các lang khác .. haiz :(
  • Dang_KhoaDang_Khoa Posts: 3,861Administrators
    Nếu so với GML thì nó hơi ngược nhau ^^
Sign In or Register to comment.