void GameScene::update(float dt) { //隨機產生0(沒有)或1(100分)或2(200分) int n = random(0, 2); int randomHole = random(0, 8); if(n==1) { //只有兩種可能時,createRabbits(randomHole, n == 1 ? _NORMALRABBIT : _ANGRYRABBIT); createRabbits(randomHole, _NORMALRABBIT); } else if(n==2) { createRabbits(randomHole, _ANGRYRABBIT); } else { _touchArray[randomHole] = false; //代表不能點擊 ClippingNode *clippingSprite = _clippingNodeArray.at(randomHole); Sprite *rabbitSprite = _rabbitMap.at(randomHole); clippingSprite->removeChild(rabbitSprite); } }
//創建兔子 void GameScene::createRabbits(int randomHole, int type) { ClippingNode *clippingSprite = _clippingNodeArray.at(randomHole); Sprite *rabbitSprite = _rabbitMap.at(randomHole); if(!clippingSprite->getChildren().contains(rabbitSprite)) { //底板(兔兔) Sprite *rabbit = Sprite::createWithSpriteFrameName(__String::createWithFormat("download_rabbit_0%d.png",type)->getCString()); rabbit->setPosition(Vec2(0, -110)); rabbit->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); rabbit->setTag(10000000 + type); auto move = MoveTo::create(0.1f, Vec2(0, -10)); auto moveBack = MoveTo::create(0.1f, Vec2(0, -200)); //CC_CALLBACK_x可以帶很多參數,可是必須加在this後面,x代表的是程序強迫帶的參數個數 //ex:CC_CALLBACK_0(cppName::functionName, this, var1, var2...); auto animateDone = CallFunc::create(CC_CALLBACK_0(GameScene::actionFinished, this, randomHole) ); ActionInterval* delay = Sequence::create(move, DelayTime::create(1), moveBack, animateDone, NULL); rabbit->runAction(delay); _touchArray[randomHole] = true; //代表開放第一次點擊 _rabbitMap.insert(randomHole, rabbit); _clippingNodeArray.at(randomHole)->addChild(rabbit); } }
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; }
ClippingNode* ClippingNode::create(Node *pStencil) { ClippingNode *ret = new (std::nothrow) ClippingNode(); if (ret && ret->init(pStencil)) { ret->autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; }
ClippingNode* ClippingNode::create() { ClippingNode *ret = new ClippingNode(); if (ret && ret->init()) { ret->autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; }
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; }
void GLView::render() { m_render = Director::getInstance()->getRenderer(); initTexture2d(); ClippingNode* clippingNode = ClippingNode::create(); Sprite* stencil = Sprite::createWithFileName("E://5.png"); Sprite* back = Sprite::createWithFileName("E://3.png"); // back->setPosition(200, 10); stencil->setPosition(200, 10); clippingNode->setSprite(back); clippingNode->setStencil(stencil); clippingNode->setAlpha(1.0); bool saveImage = false; //截图功能 RenderTexure bool testClippingNode = false; //遮罩功能ClippingNode bool testDrawNodePoint = false; //DrawNode功能之画点 bool testDrawNodeLine = true; //DrawNode功能之画线 /* Loop until the user closes the window */ while (!glfwWindowShouldClose(_mainWindow)) { glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT); if (saveImage) { /* 测试 RenderTexture Size size = Director::getInstance()->getWinSize(); RenderTexture* renderTexure = RenderTexture::create(20,20); renderTexure->begin(); renderTexure->end(); renderTexure->onBegin(); for (Sprite* sprite : _sprites) { sprite->setPosition(20, 20); sprite->visit(&m_render); sprite->setPosition(0, 0); } m_render.draw(); renderTexure->onEnd(); renderTexure->saveToFile("1.png"); saveImage = false; CC_SAFE_DELETE(renderTexure);*/ } else if (testClippingNode){ /* 测试ClippingNode auto _director = Director::getInstance(); clippingNode->onBeforeVisit(); stencil->visit(&m_render); m_render.draw(); clippingNode->onAfterVisitStencil(); back->visit(&m_render); m_render.draw(); clippingNode->onAfterVisit();*/ } else if (testDrawNodePoint) { DrawNode* drawNode = DrawNode::create(); CHECK_GL_ERROR_DEBUG(); for (int i = 1; i <= 100; i++) { drawNode->drawPoint(Vec2(100+i, 100), 1, Color4F(255.0, 0, 0, 255.0)); } drawNode->onDrawPoinit(); // CHECK_GL_ERROR_DEBUG(); }else if (testDrawNodeLine) { DrawNode* drawNode = DrawNode::create(); CHECK_GL_ERROR_DEBUG(); drawNode->drawLine(Vec2(100, 100), Vec2(200, 200), Color4F(255.0, 0, 0, 255.0)); drawNode->drawLine(Vec2(100, 200), Vec2(200, 100), Color4F(255.0, 0, 0, 255.0)); drawNode->onDrawLine(); } else{ for (Sprite* sprite : _sprites) { sprite->visit(&m_render); } m_render.draw(); } /* Swap front and back buffers */ glfwSwapBuffers(_mainWindow); /* Poll for and process events */ glfwPollEvents(); } }
void GameResultSuccess::playWingAction(void) { auto NodeWing = (Node*)GameMainLayer::seekNodeByName(_myLayer, "NodeWing"); NodeWing->setScale(1.2f); // 右流光效果 auto imgLeftWing = Sprite::create("ccsRes/resResoutLayer/ImgWing.png"); imgLeftWing->setAnchorPoint(Vec2(0, 0.5)); Size clipSize = imgLeftWing->getContentSize(); Sprite* spark = Sprite::create("FirstUIRes/Sptigeguang.png"); spark->setScaleY(1.2f); //spark->setPosition(-clipSize.width, -50); ClippingNode* clippingLeftNode = ClippingNode::create(); NodeWing->addChild(clippingLeftNode); clippingLeftNode->setAlphaThreshold(0.05f); //设置alpha闸值 clippingLeftNode->setContentSize(clipSize); //设置尺寸大小 clippingLeftNode->setStencil(imgLeftWing); //设置模板stencil clippingLeftNode->addChild(imgLeftWing, 1); //先添加标题,会完全显示出来,因为跟模板一样大小 clippingLeftNode->addChild(spark, 2); //会被裁减 MoveTo* moveAction = MoveTo::create(1.0f, Vec2(clipSize.width, 0)); MoveTo* moveBackAction = MoveTo::create(0.01f, Vec2(-clipSize.width, 0)); spark->runAction(RepeatForever::create(Sequence::create(CCDelayTime::create(1.5f), moveAction, moveBackAction, NULL))); // 左流光效果 auto imgRightWing = Sprite::create("ccsRes/resResoutLayer/ImgWing.png"); imgRightWing->setFlipX(true); imgRightWing->setAnchorPoint(Vec2(1, 0.5)); clipSize = imgRightWing->getContentSize(); Sprite* rightspark = Sprite::create("FirstUIRes/Sptigeguang.png"); rightspark->setFlipX(true); rightspark->setScaleY(1.2f); //spark->setPosition(-clipSize.width, -50); ClippingNode* clippingRightNode = ClippingNode::create(); NodeWing->addChild(clippingRightNode); clippingRightNode->setAlphaThreshold(0.05f); //设置alpha闸值 clippingRightNode->setContentSize(clipSize); //设置尺寸大小 clippingRightNode->setStencil(imgRightWing); //设置模板stencil clippingRightNode->addChild(imgRightWing, 1); //先添加标题,会完全显示出来,因为跟模板一样大小 clippingRightNode->addChild(rightspark, 2); //会被裁减 MoveTo* moveAction1 = MoveTo::create(1.0f, Vec2(-clipSize.width, 0)); MoveTo* moveBackAction1 = MoveTo::create(0.01f, Vec2(clipSize.width, 0)); rightspark->runAction(RepeatForever::create(Sequence::create(CCDelayTime::create(1.5f), moveAction1, moveBackAction1, NULL))); }
bool ExorbitanceScene::init() { if(!Layer::init()) { return false; } SpriteFrameCache::getInstance()->addSpriteFramesWithFile(HERO_PICTURE_PLIST); std::string df = FileUtils::getInstance()->getWritablePath() + SAVEYOURSISTERSQL; if(!FileUtils::getInstance()->isFileExist(df)) { log("There is a sqlite!"); } else { std::string fe = FileUtils::getInstance()->fullPathForFilename(SAVEYOURSISTERSQL); std::string df = FileUtils::getInstance()->getWritablePath() + SAVEYOURSISTERSQL; log("database path == %s", df.c_str()); fileCopy(fe.c_str(),df.c_str()); } //数据库操作 opendb(); querySkillDb(); queryHeroDb(); queryGearDb(); //开始界面显示 Size vSize = Director::getInstance()->getVisibleSize(); //背景 Sprite * ground = Sprite::create("startground.png"); ground->setPosition(vSize/2); this->addChild(ground); //进入游戏按钮实现 Vec2 vOrigin = Director::getInstance()->getVisibleOrigin(); ClippingNode * cl = ClippingNode::create(); cl->setAlphaThreshold(0.5f); cl->setAnchorPoint(Vec2(0.5,0.5)); this->addChild(cl); auto l1 = Label::createWithTTF("SAVE,YOUR,SISTER","Marker Felt.ttf",80,Size::ZERO, TextHAlignment::CENTER); l1->setColor(Color3B::BLACK); l1->setPosition(400,500); cl->addChild(l1); auto sp = Sprite::create("Orb_Icons_003.png"); sp->setPosition(l1->getPosition().x - l1->getContentSize().width/2,l1->getPositionY()); cl->setStencil(sp); auto mv = MoveBy::create(fabsf(vSize.width/100),Vec2(vSize.width-l1->getPositionX(),0)); sp->runAction(RepeatForever::create(Sequence::create(mv,mv->reverse(),NULL))); auto bg = Sprite::create("jnm.png"); bg->setPosition(300,300); this->addChild(bg); auto lb = Label::createWithTTF("Please Click On The Screen","Marker Felt.ttf",30,Size::ZERO, TextHAlignment::CENTER); lb->setPosition(600,300); lb->setColor(Color3B::BLACK); lb->runAction(RepeatForever::create(Sequence::create(FadeIn::create(0.8),FadeOut::create(0.8),NULL))); this->addChild(lb); auto listen = EventListenerTouchOneByOne::create(); listen->onTouchBegan = [=](Touch *t,Event *e) { auto scene = StartScene::createScene(); Director::getInstance()->replaceScene(scene); SimpleAudioEngine::getInstance()->playEffect("select.wav"); return true; }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen,this); return true; }
ClippingNode* ClippingNode::create() { ClippingNode* newNode = new ClippingNode(); newNode->autorelease(); return newNode; }