45fan.com - 路饭网

搜索: 您的位置主页 > 手机频道 > 阅读资讯:cocos2dx视频砖块进阶篇的详细介绍

cocos2dx视频砖块进阶篇的详细介绍

2016-08-29 05:59:45 来源:www.45fan.com 【

cocos2dx视频砖块进阶篇的详细介绍

 

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

 

本文地址:http://www.45fan.com/a/luyou/69108.html
Tags: 教程 视频 Cocos2dx
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部