100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 用cocos2d-x(初级)实现篮球投篮小游戏 控制篮球投篮 一共三关(第二第三关使用box2d

用cocos2d-x(初级)实现篮球投篮小游戏 控制篮球投篮 一共三关(第二第三关使用box2d

时间:2023-06-06 16:53:54

相关推荐

用cocos2d-x(初级)实现篮球投篮小游戏 控制篮球投篮 一共三关(第二第三关使用box2d

实现的功能:(分关说明)

主界面:

1.点击三个不同的按钮实现界面跳转。

第一二三关:

1.用简单的物理公式在update控制小球的运动,实时刷新小球的坐标。

2.使用box2d物理引擎来制作游戏场景,控制小球的运动以及碰撞。

3.点击退出按钮返回主界面。

游戏截图:

(嘿嘿,找了个巨配的背景)

然后就是点击第一关第二关第三关进入不同的关卡

第一关是没有使用box2d物理引擎的,大致的做法就是直接更改update里面小球的position。运用了一丢丢物理公式。

拖拽鼠标左键的时候可以控制小球的发射方向以及力度。

第一关没有使用物理引擎,所以小球之前、小球与物体的碰撞都需要自己写。

#include "DemoLayer.h"#include "math.h"#include "Box2DDemoLayer.h"#include "HelloWorldScene.h"#define MASS0.4#define Gravity ccp(0,-9.8*100*MASS)#define Height 500#define Width640#define RadiusOfBall 36#define LossRateOfCollide 0.8CCScene* DemoLayer::scene(){// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectDemoLayer *layer = DemoLayer::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;}bool DemoLayer::init(){if (!CCLayer::init()){return false;}// size = CCDirector::sharedDirector()->getVisibleSize();left = (size.width - Width) / 2;right = (size.width + Width) / 2;bottom = (size.height - Height) / 2;CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();this->setTouchEnabled(true);CCLabelTTF* pLabel = CCLabelTTF::create("The first level(don't use Box2D)", "Arial", 24);// position the label on the center of the screenpLabel->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - pLabel->getContentSize().height));// add the label as a child to this layerthis->addChild(pLabel, 1);//按钮的代码CCMenuItemImage *pCloseItem = CCMenuItemImage::create("blackBtnbackground.png",//正常状态的图片,系统自带的"blackBtnbackground.png",//被点击的图片this,menu_selector(DemoLayer::menuCloseCallback));pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2 - 10,origin.y + pCloseItem->getContentSize().height / 2 + 10));CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu);//这里的退出按钮由于找不到合适的素材,我就用文本代替了,然后按钮的图片是用一张黑色的图片放在按钮上,所以看不出来。CCLabelTTF* pLabel1 = CCLabelTTF::create("Exit", "Arial", 30);pLabel1->setPosition(ccp(origin.x + visibleSize.width - 35, origin.y + pLabel1->getContentSize().height - 15));this->addChild(pLabel1, 1);//用纹理缓存(资源池)加载纹理 这里的纹理就是一个点CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("texball.png");//创建批处理渲染方式的精灵节点CCSpriteBatchNode *mgr = CCSpriteBatchNode::createWithTexture(texture);//添加到层中this->addChild(mgr);background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 640, 500));//创建背景background->setAnchorPoint(ccp(0, 0));background->setPosition(ccp(left, bottom));mgr->addChild(background);//开始箭头位置startArea = CCSprite::createWithTexture(texture, CCRectMake(647, 124, 100, 100));startArea->setPosition(ccp(left + 80, bottom + 150));mgr->addChild(startArea);//箭头arrow = CCSprite::createWithTexture(texture, CCRectMake(0, 500, 690, 120));arrow->setPosition(startArea->getPosition());arrow->setScale(0.1);mgr->addChild(arrow);//小球ball = CCSprite::createWithTexture(texture, CCRectMake(654, 0, 72, 72));ball->setPosition(startArea->getPosition());mgr->addChild(ball);//篮板basketry = CCSprite::createWithTexture(texture, CCRectMake(656, 86, 114, 24));basketry->setPosition(ccp(left + 550, bottom + 300));mgr->addChild(basketry);//篮筐target = CCSprite::createWithTexture(texture, CCRectMake(648, 272, 16, 160));target->setPosition(ccp(left + 600, bottom + 350));mgr->addChild(target);//铁三角trangle = CCSprite::createWithTexture(texture, CCRectMake(760, 0, 90, 90));trangle->setPosition(ccp(left + Width - 45, bottom + 45));mgr->addChild(trangle);isShoot = false;//加速度为Gravityacceleration = Gravity;//篮筐左点和右点basketryLeftPos = ccp(left + 493, bottom + 300);//篮筐左点basketryRightPos = ccp(left + 579, bottom + 300);//篮筐右点dis0fLeftbasketryandball = ccpLength(ccpSub(basketryLeftPos, ball->getPosition()));//左点与篮球距离dis0fRightbasketryandball = ccpLength(ccpSub(basketryRightPos, ball->getPosition()));//右点与篮球距离Target = ccp(target->getPosition().x - 8, target->getPosition().y - 80);//target左下角点与篮球距离normal = ccp(0, 0);P = ccp(0, 0);DotVelocity = ccp(0, 0);schedule(schedule_selector(DemoLayer::frmeFunc), 0.001);return true;}void DemoLayer::LossVelocity(CCPoint &v){//按照碰撞损失,减少速度v = v*LossRateOfCollide;}void DemoLayer::HandleCollision(CCPoint &prePos){CCPoint ballPos = ball->getPosition();//实现篮球与边缘的碰撞反应//左右if (ballPos.x>Width+100|| ballPos.x<185){ball->setPosition(prePos);velocity = ccp(-velocity.x, velocity.y);LossVelocity(velocity);}//上下if (ballPos.y>Height-30|| ballPos.y<135){ball->setPosition(prePos);velocity = ccp(velocity.x, -velocity.y);LossVelocity(velocity);}//篮筐左点if (dis0fLeftbasketryandball<RadiusOfBall){ball->setPosition(prePos);normal = ccpNormalize(ccpSub(ball->getPosition(), basketryLeftPos));//法线单一化P = normal*ccpDot(-velocity, normal);//法线DotVelocity = P * 2 + velocity;//反速度velocity = DotVelocity;LossVelocity(velocity);}//篮筐右点if (dis0fRightbasketryandball<RadiusOfBall){ball->setPosition(prePos);normal = ccpNormalize(ccpSub(ball->getPosition(), basketryRightPos));//法线单一化P = normal*ccpDot(-velocity, normal);//法线DotVelocity = P * 2 + velocity;//反速度velocity = DotVelocity;LossVelocity(velocity);}//篮板if (ballPos.y + RadiusOfBall>Target.y&&ballPos.x + RadiusOfBall>Target.x ){ball->setPosition(prePos);velocity = ccp(-velocity.x, velocity.y);LossVelocity(velocity);}//三角板if (dis0ftrangleandball<RadiusOfBall){ball->setPosition(prePos);normal = ccpNormalize(ccp(1, -1));P = normal*ccpDot(-velocity, normal);//法线DotVelocity = P * 2 + velocity;//反速度velocity = DotVelocity;LossVelocity(velocity);}}void DemoLayer::registerWithTouchDispatcher(){CCDirector* pDirector = CCDirector::sharedDirector();pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}//鼠标touch开始bool DemoLayer::ccTouchBegan(CCTouch* touch, CCEvent* event){CCPoint touchLocation = touch->getLocation();touchLocation = convertToNodeSpace(touchLocation);ball->setPosition(startArea->getPosition());startPos = touchLocation;isShoot = false;return true;}//鼠标touch中void DemoLayer::ccTouchMoved(CCTouch* touch, CCEvent* event){//实现随着鼠标触碰移动,箭头的大小、方向做出相应变化//对这个移动的向量,记录为力force,将来TouchEnd时做为投篮时的作用力CCPoint touchLocation = touch->getLocation();touchLocation = convertToNodeSpace(touchLocation);CCPoint a;//鼠标拖动时箭头float distanceScale;a.x = startArea->getPositionX() - touchLocation.x;a.y = startArea->getPositionY() - touchLocation.y;arrow->setRotation(-(ccpToAngle(a) / 3.1415 * 180));distanceScale = (ccpDistance(startArea->getPosition(), touchLocation)) / 500;if (distanceScale >= 0.3){distanceScale = 0.3;}arrow->setScale(distanceScale);//将里force = startPos - touchLocation;}//鼠标touch结束void DemoLayer::ccTouchEnded(CCTouch* touch, CCEvent* event){//根据投篮的作用力,根据球的密度,计算初速度 isShoot = true;arrow->setScale(0);velocity = ccpMult(force * 20, MASS);}void DemoLayer::frmeFunc(float dt){//如果篮球在投掷过程中,计算随着时间的流逝,篮球的运动轨迹velocity = velocity + Gravity / MASS*dt*1.5f;CCPoint abc = ball->getPosition();CCPoint curPos = velocity*dt;curPos = ball->getPosition() + curPos;dis0fLeftbasketryandball = ccpLength(ccpSub(basketryLeftPos, curPos));//篮球与左点的距离dis0fRightbasketryandball = ccpLength(ccpSub(basketryRightPos, curPos));//篮球与右点的距离dis0fTargetandball = ccpLength(ccpSub(Target, curPos));//篮球与篮板左下角点的距离dis0ftrangleandball = fabsf(-curPos.x+230 + (curPos.y - left + Width - 90))*sqrt(2.0f) / 2.0f;//篮球到三角板的距离if (isShoot){ball->setPosition(curPos);HandleCollision(abc);}}//点击按钮切换场景void DemoLayer::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");#elseCCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.4f, HelloWorld::scene()));//核心的修改#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);#endif#endif}

第二关:

由于没有花什么心思在关卡设计,所以第二关主要是引入box2d物理引擎。

第二关的代码就不贴在这里了,跟第三关的差不多,不过就是第三关的代码加了一些障碍物。

第三关:

这一关加了几个木箱来阻挡小球

#include "TheThirdLevel.h"#include "HelloWorldScene.h"#define PTM_RATIO 32#define Width640#define Height 500CCScene* TheThirdLevel::scene(){// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectTheThirdLevel *layer = TheThirdLevel::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;}// on "init" you need to initialize your instancebool TheThirdLevel::init(){//// 1. super init firstif ( !CCLayer::init() ){return false;}size = CCDirector::sharedDirector()->getVisibleSize();left = (size.width - Width) / 2;right = (size.width + Width) / 2;bottom = (size.height - Height) / 2;CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();texture = CCTextureCache::sharedTextureCache()->addImage("texball.png");boxTexture = CCTextureCache::sharedTextureCache()->addImage("boxTexture.png");//字体CCLabelTTF* pLabel = CCLabelTTF::create("The third level(use Box2D)", "Arial", 24);pLabel->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - pLabel->getContentSize().height));this->addChild(pLabel, 1);//按钮的代码CCMenuItemImage *pCloseItem = CCMenuItemImage::create("blackBtnbackground.png",//正常状态的图片,系统自带的"blackBtnbackground.png",//被点击的图片this,menu_selector(TheThirdLevel::menuCloseCallback));pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2-10,origin.y + pCloseItem->getContentSize().height / 2+10));CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu);//退出文本CCLabelTTF* pLabel1 = CCLabelTTF::create("Exit", "Arial", 30);pLabel1->setPosition(ccp(origin.x + visibleSize.width-35, origin.y +pLabel1->getContentSize().height-15));this->addChild(pLabel1, 1);//地图background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 640, 500));background->setAnchorPoint(ccp(0, 0));background->setPosition(ccp(left, bottom));this->addChild(background);//箭头圆圈startArea = CCSprite::createWithTexture(texture, CCRectMake(647, 124, 100, 100));startArea->setPosition(ccp(left + 520, bottom + 150));this->addChild(startArea);//箭头arrow = CCSprite::createWithTexture(texture, CCRectMake(0, 500, 690, 120));arrow->setPosition(startArea->getPosition());arrow->setScale(0.1);this->addChild(arrow);//木箱//1.1box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(444, 138));this->addChild(box);//1.2box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(444, 203));this->addChild(box);//1.3box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(444, 268));this->addChild(box);//1.4box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(444, 333));this->addChild(box);//2.1box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(510,138));this->addChild(box);//2.2box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(510, 203));this->addChild(box);//2.3box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(510, 268));this->addChild(box);//2.4box = CCSprite::createWithTexture(boxTexture, CCRectMake(0, 0, 88, 80));box->setPosition(ccp(510, 333));this->addChild(box);//创建世界重力b2Vec2 gravity;gravity.Set(0.0f, -50.0f);world = new b2World(gravity);myContactListener = new MyContactListener();world->SetContactListener(myContactListener);world->SetAllowSleeping(true);//world->SetContinuousPhysics(true);b2BodyDef groundBodyDef;groundBodyDef.position.Set(0.0f, 0.0f);b2Body* groundBody = world->CreateBody(&groundBodyDef);// Define the ground box shape.b2EdgeShape groundBox;// bottomgroundBox.Set(b2Vec2(160.0f / PTM_RATIO, 102.0f / PTM_RATIO), b2Vec2(800.0f / PTM_RATIO, 102.0f / PTM_RATIO));groundBody->CreateFixture(&groundBox,0);// topgroundBox.Set(b2Vec2(0.0f/PTM_RATIO,570.0f/PTM_RATIO), b2Vec2(800.0f/PTM_RATIO,570.0f/PTM_RATIO));groundBody->CreateFixture(&groundBox,0);// leftgroundBox.Set(b2Vec2(162.0f / PTM_RATIO, 77.0f / PTM_RATIO), b2Vec2(162.0f / PTM_RATIO, 600.0f / PTM_RATIO));groundBody->CreateFixture(&groundBox,0);// rightgroundBox.Set(b2Vec2(770.0f / PTM_RATIO, 77.0f / PTM_RATIO), b2Vec2(770.0f / PTM_RATIO, 600.0f / PTM_RATIO));groundBody->CreateFixture(&groundBox,0);startPoint.setPoint(left + 520, bottom + 150);//球b2BodyDef bodyDef;//bodyDef.type = b2_dynamicBody;bodyDef.type = b2_staticBody;bodyDef.position.Set(startPoint.x / PTM_RATIO, startPoint.y / PTM_RATIO);//bodyDef.position.Set(100, 100);CCSprite *sprite = CCSprite::createWithTexture(texture, CCRectMake(654, 0, 72, 72));sprite->setPosition(startPoint);bodyDef.userData = sprite;this->addChild(sprite);ball = world->CreateBody(&bodyDef);b2CircleShape dynamicBall;dynamicBall.m_radius=36/PTM_RATIO;b2FixtureDef fixtureDef;fixtureDef.shape = &dynamicBall;fixtureDef.density = 1.0f;//密度fixtureDef.friction = 0.3f;//摩擦系数fixtureDef.restitution = 0.8f;//恢复系数ball->CreateFixture(&fixtureDef);////挡板b2BodyDef bodyDef2;//bodyDef.type = b2_dynamicBody;bodyDef2.type = b2_staticBody;//bodyDef.position.Set(100, 100);sprite = CCSprite::createWithTexture(texture, CCRectMake(648, 272, 16, 160));sprite->setPosition(ccp(168, 420));bodyDef2.position.Set(190/PTM_RATIO, 420/PTM_RATIO);bodyDef2.userData = sprite;this->addChild(sprite);target = world->CreateBody(&bodyDef2);b2PolygonShape tg;tg.SetAsBox(20.0/PTM_RATIO, 80.0/PTM_RATIO);b2FixtureDef fixtureDef2;fixtureDef2.shape = &tg;fixtureDef2.density = 1.0f;//密度fixtureDef2.friction = 0.3f;//摩擦系数fixtureDef2.restitution = 0.8f;//恢复系数target->CreateFixture(&fixtureDef2);////篮筐右点b2BodyDef bodyDef3;//bodyDef.type = b2_dynamicBody;bodyDef3.type = b2_staticBody;//bodyDef.position.Set(100, 100);sprite = CCSprite::createWithTexture(texture, CCRectMake(656, 86, 114, 24));sprite->setPosition(ccp(220, 370));sprite->setRotationY(180);bodyDef3.position.Set(288 / PTM_RATIO, 390 / PTM_RATIO);bodyDef3.userData = sprite;this->addChild(sprite);basketry = world->CreateBody(&bodyDef3);b2CircleShape bak;bak.m_radius = 0.01;b2FixtureDef fixtureDef3;fixtureDef3.shape = &bak;fixtureDef3.density = 1.0f;//密度fixtureDef3.friction = 0.3f;//摩擦系数fixtureDef3.restitution = 0.8f;//恢复系数basketry->CreateFixture(&fixtureDef3);//篮筐左点b2BodyDef bodyDef4;bodyDef4.type = b2_staticBody;bodyDef4.position.Set(215 / PTM_RATIO, 390 / PTM_RATIO);bodyDef4.userData = sprite;basketry = world->CreateBody(&bodyDef4);basketry->CreateFixture(&fixtureDef3);//木箱b2BodyDef bodyDef5;bodyDef5.type = b2_staticBody;bodyDef5.position.Set(480 / PTM_RATIO, 200 / PTM_RATIO);bodyDef5.userData = sprite;boxBody = world->CreateBody(&bodyDef5);b2PolygonShape bx;bx.SetAsBox(75.0 / PTM_RATIO, 175.0 / PTM_RATIO);b2FixtureDef fixtureDef5;fixtureDef5.shape = &bx;fixtureDef5.density = 1.0f;//密度fixtureDef5.friction = 0.3f;//摩擦系数fixtureDef5.restitution = 0.8f;//恢复系数boxBody->CreateFixture(&fixtureDef5);this->setTouchEnabled(true);this->scheduleUpdate();return true;}TheThirdLevel::~TheThirdLevel(){delete world;delete myContactListener;world = NULL;}void TheThirdLevel::createDynamicBox(CCPoint &pos){b2BodyDef bodyDef;bodyDef.type = b2_dynamicBody;//bodyDef.type = b2_staticBody;bodyDef.position.Set(pos.x / PTM_RATIO,pos.y / PTM_RATIO);CCSprite *sprite = CCSprite::createWithTexture(texture, CCRectMake(654, 0, 72, 72));bodyDef.userData = sprite;sprite->setPosition(pos);this->addChild(sprite);b2Body* body = world->CreateBody(&bodyDef);b2CircleShape dynamicBox;//b2PolygonShape dynamicBox;//dynamicBox.SetAsBox(0.5f, 0.5f);dynamicBox.m_radius=36/PTM_RATIO;b2FixtureDef fixtureDef;fixtureDef.shape = &dynamicBox;fixtureDef.density = 0.5f;//密度fixtureDef.friction = 0.3f;//摩擦系数fixtureDef.restitution = 0.8f;//恢复系数body->CreateFixture(&fixtureDef);}void TheThirdLevel::registerWithTouchDispatcher(){CCDirector* pDirector = CCDirector::sharedDirector();pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}//鼠标touch开始bool TheThirdLevel::ccTouchBegan(CCTouch* touch, CCEvent* event){CCPoint touchLocation = touch->getLocation(); touchLocation = convertToNodeSpace( touchLocation );/*createDynamicBox(touchLocation);*/startPos = touchLocation;CCSprite * p = (CCSprite*)ball->GetUserData();p->setPosition(startPoint);ball->SetType(b2_staticBody);b2Vec2 startP;startP.Set(startPoint.x / PTM_RATIO, startPoint.y / PTM_RATIO);ball->SetTransform(startP, 0.0);return true;}//鼠标touch中void TheThirdLevel::ccTouchMoved(CCTouch* touch, CCEvent* event){//补充代码,实现随着鼠标触碰移动,箭头的大小、方向做出相应变化//对这个移动的向量,记录为力force,将来TouchEnd时做为投篮时的作用力CCPoint touchLocation = touch->getLocation();touchLocation = convertToNodeSpace(touchLocation);CCPoint a;float distanceScale;a.x = startArea->getPositionX() - touchLocation.x;a.y = startArea->getPositionY() - touchLocation.y;arrow->setRotation(-(ccpToAngle(a) / 3.1415 * 180));distanceScale = (ccpDistance(startArea->getPosition(), touchLocation)) / 500;if (distanceScale >= 0.25){distanceScale = 0.25;}arrow->setScale(distanceScale);}//鼠标touch结束void TheThirdLevel::ccTouchEnded(CCTouch* touch, CCEvent* event){CCPoint touchLocation = touch->getLocation(); touchLocation = convertToNodeSpace( touchLocation );arrow->setScale(0);ball->SetType(b2_dynamicBody);b2Vec2 force;CCPoint f = startPoint - touchLocation ;force.Set(f.x * 20, f.y * 20);ball->ApplyForce(force, ball->GetWorldCenter());}void TheThirdLevel::update(float dt){int velocityIterations = 8;int positionIterations = 2;world->Step(dt, velocityIterations, positionIterations);b2Body *b = ball;for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()){if (b->GetUserData() != NULL && b->GetType() == b2_dynamicBody) //if(b->GetType() == b2_dynamicBody){CCSprite *sprite = (CCSprite*)b->GetUserData();sprite->setPosition(ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));sprite->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));}}}void TheThirdLevel::parseContact(){// b2Vec2 contactPoint;// contactPoint.SetZero();// for(b2Contact * contact = world->GetContactList();contact;contact=contact->GetNext()){// /* if (contact->GetFixtureA()->GetBody() == contactObj ||//contact->GetFixtureB()->GetBody() == contactObj){*///{//b2Manifold *manifold = contact->GetManifold();//switch (manifold->type) //{// case b2Manifold::e_circles:// contactPoint = contact->GetFixtureA()->GetBody()->GetWorldPoint(manifold->localPoint);// break;//case b2Manifold::e_faceA:// contactPoint = contact->GetFixtureA()->GetBody()->GetWorldPoint(manifold->localPoint);// break;// case b2Manifold::e_faceB:// contactPoint = contact->GetFixtureB()->GetBody()->GetWorldPoint(manifold->localPoint);// break;// default:// break;//}//}// }}//点击按钮切换场景void TheThirdLevel::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");#elseCCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.4f, HelloWorld::scene()));//核心的修改#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);#endif#endif}

项目总结:总的来说box2d这个物理引擎还是十分强大的,可以不去计算物体碰撞的方向改变,由引擎直接帮你完成,你只需要给物体添加一个图片精灵以及刚体就可以了。

**项目源代码下载地址:**/detail.html?id=1659

好了呀,又一个cocos2d-x的教程写完了,希望大家跟我一起努力努力,一起学习嘿嘿~

用cocos2d-x(初级)实现篮球投篮小游戏 控制篮球投篮 一共三关(第二第三关使用box2d物理引擎)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。