官术网_书友最值得收藏!

Inheritance

In Object-Oriented Programming (OOP), inheritance is a powerful tool. Put briefly, you can define a class that inherits from another class. An object created using the first class will contain all of the methods and member variables of the master class as well as its own.

Godot is strongly object-oriented, and this gives you the opportunity to use inheritance not just with objects (scripts) but also with scenes, allowing you a great deal of flexibility when designing your game's architecture. It also removes the need to duplicate code—if two objects need to share a set of methods and variables, for example, you can create a common script and let both objects inherit from it. If you make a change to that code, it will apply to both objects.

In this project, the player's character will be controlled by key events, while the mobs will wander around the maze randomly. However, both types of character need to have a number of properties and functions in common:

  • A spritesheet containing the four directional movement animations
  • An AnimationPlayer to play the movement animations
  • Grid-based movement (the character can only move one full tile at a time)
  • Collision detection (the character can't move through walls)

By using inheritance, you can create a generic Character scene containing the nodes that all characters need. The player and mob scenes can inherit the shared nodes from that scene. Similarly, the actual movement code (though not the controls) will be identical between player and mob, so they can both inherit from the same script to handle movement.

主站蜘蛛池模板: 盈江县| 雅安市| 米易县| 临漳县| 高陵县| 砚山县| 潍坊市| 宝鸡市| 正定县| 商城县| 镇远县| 麻城市| 璧山县| 讷河市| 无锡市| 巴青县| 威宁| 阿拉善左旗| 阜新市| 曲周县| 定边县| 金山区| 兴海县| 靖西县| 乌拉特前旗| 达拉特旗| 眉山市| 稷山县| 奉化市| 甘孜| 湘阴县| 托克托县| 子长县| 洱源县| 廉江市| 同德县| 绩溪县| 防城港市| 宜丰县| 喜德县| 峨山|