質問するログイン新規登録

回答編集履歴

3

scheduleUpdateについて追記

2020/02/21 05:20

投稿

mingos
mingos

スコア4316

answer CHANGED
@@ -100,7 +100,7 @@
100
100
  Scene::initWithPhysics()を使うだけで物理エンジンに対応したシーンになります。
101
101
 
102
102
  ```cpp
103
- bool GameButtleScene::init(const Chosen& chosen) {
103
+ bool GameButtleScene::initWithChosen(const Chosen& chosen) {
104
104
  /*
105
105
  // 物理エンジンを使わない場合
106
106
  if(!Scene::init()) {
@@ -117,4 +117,32 @@
117
117
 
118
118
  return true;
119
119
  }
120
+ ```
121
+
122
+ ### ScheduleUpdate
123
+
124
+ initWithChosen()の中に書くだけです。
125
+ もちろん、void update(float dt)も用意しなければいけません。
126
+
127
+ ```cpp
128
+ class GameButtleScene : public Scene {
129
+ public:
130
+ void update(float dt);
131
+ }
132
+
133
+ bool GameButtleScene::initWithChosen(const Chosen& chosen) {
134
+ if(!Scene::init()) {
135
+ return false;
136
+ }
137
+ mChosen = chosen;
138
+
139
+ // 初期化処理
140
+ scheduleUpdate();
141
+
142
+ return true;
143
+ }
144
+
145
+ void GameButtleScene::update(float dt) {
146
+
147
+ }
120
148
  ```

2

物理エンジンの初期化について追記

2020/02/21 05:20

投稿

mingos
mingos

スコア4316

answer CHANGED
@@ -85,4 +85,36 @@
85
85
  return true;
86
86
  }
87
87
 
88
+ ```
89
+
90
+ ---
91
+
92
+ ## 【追記】
93
+
94
+ ### 物理エンジンを使用する場合の初期化
95
+
96
+ 物理エンジンを使用する際に使用する、引数なしのScene::createWithPhysics()のソースを見ると、
97
+ Scene::initWithPhysics()を呼び出していますので、それを真似すればOKです。
98
+
99
+ つまり、GameButtleScene::initWithChosen()の中でScene::init()の代わりに、
100
+ Scene::initWithPhysics()を使うだけで物理エンジンに対応したシーンになります。
101
+
102
+ ```cpp
103
+ bool GameButtleScene::init(const Chosen& chosen) {
104
+ /*
105
+ // 物理エンジンを使わない場合
106
+ if(!Scene::init()) {
107
+ return false;
108
+ }
109
+ */
110
+
111
+ // 物理エンジンを使用する場合
112
+ if(!Scene::initWithPhysics()) {
113
+ return false;
114
+ }
115
+
116
+ ...
117
+
118
+ return true;
119
+ }
88
120
  ```

1

コメントを追記

2020/02/21 05:14

投稿

mingos
mingos

スコア4316

answer CHANGED
@@ -13,6 +13,8 @@
13
13
  #ifndef __Chosen__
14
14
  #define __Chosen__
15
15
 
16
+ #include "cocos2d.h"
17
+
16
18
  struct Chosen {
17
19
  // 任意のパラメータ
18
20
  }
@@ -20,7 +22,7 @@
20
22
  #endif
21
23
  ```
22
24
 
23
- * HomeScene任意の場所
25
+ * HomeScene任意の場所(GameButtleSceneに遷移する処理)
24
26
  ```
25
27
  #include "Chosen.h"
26
28
  #include "GameButtleScene.h"
@@ -32,7 +34,7 @@
32
34
  ...
33
35
 
34
36
  // パラメータを渡してシーンを遷移
35
- Scene* scene = GameButtleScene::createScene(chosen);
37
+ auto scene = GameButtleScene::createScene(chosen);
36
38
  auto transition = TransitionFade::create(1.0f, scene);
37
39
  Director::getInstance()->replaceScene(transition);
38
40
  ```
@@ -53,6 +55,8 @@
53
55
  virtual bool initWithChosen(const Chosen& chosen);
54
56
 
55
57
  private:
58
+ // とりあえずメンバ変数にしておきますが
59
+ // initWithChosen()の中だけでしか使わない場合はメンバ変数にする必要はなし
56
60
  Chosen mChosen;
57
61
  };
58
62
  ```