cocos2dx视频教程进阶篇--第2天--打砖块游戏-box2d物理引擎--续


注意:这是第二篇,上回地址: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;
}

4. 结束场景头文件
#pragma  once

#include "cocos2d.h"

USING_NS_CC;

class GameOverScene : public CCLayer
{
public:
	bool initWithWin(bool isWin);

	static CCScene* scenseWithWin(bool isWin);
};

5. 碰撞监听文件


#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)
{

}

6.碰撞监听头文件
#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);
	

};

7 box2d 帮助类
#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值