親オブジェクトについて


object_get_parent()

親オブジェクトについて

親オブジェクトとは複数の"子"オブジェクトをグループ化し、階層構造を形成するためのオブジェクトです。

 オブジェクトのプロパティの中で最も協力なオプションの1つは、親オブジェクトを割り当てる機能です。ゲーム内のすべてのオブジェクトは、親オブジェクトを持つことができますが、これは何を意味するのか? それは親オブジェクトが持つアクションやイベントを共有することです。 これは子が親を「継承」すると呼ばれています。しかしそれがすべてでありません! 親の機能を継承することで、子で実装するコード量を少なく収めることができます。親オブジェクトの考え方としては、子オブジェクト達の"グループ"と捉えることもできます。子は親の機能を拡張しますが、親の機能を失なわずに共有できます。では、ここでいくつかの例を上げてみましょう。

 プレイヤーと4つの異なる敵がいるとします。さて、プレイヤーはこれら4つの敵のいずれかに触れると死ぬとすると、4つのコリジョンイベントが必要となります。. しかし、1つの親を作成し、4つの敵はそれを継承するとします。そうすると、プレイヤーはこの親オブジェクトのみとのコリジョンイベントを判定すれば、他の4つの敵も一緒に判定することが可能です。

parent_image_1.png parent_image_2.png

もう1つの例としては、同じように振る舞う10の異なるオブジェクトを使ってゲームを作りたいとします。そこで1つの親を作って、それぞれ10のオブジェクトに継承させると、それぞれが何もアクションやコードを設定しなくても、親と同じ動きをします。スプライトだけ差し替えて見た目だけ変えることも可能です。

最後に、親オブジェクトを使って"合成と一致"をすることができます。2匹のモンスターがいて、1匹は上下移動して、もう1匹は左右移動するという別々の動きをするとします。ただし、HPの状態によりプレイヤーに弾を打つという共通の動作をしたいと考えています。この場合、別々の動きをする以外は同じアクションをしています。ですので、親を定義して各モンスターに継承させます。それにより子は親のイベントを上書きます。もし親のアクションを起こしたいのであれば、親のイベントを呼び出すことも可能です。

As indicated, wherever you use a parent object, this also implies the descendants (or "children"). This happens when, in an action, you indicate that the action must be applied to instances of a certain object. It also happens when you use the with() statement in code (see further on in the manual), and it works when you call code functions like instance_position, instance_number, etc... Finally, it works when you refer to variables in other objects too, like, in the above monster example, if I set the monster 1 speed to 10, then the monster 2 speed will also go to ten as it is a child object of monster 1.

It is actually good practice in most cases to create one base parent object and have this base object contain all the default behavior but never use it in the game. Rather use all the children objects and only use the parent in situations like those I have outlined above. You should also realise that parents can have parents too! Obviously you can't create a cycle of "parent1 is child of parent 2 is child of parent 1" but you can create what is called "object hierarchy" where "parent1 is child of parent2 is child of parent3". This is extremely useful to keep your game structured and you are strongly advised to learn to use this mechanism.

Now you can see why parenting can really save a lot of time and work, and why it is one of the most useful tools in the GameMaker arsenal. So take your time to get to know this function as it will really help you in the long run!

Wiki内検索

#search(): You already view a search box

MENU

ドキュメント

GMLリファレンス

2ch

その他



最新の30件

2024-03-07 2023-08-09 2023-05-21 2023-05-20 2023-05-04 2022-10-13 2022-07-21 2022-07-02 2022-04-06 2021-11-30 2021-11-26 2021-10-03 2021-09-23 2021-08-15 2021-06-02 2021-01-23 2021-01-21 2021-01-19 2021-01-18 2021-01-16 2021-01-15

人気の10件

今日の10件



これまでの訪問者: 3844
今日の訪問者: 1
昨日の訪問者: 1
閲覧者: 2