注意:这是第二篇,上回地址:http://blog.csdn.net/s_xing/article/details/20836727,
我们接着写
感觉直接看代码太枯燥的,请关注视频讲解
更新:高清avi视频和源代码已经提供下载:
百度网盘:http://pan.baidu.com/s/1ELk78 里面的进阶篇。
2.主场景头文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "../../../../external/Box2D/Box2D.h"
#include "MyContactListener.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{
int _score;
b2World* _world;
b2Body* _ball;
b2Body* _paddle;
b2Body* _groundBody;
CCSprite* _ballSprite;
CCSprite* _paddleSprite;
b2MouseJoint* _mouseJoint;
MyContactListener* listener;
b2Fixture* _bottom;
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
void update(float delta);
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
};
#endif // __HELLOWORLD_SCENE_H__
3. 结束场景
#include "GameOverScense.h"
bool GameOverScene::initWithWin( bool isWin )
{
if (!CCLayer::init()){
return false;
}
char words[64];
if (isWin){
sprintf(words, "Man you rock!");
} else {
sprintf(words, "Man you suck!");
}
CCLabelTTF* label = CCLabelTTF::create(words, "Arial", 30);
label->setPosition(ccp(320, 300));
this->addChild(label);
return true;
}
CCScene* GameOverScene::scenseWithWin( bool isWin )
{
CCScene* sc = CCScene::create();
GameOverScene* layer = new GameOverScene();
layer->initWithWin(isWin);
sc->addChild(layer);
return sc;
}
#pragma once
#include "cocos2d.h"
USING_NS_CC;
class GameOverScene : public CCLayer
{
public:
bool initWithWin(bool isWin);
static CCScene* scenseWithWin(bool isWin);
};
#include "MyContactListener.h"
void MyContactListener::BeginContact( b2Contact* contact )
{
MyContactPeer peer = {contact->GetFixtureA(), contact->GetFixtureB()};
_contacts.push_back(peer);
}
void MyContactListener::EndContact( b2Contact* contact )
{
MyContactPeer peer = {contact->GetFixtureA(), contact->GetFixtureB()};
vector<MyContactPeer>::iterator pos, posFound;
for (pos = _contacts.begin(); pos != _contacts.end(); pos ++){
MyContactPeer onePeer = *pos;
if (onePeer.fixA == peer.fixA && onePeer.fixB == peer.fixB){
posFound = pos;
_contacts.erase(posFound);
return;
}
}
}
MyContactListener::MyContactListener():_contacts()
{
}
MyContactListener::~MyContactListener()
{
}
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
}
void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
}
#pragma once
#include "../../../../external/Box2D/Box2D.h"
#include <vector>
using namespace std;
struct MyContactPeer{
b2Fixture* fixA;
b2Fixture* fixB;
};
class MyContactListener : public b2ContactListener{
public:
vector<MyContactPeer> _contacts;
MyContactListener();
~MyContactListener();
void BeginContact(b2Contact* contact) ;
void EndContact(b2Contact* contact);
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
#pragma once //避免重复包含
#include "../../../../external/Box2D/Box2D.h"
class Box2dUtils{
public:
static b2Body* createDynamicBody(float posX, float posY, void* userData, b2World* _world){
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody; //
ballBodyDef.position.Set(posX, posY);
ballBodyDef.userData = userData; // 把屏幕中的精灵作为 物理世界中物体的 userData
b2Body* ball = _world ->CreateBody(&ballBodyDef);
return ball;
}
static b2Fixture* createFixture(b2Shape* shape, float density, float friction, float restitution, b2Body* ball ){
b2FixtureDef ballFixDef;
ballFixDef.shape = shape;
ballFixDef.density = density;
ballFixDef.friction = friction;
ballFixDef.restitution = restitution;
return ball->CreateFixture(&ballFixDef);
}
};
转载注明出处:http://blog.csdn.net/s_xing/article/details/20836693