MEGA Bubble for iOS

Coming Soon & Free for iPhone/iPad.

Advertisements

box2d preSolveContact

if you have similar requirements:

– can’t use a sensor, because you want the object to still react to some other objects in the world
– can’t use collision filter, because you want to listen to the contact

you can use preSolveContact.

An example, where I want to remove the node as I’m about to hit it. Unlike beginContact, preSolve may be called many times on a single colision, so once we have the class from the presolve contact node’s userData, we set a isDead property of that class to true so that we don’t try and remove the same node from the box2d world many times.

Example:

-(void) preSolveContact:(b2Contact*)contact  manifold:(const b2Manifold*) oldManifold
{
	b2WorldManifold worldManifold;
	contact->GetWorldManifold(&worldManifold);
	b2Fixture *fixtureA = contact->GetFixtureA();
	b2Fixture *fixtureB = contact->GetFixtureB();
	NSAssert( fixtureA != fixtureB, @"preSolveContact bug");
	
	b2Body	*bodyA = fixtureA->GetBody();
	b2Body	*bodyB = fixtureB->GetBody();
	NSAssert( bodyA != bodyB, @"preSolveContact bug");
	b2Body *otherBody = (bodyA == body_) ? bodyB : bodyA;
	b2Fixture *otherFixture = (bodyA == body_) ? fixtureB : fixtureA;
	
	if( !otherFixture->IsSensor() ) {
		BodyNode *bNode = (BodyNode*)otherBody->GetUserData();
		Class tClass = [Tank class];
		
		if([bNode isKindOfClass:tClass])
		{
			contact->SetEnabled(false);
			Tank *t = (Tank*)bNode;
			
			if(!t.isDead) {
				t.isDead = YES;
				[arrayTanksToRemove_ addObject:t];
			}
		}	
	}
}

cocos2d: maintain state through levels

Quick hack if you don’t want to bother to add a singleton to your project and want to reload or swap a level while maintaining some data.

-(void) nextLevel {
	CCScene *scene = [[game_ class] scene];

	for (CCNode *node in scene.children) {
		if ([node isKindOfClass:[game_ class]]) {
			GameNode *gn = (GameNode*)node;
			gn.level = game_.level;
			gn.totalScore = game_.totalScore;

			break;
		}
	}
	[[CCDirector sharedDirector] replaceScene: [CCTransitionMoveInT transitionWithDuration:1.0f scene:scene]];
}