cocos2d物理引擎测试

  • s4_451129
    了解作者
  • 2.1MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-15 01:37
    上传日期
物理引擎的简单应用代码,给大家分享物理引擎的简单应用代码,给大家分享物理引擎的简单应用代码,给大家分享物理引擎的简单应用代码,给大家分享,
Hello.zip
  • Classes
  • AppDelegate.cpp
    2.9KB
  • Ball.cpp
    2.4KB
  • GameLayer.cpp
    14.3KB
  • LineContainer.cpp
    1.2KB
  • GameLayer.h
    1.7KB
  • Cue.cpp
    1.1KB
  • CollisionListener.h
    629B
  • AppDelegate.h
    987B
  • CollisionListener.cpp
    2.6KB
  • Ball.h
    688B
  • GLES-Render.cpp
    7.7KB
  • GLES-Render.h
    2.1KB
  • Cue.h
    367B
  • b2Sprite.h
    815B
  • b2Sprite.cpp
    1.2KB
  • LineContainer.h
    549B
  • Resources
  • ipadhd
  • bg.png
    1.3MB
  • font.png
    12.5KB
  • sprite_sheet.tps
    3.3KB
  • sprite_sheet.png
    26.4KB
  • font.fnt
    1.3KB
  • sprite_sheet.plist
    3.9KB
  • iphone
  • bg.png
    173.9KB
  • font.png
    7.4KB
  • sprite_sheet.tps
    3.3KB
  • sprite_sheet.png
    6.5KB
  • font.fnt
    1.4KB
  • sprite_sheet.plist
    3.9KB
  • res
  • .gitkeep
    0B
  • fonts
  • Marker Felt.ttf
    25.2KB
  • ipad
  • bg.png
    333.6KB
  • font.png
    9.9KB
  • sprite_sheet.tps
    3.3KB
  • sprite_sheet.png
    12.2KB
  • font.fnt
    1.3KB
  • sprite_sheet.plist
    3.9KB
  • HelloWorld.png
    135.5KB
  • drop.wav
    11.3KB
  • whitedrop.wav
    12.5KB
  • CloseNormal.png
    6.2KB
  • ball.wav
    30.6KB
  • CloseSelected.png
    5.4KB
  • hit.wav
    29.7KB
