#include "HelloWorldScene.h" #include "GameScene.h" #include "SimpleAudioEngine.h" #include "Ball.h" #include "params.h" USING_NS_CC; Scene* HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); Size visibleSize = Director::getInstance()->getVisibleSize(); //choose whitch part need to draw, Joint, Shape, Contact, None or All //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); auto layer = HelloWorld::create(); scene->addChild(layer); auto material = PHYSICSBODY_MATERIAL_DEFAULT; material.density = 1.0f; material.restitution = 1.0f; material.friction = 0.0f; // the edge of the screen auto body = PhysicsBody::createEdgeBox(visibleSize, material); auto edgeNode = Node::create(); edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); body->setDynamic(false); edgeNode->setPhysicsBody(body); scene->addChild(edgeNode); return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // Adiona o fundo auto bg = cocos2d::LayerColor::create(COLOR_back); this->addChild(bg); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object // auto closeItem = MenuItemImage::create( // "CloseNormal.png", // "CloseSelected.png", // CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); // closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , // origin.y + closeItem->getContentSize().height/2)); // // create menu, it's an autorelease object // auto menu = Menu::create(closeItem, NULL); // menu->setPosition(Vec2::ZERO); // this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto menu_item_start = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::Play, this)); menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.25))); auto *menu = Menu::create(menu_item_start, NULL); menu->setPosition(Point(0, 0)); this->addChild(menu, 10); for (int i = 0; i < 10; i++) { auto ball = Ball::create(); ball->setPosition(visibleSize.width / 2, BALL_SIZE*2); ball->throwBall(); this->addChild(ball); } auto core = DrawNode::create(); core->drawDot(Vec2(0, 0), CORE_RADIUS*4, Color4F(COLOR_red)); core->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.75))); this->addChild(core); auto physicsBody = PhysicsBody::createCircle(CORE_RADIUS*4, PhysicsMaterial(0.1f, 0.9f, 0.0f)); physicsBody->setDynamic(false); core->addComponent(physicsBody); auto text = Label::createWithTTF("RedCore", "fonts/Marker Felt.ttf", 100); text->setPosition(core->getPosition()); this->addChild(text); double last, diff; int level; UserDefault *userdata = UserDefault::getInstance(); level = userdata->getIntegerForKey("level"); last = userdata->getDoubleForKey("time"); unsigned long int sec = time(NULL); diff = 3600 * 12 + last - sec; if (last < 0) { int c = last/(3600 * 12); // quantas vidas ja se passaram last += c*3600.0*12.0; userdata->setDoubleForKey("time", last); level -= c; userdata->setIntegerForKey("level", level); diff = 3600 * 12 + last - sec; } char level_text[256]; if (level != 0) { sprintf(level_text,"You are at level %d. In %.1f hour(s) you will regrow a level.", level, diff/3600.0); auto textlevel = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 20); textlevel->setPosition(menu_item_start->getPositionX(), menu_item_start->getPositionY()-60); this->addChild(textlevel); } return true; } void HelloWorld::Play(cocos2d::Ref *pSender) { auto scene = GameScene::createScene(0); Director::getInstance()->replaceScene(scene); //Director::getInstance()->end(); }