質問編集履歴

1

質問に対して回答いたしました。

2016/10/28 00:44

投稿

livetc
livetc

スコア86

test CHANGED
File without changes
test CHANGED
@@ -11,6 +11,8 @@
11
11
  ArrayList<TG_Component> components = new ArrayList<TG_Component>();
12
12
 
13
13
  public <T> T AddComponent(TG_Component component){
14
+
15
+ component.gameObject = this;
14
16
 
15
17
  components.add(component);
16
18
 
@@ -61,3 +63,57 @@
61
63
  あるいは、別の切り口でGetComponentする方法があればご教示ください。
62
64
 
63
65
  なお、Javaもジェネリックもよくわかっていません。
66
+
67
+
68
+
69
+ (ご質問への回答)
70
+
71
+ > ArrayListで使っている型のTG_Componentと、各メソッドの総称型のTはどのような関係を想定しているのでしょうか?
72
+
73
+
74
+
75
+ 回答になっているかわからないのですが、関係性がわかるかな?というプログラムを(シューティングゲームを作るような想定で)書いてみました。
76
+
77
+ 伝わらなかったらすみません。。
78
+
79
+ ```
80
+
81
+ class Player extends TG_GameObject{
82
+
83
+ Player(){
84
+
85
+ AddComponent(new Ship());
86
+
87
+ AddComponent(new Bullet());
88
+
89
+ }
90
+
91
+ }
92
+
93
+ class Ship extends TG_Component{
94
+
95
+ public TG_GameObject gameObject; // 所属するgameObject
96
+
97
+ public void update(){
98
+
99
+ // 所属するgameObjectにAddされているBulletというComponentを探す(これがやりたい!)
100
+
101
+ gameObject.GetComponent<Bullet>().shot();
102
+
103
+ }
104
+
105
+ }
106
+
107
+ class Bullet extends TG_Component{
108
+
109
+ public TG_GameObject gameObject;
110
+
111
+ public void shot(){
112
+
113
+ render();
114
+
115
+ }
116
+
117
+ }
118
+
119
+ ```