cocos2d-x box2d 弹球

  • M6_470170
    了解作者
  • 187.8KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-24 01:32
    上传日期
修改自子龙山人翻译的教程弹球 vs2008编译运行通过
Breakout-win32.zip
  • Breakout
  • Resources
  • CloseSelected.png
    5.4KB
  • Paddle.png
    2KB
  • Ball.png
    2.5KB
  • HelloWorld.png
    141.5KB
  • CloseNormal.png
    6.2KB
  • logo8.png
    1.9KB
  • win32
  • res
  • Breakout.ico
    46.5KB
  • Breakout.rc
    2.2KB
  • resource.h
    471B
  • main.h
    280B
  • main.cpp
    400B
  • Classes
  • AppDelegate.h
    1.1KB
  • AppDelegate.cpp
    4.3KB
  • HelloWorldScene.cpp
    8.6KB
  • HelloWorldScene.h
    1.2KB
  • Breakout.win32.vcproj
    6KB
  • Breakout.win32.vcproj.DELL.Administrator.user
    1.4KB
  • Breakout.win32.vcproj.user
    484B
内容介绍
#include "HelloWorldScene.h" #define PTM_RATIO 32 using namespace cocos2d; HelloWorld::HelloWorld() { } HelloWorld::~HelloWorld() { delete _world; _groundBody=NULL; } CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::node(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::node(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 20)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2, size.height/2)); // Add the sprite to HelloWorld layer as a child layer. this->addChild(pSprite, 0); this->setIsTouchEnabled(true); // box2d CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); b2Vec2 gravity=b2Vec2(0.0f,0.0f); bool doSleep=true; // create a world; _world=new b2World(gravity); _world->SetAllowSleeping(doSleep); _world->SetContinuousPhysics(true); // Create edges around the entire screen b2BodyDef groundBodyDef; groundBodyDef.position.Set(screenSize.width/2/PTM_RATIO,screenSize.height/2/PTM_RATIO); _groundBody=_world->CreateBody(&groundBodyDef); // Define the ground box shape. b2PolygonShape groundBox; //b2FixtureDef groundBoxDef; //groundBoxDef.shape=&groundBox; // bottom groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, -screenSize.height/2/PTM_RATIO), 0); _groundBody->CreateFixture(&groundBox, 0); // top groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, screenSize.height/2/PTM_RATIO), 0); _groundBody->CreateFixture(&groundBox, 0); // left groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(-screenSize.width/2/PTM_RATIO, 0), 0); _groundBody->CreateFixture(&groundBox, 0); // right groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(screenSize.width/2/PTM_RATIO, 0), 0); _groundBody->CreateFixture(&groundBox, 0); // Create sprite and add it to the layer CCSprite *ball=CCSprite::spriteWithFile("Ball.png",CCRectMake(0,0,52,52)); ball->setPosition(ccp(100,100)); ball->setTag(1); this->addChild(ball); // Create ball body b2BodyDef ballBodyDef; ballBodyDef.type=b2_dynamicBody; ballBodyDef.position.Set(100/PTM_RATIO,100/PTM_RATIO); ballBodyDef.userData=ball; b2Body *ballBody=_world->CreateBody(&ballBodyDef); // Create circle shape b2CircleShape circle; circle.m_radius=26.0/PTM_RATIO; // Create shape definition and add to body b2FixtureDef ballShapeDef; ballShapeDef.shape=&circle; ballShapeDef.density=1.0f; ballShapeDef.friction = 0.0f; ballShapeDef.restitution=1.0f; _ballFix=ballBody->CreateFixture(&ballShapeDef); // Give shape initial impulse... b2Vec2 force=b2Vec2(10,10); ballBody->ApplyLinearImpulse(force,ballBodyDef.position); // Create paddle and add it to the layer CCSprite *paddle=CCSprite::spriteWithFile("Paddle.png"); paddle->setPosition(ccp(screenSize.width/2,50)); this->addChild(paddle); // Create paddle body b2BodyDef paddleBodyDef; paddleBodyDef.type=b2_dynamicBody; paddleBodyDef.position.Set(screenSize.width/2/PTM_RATIO,50/PTM_RATIO); paddleBodyDef.userData=paddle; _paddleBody=_world->CreateBody(&paddleBodyDef); // Create paddle shape b2PolygonShape paddleShape; paddleShape.SetAsBox(paddle->getContentSize().width/2/PTM_RATIO, paddle->getContentSize().height/2/PTM_RATIO); // Create shape definition and add to body b2FixtureDef paddleShapeDef; paddleShapeDef.shape=&paddleShape; paddleShapeDef.density = 10.0f; paddleShapeDef.friction = 0.4f; paddleShapeDef.restitution = 0.1f; _paddleFix=_paddleBody->CreateFixture(&paddleShapeDef); // Restrict paddle along the x axis b2PrismaticJointDef jointDef; b2Vec2 worldAxis(1.0f,0.0f); jointDef.collideConnected=true; jointDef.Initialize(_paddleBody,_groundBody,_paddleBody->GetWorldCenter(),worldAxis); _world->CreateJoint(&jointDef); _mouseJoint=NULL; this->schedule(schedule_selector(HelloWorld::tick)); bRet = true; } while (0); return bRet; } void HelloWorld::tick(cocos2d::ccTime dt) { _world->Step(dt,10,10); for(b2Body *b=_world->GetBodyList();b;b=b->GetNext()) { if(b->GetUserData()!=NULL) { CCSprite *sprite=(CCSprite *)b->GetUserData(); if(sprite->getTag()==1) { static int maxSpeed=10; b2Vec2 velocity=b->GetLinearVelocity(); float32 speed=velocity.Length(); if(speed > maxSpeed) { b->SetLinearDamping(0.5); } else if(speed < maxSpeed) { b->SetLinearDamping(0); } } sprite->setPosition(ccp(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO)); sprite->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle())); } } } void HelloWorld::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent) { if(_mouseJoint!=NULL)return; CCTouch *pTouch=(CCTouch *)pTouches->anyObject(); CCPoint location=pTouch->locationInView(pTouch->view()); location=CCDirector::sharedDirector()->convertToGL(location); b2Vec2 locationWorld=b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); if(_paddleFix->TestPoint(locationWorld)) { b2MouseJointDef md; md.bodyA=_groundBody; md.bodyB=_paddleBody; md.target=locationWorld; md.collideConnected=true; md.maxForce=1000.0f*_paddleBody->GetMass(); _mouseJoint=(b2MouseJoint *)_world->CreateJoint(&md); _paddleBody->SetAwake(true); } } void HelloWorld::ccTouchesMoved(CCSet *pTouches,CC
评论
    相关推荐
    • Cocos2d Game
      I create a Cocos2d game
    • cocos2d富文本
      cocos2d 富文本实现 格式 [i:图片路径|-] [s:22|c:00ff00ff]***[-] [s:22]***[-] [l:item/1001]...[-] 随意组合嵌套
    • Cocos2d学习文档
      Cocos2d学习文档 适合新手 第1章-介绍cocos2d.pdf 第2章-开始学习.pdf 。。。。 第4章-你的第一个游戏(修正错误).pdf 。。。。
    • Cocos2d学习资料
      Cocos2D目录: 第一章:介绍Cocos2d 第二章: 开始学习 第三章: 基础知识 第四章: 你的第一个游戏(修正错误) 第五章: 游戏构成要素 第六章: 深入了解精灵(Sprite) 第七章: 横向滚屏射击游戏 第八章: 完成滚屏射击游戏...
    • cocos2d demo
      本资源是一个小的经典的cocos2d-x的demo,后续每周更新增加功能的版本
    • cocos2d精灵教程
      (译)cocos2d精灵教程:第一部分.pdf (译)cocos2d精灵教程:第三部分.pdf (译)cocos2d精灵教程:第二部分.pdf (译)cocos2d菜单教程:第一部分.pdf (译)cocos2d菜单教程:第三部分(完).pdf (译)...
    • cocos2d模板
      cocos2d-template-updated
    • Cocos2d手势
      为实现Cocos2d手势支持,改写的CCLayer、CCNode、CCGestureRecognizer三个源码类,可以参考我博客使用!
    • Cocos2D文档
      Cocos2D开发参考文档 Cocos2D帮助文档
    • cocos2d实例
      cocos的一些实例代码,有助于初学者。