内容介绍
// // GameLayer.cpp // MiniPool // // Created by Roger Engelbert on 1/13/13. // Copyright __MyCompanyName__ 2013. All rights reserved. // #include "GameLayer.h" #include "CollisionListener.h" #define SHOT_POWER 6.0 using namespace cocos2d; using namespace CocosDenshion; GameLayer::GameLayer() { _screenSize = Director::getInstance()->getWinSize(); auto bg = Sprite::create("bg.png"); bg->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.5f)); this->addChild(bg); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprite_sheet.plist"); _gameBatchNode = SpriteBatchNode::create("sprite_sheet.png", 50); this->addChild(_gameBatchNode, kMiddleground); // init physics this->initPhysics(); //listen for touches auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); scheduleUpdate(); _usingCue = false; _running = true; _canShoot = true; _gameState = kGameIntro; _time = 0; _ballsInPlay = 15; _lineContainer = LineContainer::create(); this->addChild(_lineContainer); _intro = Sprite::createWithSpriteFrameName("logo.png"); _intro->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.55f)); _gameBatchNode->addChild(_intro, kForeground); _gameOver = Sprite::createWithSpriteFrameName("gameover.png"); _gameOver->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.55f)); _gameOver->setVisible(false); _gameBatchNode->addChild(_gameOver, kForeground); _timer = Label::createWithBMFont("font.fnt", "0", TextHAlignment::CENTER); _timer->setAnchorPoint(Vec2(0.5f, 0.0f)); _timer->setPosition(Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.92f)); _timer->setVisible(false); this->addChild(_timer, kForeground); this->schedule(CC_SCHEDULE_SELECTOR(GameLayer::ticktock), 1.5f); } GameLayer::~GameLayer() { delete _world; _world = nullptr; delete _collisionListener; for (auto ball : _balls) ball->release(); for (auto pocket : _pockets) pocket->release(); _balls.clear(); _pockets.clear(); delete _debugDraw; } void GameLayer::initPhysics() { b2Vec2 gravity; gravity.Set(0.0f, 0.0f); _world = new b2World(gravity); _world->SetAllowSleeping(true); _world->SetContinuousPhysics(true); _collisionListener = new CollisionListener(); _world->SetContactListener(_collisionListener); _debugDraw = new (std::nothrow) GLESDebugDraw( PTM_RATIO ); _world->SetDebugDraw(_debugDraw); uint32 flags = 0; flags += b2Draw::e_shapeBit; // flags += b2Draw::e_jointBit; // flags += b2Draw::e_aabbBit; // flags += b2Draw::e_pairBit; // flags += b2Draw::e_centerOfMassBit; _debugDraw->SetFlags(flags); b2FixtureDef fixtureDef; b2BodyDef bodyDef; //Create table //add pockets _pockets = Vector<b2Sprite*>(6); b2CircleShape circle; float startX = _screenSize.width * 0.07; float startY = _screenSize.height * 0.92f; for (int i = 0; i < 6; i++) { bodyDef.type = b2_staticBody; if (i < 3) { bodyDef.position.Set( startX/PTM_RATIO, (startY - i * (_screenSize.height * 0.84f * 0.5f))/PTM_RATIO); } else { bodyDef.position.Set( (startX + _screenSize.width * 0.85f)/PTM_RATIO, (startY - (i-3) * (_screenSize.height * 0.84f * 0.5f))/PTM_RATIO); } auto pocket = _world->CreateBody(&bodyDef); fixtureDef.isSensor = true; circle.m_radius = (float) (1.5 * BALL_RADIUS) / PTM_RATIO; fixtureDef.shape = &circle; pocket->CreateFixture(&fixtureDef); auto pocketData = new b2Sprite(this, kSpritePocket); pocket->SetUserData(pocketData); _pockets.pushBack(pocketData); } //create table sides b2BodyDef tableBodyDef; tableBodyDef.position.Set(0, 0); auto tableBody = _world->CreateBody(&tableBodyDef); // Define the table edges b2EdgeShape tableBox; // bottom tableBox.Set(b2Vec2(_screenSize.width * 0.14f/PTM_RATIO, _screenSize.height * 0.09f/PTM_RATIO), b2Vec2(_screenSize.width * 0.86f/PTM_RATIO, _screenSize.height * 0.09f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); // top tableBox.Set(b2Vec2(_screenSize.width * 0.14f/PTM_RATIO, _screenSize.height * 0.91f/PTM_RATIO), b2Vec2(_screenSize.width * 0.86f/PTM_RATIO, _screenSize.height * 0.91f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); // left tableBox.Set(b2Vec2(_screenSize.width * 0.09f/PTM_RATIO, _screenSize.height * 0.12f/PTM_RATIO), b2Vec2(_screenSize.width * 0.09f/PTM_RATIO, _screenSize.height * 0.46f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); tableBox.Set(b2Vec2(_screenSize.width * 0.91f/PTM_RATIO, _screenSize.height * 0.12f/PTM_RATIO), b2Vec2(_screenSize.width * 0.91f/PTM_RATIO, _screenSize.height * 0.46f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); // right tableBox.Set(b2Vec2(_screenSize.width * 0.09f/PTM_RATIO, _screenSize.height * 0.54f/PTM_RATIO), b2Vec2(_screenSize.width * 0.09f/PTM_RATIO, _screenSize.height * 0.88f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); tableBox.Set(b2Vec2(_screenSize.width * 0.91f/PTM_RATIO, _screenSize.height * 0.54f/PTM_RATIO), b2Vec2(_screenSize.width * 0.91f/PTM_RATIO, _screenSize.height * 0.88f/PTM_RATIO)); tableBody->CreateFixture(&tableBox,0); //add balls _balls = Vector<Ball *>(15); int col = 5; int colCnt = 0; startX = (_screenSize.width - 8 * BALL_RADIUS) * 0.5f; startY = _screenSize.height * 0.82f; float newX; float newY; int color; for (int i = 0; i < 15; i++) { if (i == 10) { color = kColorBlack; } else if (i % 2 == 0) { color = kColorYellow; } else { color = kColorRed; } if (colCnt < col) { newX = startX + colCnt * 2 * BALL_RADIUS; colCnt++; } else { startX += BALL_RADIUS; startY -= 2 * BALL_RADIUS; newX = startX; col--; colCnt = 1; } newY = startY; auto ball = Ball::create(this, kSpriteBall, Vec2(newX, newY), color); _gameBatchNode->addChild(ball, kMiddleground); _balls.pushBack(ball); } //add cue ball _player = Ball::create(this, kSpritePlayer, Vec2(_screenSize.width * 0.5f, _screenSize.height * 0.3f), kColorWhite); _gameBatchNode->addChild(_player, kMiddleground); _cue = Cue::create(this); _cue->setSpritePosition(Vec2(_screenSize.width * 0.2f, _screenSize.height * 0.3f)); _cue->getBody()->SetTransform(_cue->getBody()->GetPosition(), M_PI*0.5); _gameBatchNode->addChild(_cue, kForeground); } void GameLayer::update(float dt) { if (!_running) return; if (_gameState == kGameOver) return; _world->Step(dt, 10, 10); //track invisible objects for (auto ball : _balls) { if (!ball->isVisible() && ball->getInPlay()) { ball->setInPlay(false); ball->hide(); //count down balls _ballsInPlay--; SimpleAudioEngine::getInstance()->playEffect("drop.wav"); if (_ballsInPlay
评论
    相关推荐
    • 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的一些实例代码,有助于初学者。