@@ -100,7 +100,7 @@ public void onDestroy() {
100
100
101
101
static class JBox2DTestLayer extends Layer {
102
102
public static final int kTagSpriteManager = 1 ;
103
-
103
+
104
104
// Pixel to meters ratio. Box2D uses meters as the unit for measurement.
105
105
// This ratio defines how many pixels correspond to 1 Box2D "meter"
106
106
// Box2D is optimized for objects of 1x1 meter therefore it makes sense
@@ -125,8 +125,6 @@ public JBox2DTestLayer() {
125
125
Vec2 gravity = new Vec2 (0.0f , -10.0f );
126
126
127
127
bxWorld = new World (new AABB (lower , upper ), gravity , true );
128
- bxWorld .setContinuousPhysics (true );
129
- bxWorld .setPositionCorrection (true );
130
128
131
129
// TODO: JBox2D debug drawing is a bit different now. We could add debug drawing the old way, but looks like that is going away soon.
132
130
/*
@@ -164,12 +162,25 @@ public JBox2DTestLayer() {
164
162
label .setPosition (s .width / 2f , s .height - 50f );
165
163
label .setColor (new CCColor3B (0 , 0 , 255 ));
166
164
addChild (label );
167
-
168
- // schedule the physics update processor
169
- this .schedule ("tick" );
170
165
}
171
166
172
- private void addNewSpriteWithCoords (CCPoint pos ) {
167
+ @ Override
168
+ public void onEnter () {
169
+ super .onEnter ();
170
+
171
+ // start ticking (for physics simulation)
172
+ schedule ("tick" );
173
+ }
174
+
175
+ @ Override
176
+ public void onExit () {
177
+ super .onExit ();
178
+
179
+ // stop ticking (for physics simulation)
180
+ unschedule ("tick" );
181
+ }
182
+
183
+ private void addNewSpriteWithCoords (CCPoint pos ) {
173
184
AtlasSpriteManager mgr = (AtlasSpriteManager ) getChild (kTagSpriteManager );
174
185
175
186
// We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
@@ -194,24 +205,23 @@ private void addNewSpriteWithCoords(CCPoint pos) {
194
205
bxSpritePolygonDef .density = 1.0f ;
195
206
bxSpritePolygonDef .friction = 0.3f ;
196
207
197
- synchronized (bxWorld ) {
198
- Body bxSpriteBody = bxWorld .createBody (bxSpriteBodyDef );
199
-
208
+ synchronized (bxWorld ) {
200
209
// Define the dynamic body fixture and set mass so it's dynamic.
201
- bxSpriteBody .createShape (bxSpritePolygonDef );
202
- bxSpriteBody .setMassFromShapes ();
210
+ Body bxSpriteBody = bxWorld .createBody (bxSpriteBodyDef );
211
+ bxSpriteBody .createShape (bxSpritePolygonDef );
212
+ bxSpriteBody .setMassFromShapes ();
203
213
}
204
214
}
205
215
206
- public void tick (float delta ) {
216
+ public synchronized void tick (float delta ) {
207
217
// It is recommended that a fixed time step is used with Box2D for stability
208
218
// of the simulation, however, we are using a variable time step here.
209
219
// You need to make an informed choice, the following URL is useful
210
220
// http://gafferongames.com/game-physics/fix-your-timestep/
211
221
212
222
// Instruct the world to perform a simulation step. It is
213
223
// generally best to keep the time step and iterations fixed.
214
- synchronized (bxWorld ) {
224
+ synchronized (bxWorld ) {
215
225
bxWorld .step (delta , 10 );
216
226
}
217
227
@@ -233,7 +243,7 @@ public boolean ccTouchesBegan(MotionEvent event) {
233
243
CCPoint location = Director .sharedDirector ().convertCoordinate (event .getX (), event .getY ());
234
244
235
245
addNewSpriteWithCoords (location );
236
-
246
+
237
247
return TouchDispatcher .kEventHandled ;
238
248
}
239
249
0 commit comments