bool InGameLayer::init() { Layer::init(); //获取屏幕宽度大小 Size winSize = Director::getInstance()->getVisibleSize(); Size ballSize = this->getContentSize(); //创建背景图 Sprite* background = Sprite::create("backMain.jpg"); this->addChild(background); background->setAnchorPoint(Point::ZERO); //创建黄色球 yBall = Sprite::create("mouse_1.png"); this->addChild(yBall); yBall->setPosition(winSize.width - 100,winSize.height - 500); //创建时间计时文本 char chTxt[1024]; sprintf(chTxt,"%d",timeCount); timeTxt = LabelAtlas::create(chTxt,"farAwayCount_38x45.png", 38,45,'0'); this->addChild(timeTxt); timeTxt->setPosition(10,winSize.height - timeTxt->getContentSize().height); //创建小球 createBalls(50); //添加侦听 addEvents(); //添加播放暂停按钮 MenuItemImage* pauseItem = MenuItemImage::create( "pause.png","pause.png", CC_CALLBACK_1(InGameLayer ::pauseCallBack,this)); pauseMenu = Menu::create(pauseItem,NULL); this->addChild(pauseMenu,1); pauseMenu->setPosition(winSize.width - 100,winSize.height - 40); MenuItemImage* replayItem = MenuItemImage::create( "play.png","play.png", CC_CALLBACK_1(InGameLayer ::replayCallBack,this)); Menu* replayMenu = Menu::create(replayItem,NULL); this->addChild(replayMenu,1); replayMenu->setPosition(winSize.width - 50,winSize.height - 40); //遮造物 Sprite* mask = Sprite::create("progressBar1.png"); //被遮物 timeBar = Sprite::create("progressBar.png"); ClippingNode* clip = ClippingNode::create(mask); clip->addChild(timeBar); this->addChild(clip); clip->setPosition(winSize.width / 2,winSize.height - 15); return true; }
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } visibleSize = Director::getInstance()->getVisibleSize(); winSize = Director::getInstance()->getWinSize(); origin = Director::getInstance()->getVisibleOrigin(); Sprite* bg = Sprite::create("HelloWorld.png"); bg->setPosition(visibleSize / 2); //this->addChild(bg, -1); Sprite* gameTitle = Sprite::create("game_title.png"); Size clipSize = gameTitle->getContentSize(); Sprite* spark = Sprite::create("spark.png"); spark->setPosition(-clipSize.width, 0); ClippingNode* clippingNode = ClippingNode::create(); clippingNode->setAlphaThreshold(0); clippingNode->setContentSize(clipSize); clippingNode->setPosition(visibleSize / 2); this->addChild(clippingNode); clippingNode->setStencil(gameTitle); //设置模板 clippingNode->addChild(gameTitle, 1); //先添加标题,会完全显示出来,因为跟模板一样大小 clippingNode->addChild(spark,2); //会被裁减 MoveTo* moveAction = MoveTo::create(2.0f, Vec2(clipSize.width, 0)); MoveTo* moveBackAction = MoveTo::create(2.0f, Vec2(-clipSize.width, 0)); Sequence* seq = Sequence::create(moveAction, moveBackAction, NULL); RepeatForever* repeatAction = RepeatForever::create(seq); spark->runAction(repeatAction); //进行左右移动的重复动作 return true; }