Scene* HelloWorld::scene() { return HelloWorld::create(); } boolHelloWorld::init(){ //内部实现所有自己要搞的功能,重载原Scene对象的init()函数 auto visibleSize = Director::getInstance()->getVisibleSize(); //窗口对角线向量 auto origin = Director::getInstance()->getVisibleOrigin(); //左下角(0,0) // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); //第一个是未选中时的图片,第二个是选中时的图片 //第三个是回调函数 closeItem->setPosition(origin + Vec2(visibleSize) - Vec2(closeItem->getContentSize() / 2));
auto menu = Menu::create(closeItem, nullptr); //此时menu已经和closeitem绑定,位置也相互绑定 menu->setPosition(Vec2::ZERO); //但还是要先初始化一下位置,sb cocos //而且莫名其妙这里就变成相对位置了 this->addChild(menu, 1); //调用addchild方法置于图层1,不然会被初始图层0覆盖 ///////////////////////////// // 3. add your codes below...
// add a label shows "Hello World" // create and initialize a label
auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", TITLE_FONT_SIZE); //注意到这里的font size需要手动指明,没有默认字体大小给你 // position the label on the center of the screen label->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height); //注意到这不是函数重载,只传入二维向量时要写明vec2方法以接收两个参数 // add the label as a child to this layer this->addChild(label, 1);
// add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize / 2) + origin); //如果默认从(0,0)开始又为什么要把origin加上去呢
// add the sprite as a child to this layer this->addChild(sprite); //神奇的是精灵放在-1不会被覆盖,可能默认精灵不会重叠吧 auto drawNode = DrawNode::create(); drawNode->setPosition(Vec2(0, 0)); addChild(drawNode);
Rect safeArea = Director::getInstance()->getSafeAreaRect(); drawNode->drawRect(safeArea.origin, safeArea.origin + safeArea.size, Color4F::BLUE);