HelloWorldScene.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "HelloWorldScene.h"
  2. #include "GameScene.h"
  3. #include "SimpleAudioEngine.h"
  4. #include "Ball.h"
  5. #include "params.h"
  6. USING_NS_CC;
  7. Scene* HelloWorld::createScene() {
  8. auto scene = Scene::createWithPhysics();
  9. Size visibleSize = Director::getInstance()->getVisibleSize();
  10. //choose whitch part need to draw, Joint, Shape, Contact, None or All
  11. //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  12. auto layer = HelloWorld::create();
  13. scene->addChild(layer);
  14. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  15. material.density = 1.0f;
  16. material.restitution = 1.0f;
  17. material.friction = 0.0f;
  18. // the edge of the screen
  19. auto body = PhysicsBody::createEdgeBox(visibleSize, material);
  20. auto edgeNode = Node::create();
  21. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  22. body->setDynamic(false);
  23. edgeNode->setPhysicsBody(body);
  24. scene->addChild(edgeNode);
  25. return scene;
  26. }
  27. // on "init" you need to initialize your instance
  28. bool HelloWorld::init() {
  29. //////////////////////////////
  30. // 1. super init first
  31. if ( !Layer::init() )
  32. {
  33. return false;
  34. }
  35. auto visibleSize = Director::getInstance()->getVisibleSize();
  36. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  37. // Adiona o fundo
  38. auto bg = cocos2d::LayerColor::create(COLOR_back);
  39. this->addChild(bg);
  40. /////////////////////////////
  41. // 2. add a menu item with "X" image, which is clicked to quit the program
  42. // you may modify it.
  43. // add a "close" icon to exit the progress. it's an autorelease object
  44. // auto closeItem = MenuItemImage::create(
  45. // "CloseNormal.png",
  46. // "CloseSelected.png",
  47. // CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
  48. // closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
  49. // origin.y + closeItem->getContentSize().height/2));
  50. // // create menu, it's an autorelease object
  51. // auto menu = Menu::create(closeItem, NULL);
  52. // menu->setPosition(Vec2::ZERO);
  53. // this->addChild(menu, 1);
  54. /////////////////////////////
  55. // 3. add your codes below...
  56. // add a label shows "Hello World"
  57. // create and initialize a label
  58. auto menu_item_start = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::Play, this));
  59. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  60. auto *menu = Menu::create(menu_item_start, NULL);
  61. menu->setPosition(Point(0, 0));
  62. this->addChild(menu, 10);
  63. for (int i = 0; i < 10; i++) {
  64. auto ball = Ball::create();
  65. ball->setPosition(visibleSize.width / 2, BALL_SIZE*2);
  66. ball->throwBall();
  67. this->addChild(ball);
  68. }
  69. return true;
  70. }
  71. void HelloWorld::Play(cocos2d::Ref *pSender) {
  72. auto scene = GameScene::createScene(0);
  73. Director::getInstance()->replaceScene(scene);
  74. //Director::getInstance()->end();
  75. }