接着我们先来重写ContactListener:
类声明:
#import "Box2D.h"
class ContactListener : public b2ContactListener
{
public:
voidBeginContact(b2Contact* contact);
voidPreSolve(b2Contact* contact, const b2Manifold* oldManifold);
voidPostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
voidEndContact(b2Contact* contact);
};
类定义:
#import "ContactListener.h"
#import "cocos2d.h"
#import "GameLayer.h"
void ContactListener::BeginContact(b2Contact*contact)
{
b2Body* body =contact->GetFixtureA()->GetBody();
NSNumber* userData =(NSNumber*)(body->GetUserData());
if (userData == nil) {
return;
}
int tag = [userDataintValue];
if (tag == -1) {
body= contact->GetFixtureB()->GetBody();
userData= (NSNumber*)(body->GetUserData());
if(userData == nil) {
return;
}
tag= [userData intValue];
if(tag == -2) {
return;
}
}
int lastScoredStep =[[GameLayer gameLayer] lastScoredStep];
if (lastScoredStep == -1){
[GameLayergameLayer].lastScoredStep = tag;
return;
} else if (lastScoredStep!= tag) {
[GameLayergameLayer].lastScoredStep = tag;
[[GameLayergameLayer] addScore];
}
}
void ContactListener::PreSolve(b2Contact* contact,const b2Manifold* oldManifold){}
void ContactListener::PostSolve(b2Contact*contact, const b2ContactImpulse* impulse){}
void ContactListener::EndContact(b2Contact*contact){}
这里我们重写了BeginContact方法,当有刚体之间的碰撞发生时,我们通过判断两个刚体的类型来判断是否得分,刚体类型的判断就用的是我们前面的tag,注意这里我们做了判断,由于我们没有为墙体指定tag,所以当墙体的userData转换为NSNumber指针时为nil,这时候就没有必要再做判断了,并且我们为了防止重复记分,例如小球连续两次碰到当前Block(例如小球碰撞之后弹起又碰撞,或者遇到高矮台阶的那种Block),这时候就通过lastScoredStep来比较,lastScoredStep始终记录前一次得分的StepBlock。
最后我们来定义Ball类:
声明如下:
#import "CCNode.h"
#import "cocos2d.h"
#import "Box2D.h"
@interface Ball : CCNode{
float screenWidth;
float screenHeight;
b2Body* body;
CCSprite* shape;
}
+ (Ball*)ball;
- (void)reset;
- (void)pushLeft;
- (void)pushRight;
@end
定义:
#import "Ball.h"
#import "cocos2d.h"
#import "GB2ShapeCache.h"
#import "TagDefinitions.h"
#import "GameLayer.h"
@implementation Ball
+ (Ball*)ball{
return [[[Ball alloc]init] autorelease];
}
- (id)init{
if (self = [super init]) {
//screendimensions
CGSizescreenSize = [[CCDirector sharedDirector] winSize];
screenHeight= screenSize.height;
screenWidth= screenSize.width;
shape= [CCPhysicsSprite spriteWithFile:@"ball.png"];
shape.anchorPoint= ccp(0.5f, 0.5f);
CGPointposition = ccp(screenWidth*0.5f, screenHeight*0.5f+50);
b2BodyDefbodyDef;
bodyDef.type= b2_dynamicBody;
bodyDef.userData= [NSNumber numberWithInteger:-1];
bodyDef.position.Set(position.x/PTM_RATIO,position.y/PTM_RATIO);
body= [[GameLayer gameLayer] sharedWorld]->CreateBody(&bodyDef);
[[GB2ShapeCachesharedShapeCache] addFixturesToBody:body forShapeName:@"ball"];
[shapesetPTMRatio:PTM_RATIO];
[shapesetB2Body:body];
[shapesetPosition:position];
[selfaddChild:shape];
}
return self;
}
- (void)pushLeft{
b2Vec2 force;
force.Set(-4.0f, 0);
body->ApplyForceToCenter(force);
}
- (void)pushRight{
b2Vec2 force;
force.Set(4.0f, 0);
body->ApplyForceToCenter(force);
}
- (void)reset{
CGPoint position =ccp(screenWidth*0.5f, screenHeight*0.5f+50);
b2Vec2 pos;
pos.Set(position.x/PTM_RATIO,position.y/PTM_RATIO);
body->SetTransform(pos,0);
[shapesetPosition:position];
}
- (CGPoint)position{
return shape.position;
}
@end
小球的图片素材:
小球类比较简单,通过ApplyForceToCenter来对小球施力即可。
这样我们的游戏就基本制作完成了,限于篇幅,我们没有列出各个类的dealloc方法,请读者补充完成,释放资源。此外,还可以添加一些音效,添加开始界面和GameOver界面等等。
教程就到这里,如果有问题,欢迎留言~