void CCClipOut::update(float time) { CCClippingNode* clipper = (CCClippingNode*)getTarget(); CCDrawNode* stencil = (CCDrawNode*)clipper->getStencil(); // clear stencil->clear(); // build polygon float distance = m_distance * (1 - time); if(distance < FLT_EPSILON) { CCPoint v[] = { CCPointZero, CCPointZero, CCPointZero, CCPointZero }; stencil->drawPolygon(v, 4, cc4fGREEN, 0, cc4fTRANSPARENT); } else { CCPoint v[] = { m_p1, m_p2, ccp(m_p2.x + distance * m_cos, m_p2.y + distance * m_sin), ccp(m_p1.x + distance * m_cos, m_p1.y + distance * m_sin) }; stencil->drawPolygon(v, 4, cc4fGREEN, 0, cc4fTRANSPARENT); } }
bool Background::Init() { do { CCNodeLoaderLibrary * ccNodeLoaderLibraryLeft = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); CCBReader * ccbReaderLeft = new CCBReader(ccNodeLoaderLibraryLeft); CCNode * bgLeft = ccbReaderLeft->readNodeGraphFromFile("ccb/background.ccbi", this); CCBAnimationManager* animationManagerLeft = ccbReaderLeft->getAnimationManager(); animationManagerLeft->runAnimationsForSequenceNamed("left"); ccbReaderLeft->release(); CCNodeLoaderLibrary * ccNodeLoaderLibraryRight = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); CCBReader * ccbReaderRight = new CCBReader(ccNodeLoaderLibraryRight); CCNode * bgRight = ccbReaderRight->readNodeGraphFromFile("ccb/background.ccbi", this); CCBAnimationManager* animationManagerRight = ccbReaderRight->getAnimationManager(); animationManagerRight->runAnimationsForSequenceNamed("right"); ccbReaderRight->release(); CCClippingNode * clippingNodeLeft = CCClippingNode::create(); addChild(clippingNodeLeft); clippingNodeLeft->addChild(bgLeft,enZOrderBack,enTagBgLeft); CCDrawNode *shapeLeft = CCDrawNode::create(); static CCPoint triangleLeft[3]; triangleLeft[0] = ccp(0, 0); triangleLeft[1] = ccp(SCREEN_WIDTH,0); triangleLeft[2] = ccp(0,SCREEN_HEIGHT); static ccColor4F green = {0, 1, 0, 1}; shapeLeft->drawPolygon(triangleLeft, 3, green, 0, green); clippingNodeLeft->setStencil(shapeLeft); clippingNodeLeft->setInverted(false); CCClippingNode * clippingNodeRight = CCClippingNode::create(); addChild(clippingNodeRight); clippingNodeRight->addChild(bgRight,enZOrderBack,enTagBgRight); CCDrawNode *shapeRight = CCDrawNode::create(); static CCPoint triangleRight[3]; triangleRight[0] = ccp(SCREEN_WIDTH, SCREEN_HEIGHT); triangleRight[1] = ccp(SCREEN_WIDTH,0); triangleRight[2] = ccp(0,SCREEN_HEIGHT); // static ccColor4F green = {0, 1, 0, 1}; shapeRight->drawPolygon(triangleRight, 3, green, 0, green); clippingNodeRight->setStencil(shapeRight); clippingNodeRight->setInverted(false); m_CraterArrayLeft = CCArray::create(); m_CraterArrayLeft->retain(); m_CraterArrayRight = CCArray::create(); m_CraterArrayRight->retain(); m_CraterArrayLeftDeleted = CCArray::create(); m_CraterArrayLeftDeleted->retain(); m_CraterArrayRightDeleted = CCArray::create(); m_CraterArrayRightDeleted->retain(); return true; } while (false); CCLog("Function Background::Init Error!"); return false; }
// DrawNodeTest DrawNodeTest::DrawNodeTest() { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCDrawNode *draw = CCDrawNode::create(); addChild(draw, 10); // Draw 10 circles for( int i=0; i < 10; i++) { draw->drawDot(ccp(s.width/2, s.height/2), 10*(10-i), ccc4f(CCRAScutOM_0_1(), CCRAScutOM_0_1(), CCRAScutOM_0_1(), 1)); } // Draw polygons CCPoint points[] = { CCPoint(s.height/4,0), CCPoint(s.width,s.height/5), CCPoint(s.width/3*2,s.height) }; draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), ccc4f(1,0,0,0.5), 4, ccc4f(0,0,1,1)); // star poly (triggers buggs) { const float o=80; const float w=20; const float h=50; CCPoint star[] = { CCPoint(o+w,o-h), CCPoint(o+w*2, o), // lower spike CCPoint(o + w*2 + h, o+w ), CCPoint(o + w*2, o+w*2), // right spike // {o +w, o+w*2+h}, {o,o+w*2}, // top spike // {o -h, o+w}, {o,o}, // left spike }; draw->drawPolygon(star, sizeof(star)/sizeof(star[0]), ccc4f(1,0,0,0.5), 1, ccc4f(0,0,1,1)); } // star poly (doesn't trigger bug... order is important un tesselation is supported. { const float o=180; const float w=20; const float h=50; CCPoint star[] = { CCPoint(o,o), CCPoint(o+w,o-h), CCPoint(o+w*2, o), // lower spike CCPoint(o + w*2 + h, o+w ), CCPoint(o + w*2, o+w*2), // right spike CCPoint(o +w, o+w*2+h), CCPoint(o,o+w*2), // top spike CCPoint(o -h, o+w), // left spike }; draw->drawPolygon(star, sizeof(star)/sizeof(star[0]), ccc4f(1,0,0,0.5), 1, ccc4f(0,0,1,1)); } // Draw segment draw->drawSegment(ccp(20,s.height), ccp(20,s.height/2), 10, ccc4f(0, 1, 0, 1)); draw->drawSegment(ccp(10,s.height/2), ccp(s.width/2, s.height/2), 40, ccc4f(1, 0, 1, 0.5)); }
void ScrollViewDemo::setup() { CCClippingNode *clipper = CCClippingNode::create(); clipper->setTag( kTagClipperNode ); clipper->setContentSize( CCSizeMake(200, 200) ); clipper->setAnchorPoint( ccp(0.5, 0.5) ); clipper->setPosition( ccp(this->getContentSize().width / 2, this->getContentSize().height / 2) ); clipper->runAction(CCRepeatForever::create(CCRotateBy::create(1, 45))); this->addChild(clipper); CCDrawNode *stencil = CCDrawNode::create(); CCPoint rectangle[4]; rectangle[0] = ccp(0, 0); rectangle[1] = ccp(clipper->getContentSize().width, 0); rectangle[2] = ccp(clipper->getContentSize().width, clipper->getContentSize().height); rectangle[3] = ccp(0, clipper->getContentSize().height); ccColor4F white = {1, 1, 1, 1}; stencil->drawPolygon(rectangle, 4, white, 1, white); clipper->setStencil(stencil); CCSprite *content = CCSprite::create(s_back2); content->setTag( kTagContentNode ); content->setAnchorPoint( ccp(0.5, 0.5) ); content->setPosition( ccp(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) ); clipper->addChild(content); m_bScrolling = false; this->setTouchEnabled(true); }
bool CardBase::ccTouchBegan(CCTouch* touch, CCEvent* event) { glLineWidth(16); ccDrawColor4B(255,0,0,255); CCPoint vertices[] = { ccp(0, 0), ccp(50, 50), ccp(100, 50), ccp(100, 100) }; ccDrawPoly(vertices, 4, true); //glLineWidth(16); //ccDrawColor4B(0, 255, 0, 255); //ccDrawCircle( VisibleRect::center(), 100, 0, 10, false); CCSize s = this->backGround->getContentSize(); CCDrawNode *draw = CCDrawNode::create(); addChild(draw, 10); // Draw polygons CCPoint points[] = { CCPoint(s.height/4,0), CCPoint(s.width,s.height/5), CCPoint(s.width/3*2,s.height) }; draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), ccc4f(1,0,0,0.5), 0, ccc4f(0,0,0,0)); // Draw segment //draw->drawSegment(ccp(20,s.height), ccp(20,s.height/2), 10, ccc4f(0, 1, 0, 0.5)); //draw->drawSegment(ccp(10,s.height/2), ccp(s.width/2, s.height/2), 40, ccc4f(1, 0, 1, 0.5)); // this->addChild(shadow); // addChild(shadow); CCLog("ccTouchBegan"); return true; }
void GameLogic_init::clip(CCClippingNode* clipping, cocos2d::CCPoint _center, float wh){ CCDrawNode *stencil = (CCDrawNode*)clipping->getStencil(); CCPoint rectangle[4]; rectangle[0] = ccp(_center.x-wh/2, _center.y-wh/2); rectangle[1] = ccp(_center.x+wh/2, _center.y-wh/2); rectangle[2] = ccp(_center.x+wh/2,_center.y+wh/2); rectangle[3] = ccp(_center.x-wh/2, _center.y+wh/2); ccColor4F white = {0, 0, 0, 1}; stencil->drawPolygon(rectangle, 4, white, 1, white); }
CCDrawNode* BasicTest::shape() { CCDrawNode *shape = CCDrawNode::create(); static CCPoint triangle[3]; triangle[0] = ccp(-100, -100); triangle[1] = ccp(100, -100); triangle[2] = ccp(0, 100); static ccColor4F green = {0, 1, 0, 1}; shape->drawPolygon(triangle, 3, green, 0, green); return shape; }
CCDrawNode* createPentNode( ccColor4F fill, ccColor4F outline) { CCPoint pent[5]; pent[0] = CCPointMake(0,10); pent[1] = CCPointMake(10,2); pent[2] = CCPointMake(5,-7); pent[3] = CCPointMake(-5,-7); pent[4] = CCPointMake(-10,2); CCDrawNode* pt = CCDrawNode::create(); pt->drawPolygon(pent, 5, fill, 1, outline); return pt; }
void CCClipIn::update(float time) { CCClippingNode* clipper = (CCClippingNode*)getTarget(); CCDrawNode* stencil = (CCDrawNode*)clipper->getStencil(); // clear stencil->clear(); // build polygon float distance = m_distance * time; CCPoint v[] = { m_p1, m_p2, ccp(m_p2.x + distance * m_cos, m_p2.y + distance * m_sin), ccp(m_p1.x + distance * m_cos, m_p1.y + distance * m_sin) }; stencil->drawPolygon(v, 4, cc4fGREEN, 0, cc4fTRANSPARENT); }
/*创建运动背景*/ CCDrawNode* GameStage::createMotionWall(float x, float y, float bgWidth, float bgHeight, float gapH, ccColor4F color) { int num = (int)(this->stageWidth / (bgWidth + gapH)); CCDrawNode* drawNode = CCDrawNode::create(); for (int i = 0; i < num; i+=1) { int startX = i * (bgWidth + gapH) + x; CCPoint points[] = {CCPoint(startX, y), CCPoint(startX, bgHeight + y), CCPoint(startX + bgWidth, bgHeight + y), CCPoint(startX + bgWidth, y)}; //顶点列表,顶点数量,颜色 drawNode->drawPolygon(points, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); } //如果不设置宽高则 始终为0,即使内部有复杂的绘图。 drawNode->setContentSize(CCSizeMake(num * (bgWidth + gapH), bgHeight)); return drawNode; }
/*创建运动前景*/ CCDrawNode* GameStage::createFrontMotionWall(float x, float y, float bgWidth, float bgHeight, int tag) { //前景列表 CCDrawNode* drawNode = CCDrawNode::create(); ccColor4F color = ColorUtil::getColor4F(141, 143, 129, 255); CCPoint points[] = {CCPoint(x, y), CCPoint(x, bgHeight + y), CCPoint(x + bgWidth, bgHeight + y), CCPoint(x + bgWidth, y)}; //顶点列表,顶点数量,颜色 drawNode->drawPolygon(points, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); //前景中段 float my = y + 97; float mHeight = 93; color = ColorUtil::getColor4F(103, 107, 82, 255); CCPoint points2[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points2, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 3; my = my - mHeight; color = ColorUtil::getColor4F(86, 90, 67, 255); CCPoint points3[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points3, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 2; my = my - mHeight; color = ColorUtil::getColor4F(100, 103, 82, 255); CCPoint points4[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points4, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); mHeight = 1; my = my - mHeight; color = ColorUtil::getColor4F(113, 116, 99, 255); CCPoint points5[] = {CCPoint(x, my), CCPoint(x, mHeight + my), CCPoint(x + bgWidth, mHeight + my), CCPoint(x + bgWidth, my)}; drawNode->drawPolygon(points5, 4, color, 0, ColorUtil::getColor4F(0, 0, 0, 0)); drawNode->setAnchorPoint(ccp(0, 0)); drawNode->setContentSize(CCSizeMake(bgWidth, bgHeight)); //创建阴影 设置注册点在top和center CCSprite* shadow = CCSprite::create("shadow.png"); shadow->setTag(tag); shadow->setAnchorPoint(ccp(.5, 1)); shadow->setPosition(ccp(shadow->getContentSize().width *.5, y)); drawNode->setTag(tag); drawNode->addChild(shadow); return drawNode; }
bool TipsScene::init() { if ( !CCLayer::init()) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); mCallFunc = NULL; targetRidus = 100; //创建一个遮罩层。 CCLayerColor* pLayer; if (isHasTarget && !istouchRemove) { pLayer=CCLayerColor::create(ccc4(0,0,0,200)); /*创建剪裁节点*/ CCClippingNode* pClip = CCClippingNode::create() ; pClip->setInverted(true) ;//设置是否反向,将决定画出来的区域是透明的还是黑色的。 this->addChild(pClip) ; /*注意将LayerColor层添加到剪裁节点上*/ pClip->addChild(pLayer) ; //模板 CCDrawNode* pStencil = CCDrawNode::create() ; static ccColor4F red = {1,0,0,1} ; float radius = targetRidus/2 ; const int nCount = 200 ; const float angel = 2.0 * (float)M_PI/nCount ; static CCPoint circle[nCount] ; for (int i=0; i<nCount; i++) { float radian = i*angel ; //弧度 circle[i].x = radius * cosf(radian) ; circle[i].y = radius * sinf(radian) ; } pStencil->drawPolygon(circle, nCount, red, 0, red) ; pStencil->runAction(CCRepeatForever::create(CCSequence::createWithTwoActions(CCScaleBy::create(0.2f, 0.9f), CCScaleTo::create(0.2f, 1)))) ; pStencil->setPosition(targetPoint); pClip->setStencil(pStencil) ; //将这个区域从剪裁节点上面抠出来。Stencil是模板的意思 } else if (shouldPauseGame) { pLayer=CCLayerColor::create(ccc4(0,0,0,100)); this->addChild(pLayer) ; } else { } if (isAutoRemove) { scheduleOnce(schedule_selector(TipsScene::scheduleResumeGame),2.8); scheduleOnce(schedule_selector(TipsScene::scheduleRemove),3.0f); } CCSprite* tipsbg = CCSprite::create("tipbg.png"); //tipsbg -> setPosition(ccp(375,260)); tipsbg -> setPosition(ccp(375,260 + 160)); addChild(tipsbg); mTipsWord = CCLabelTTF::create("","Arial",20); mTipsWord -> setColor(ccYELLOW); //mTipsWord -> setPosition(ccp(400,240)); mTipsWord -> setPosition(ccp(400,240 + 160)); addChild(mTipsWord); setTouchEnabled(true); if (shouldPauseGame) { BattleManage* pauseLayer = BattleManage::oneself; pauseLayer -> pauseSchedulerAndActions(); } return true; }
bool GameWorld::init() { do { CC_BREAK_IF(! CCLayer::init()); CCMenuItemImage *pTransform = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuTransformCallback)); CCMenuItemImage *pLeft = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuLeftCallback)); CCMenuItemImage *pRight = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuRightCallback)); CCMenuItemImage *pDown = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuDownCallback)); CCMenuItemImage *pGameOver = CCMenuItemImage::create( "GameOver.jpg", "GameOver.jpg", this, menu_selector(GameWorld::menuGameOver)); // Place the menu item bottom-right conner. CCSize size = CCDirector::sharedDirector()->getWinSize(); pTransform->setPosition(ccp(size.width - 190, 210)); pLeft->setPosition(ccp(size.width - 260, 140)); pRight->setPosition(ccp(size.width - 120, 140)); pDown->setPosition(ccp(size.width - 190, 70)); pGameOver->setPosition(ccp(size.width / 2, size.height / 2)); pGameOver->setScale(5); pGameOver->setVisible(false); mMenuGameOver = pGameOver; // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pTransform, pLeft, pRight, pDown, pGameOver, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); addChild(pMenu, 1); CCDrawNode *pDrawNode = CCDrawNode::create(); addChild(pDrawNode); CCPoint points[] = { CCPoint(0,0), CCPoint((CubeManager::MAIN_BOARD_WIDTH)* CubeManager::CUBE_SIZE, 0), CCPoint((CubeManager::MAIN_BOARD_WIDTH)* CubeManager::CUBE_SIZE, (CubeManager::MAIN_BOARD_HEIGHT + 1)* CubeManager::CUBE_SIZE), CCPoint(0, (CubeManager::MAIN_BOARD_HEIGHT + 1)* CubeManager::CUBE_SIZE) }; pDrawNode->drawPolygon(points, sizeof(points)/sizeof(points[0]), ccc4f(0,0,0.3f,0.5f), 4, ccc4f(0,0,1,1)); CCLabelTTF* pLabel = CCLabelTTF::create("Score:", "Arial", 56); pLabel->setPosition(ccp(size.width - 300, 1100)); pLabel->setAnchorPoint(ccp(0, 0.5f)); this->addChild(pLabel); pLabel = CCLabelTTF::create("0", "Arial", 56); pLabel->setPosition(ccp(size.width - 300, 1000)); pLabel->setColor(ccc3(255, 192, 0)); pLabel->setAnchorPoint(ccp(0, 0.5f)); //pLabel->setHorizontalAlignment(kCCTextAlignmentRight); this->addChild(pLabel); mLabelScore = pLabel; if (!mCubeManager.init(this)) return false; schedule(schedule_selector(GameWorld::stepGo), 0.5f); schedule(schedule_selector(GameWorld::checkKeyInput)); schedule(schedule_selector(GameWorld::onKeyInput), 0.05f); } while(0); return true; }
void GameScene::createGameStateLabels() { //背景 float width = g_visibleSize.width / 1.5; float height = 100; float x = g_visibleSize.width - width; float y = g_visibleSize.height; CCDrawNode* node = CCDrawNode::create(); this->addChild(node); ccColor4F color = ccc4f(0, 0, 0, 0.9); CCPoint verts[] = { ccp(x, y), ccp(x + width, y), ccp(x + width, y - height), ccp(x , y - height) }; node->drawPolygon(verts, 4, color, 0, color); //レベル selectedLevel = UserSettings::getSelectedLevel(); CCString* levelString = CCString::createWithFormat("Level %d", selectedLevel + 1); CCLabelBMFont* label1 = CCLabelBMFont::create(levelString->getCString(), FONT_GREEN); label1->setScale(TEXT_SCALE); label1->setAnchorPoint(CCPointZero); label1->setPosition(g_visibleSize.width * 0.38, g_visibleSize.height - height / 2.15); label1->setTag(kTagLevel); this->addChild(label1); //残りボール数 CCLabelBMFont* label2 = CCLabelBMFont::create("Balls:", FONT_WHITE); label2->setScale(TEXT_SCALE); label2->setAnchorPoint(CCPointZero); label2->setPosition(label1->getPositionX() * 1.7, label1->getPositionY()); label2->setTag(kTagBallRemainLabel); this->addChild(label2); //スコア CCLabelBMFont* label3 = CCLabelBMFont::create("Score:", FONT_WHITE); label3->setScale(TEXT_SCALE); label3->setAnchorPoint(CCPointZero); label3->setPosition(label1->getPositionX(), label2->getPositionY() - label2->getContentSize().height * 0.5); label3->setTag(kTagScoreLabel); this->addChild(label3); //ハイスコア CCLabelBMFont* label4 = CCLabelBMFont::create("HighScore:", FONT_WHITE); label4->setScale(TEXT_SCALE); label4->setAnchorPoint(CCPointZero); label4->setPosition(label2->getPositionX(), label2->getPositionY() - label2->getContentSize().height * 0.5); this->addChild(label4); CCString* highScore = CCString::createWithFormat("%d", g_LevelState[1][selectedLevel]); CCLabelBMFont* label5 = CCLabelBMFont::create(highScore->getCString(), FONT_ORANGE); label5->setScale(TEXT_SCALE); label5->setAnchorPoint(CCPointZero); label5->setPosition( label4->getPositionX() + label4->getContentSize().width * TEXT_SCALE + 10, label4->getPositionY()); this->addChild(label5); showBallRemain(); showScore(); }
bool ResultScene::init() { bool result = GradientLayer::init(); if (result) { setTitle(MessageResultTitle); CCSize windowSize = CCDirector::sharedDirector()->getWinSize(); GameEngine *engine = GameEngine::sharedEngine(); CCSprite *braveImage = CCSprite::createWithSpriteFrameName("brave.png"); braveImage->setRotation(90); braveImage->setPosition(ccp(windowSize.width / 2, windowSize.height - kBraveImageMarginTop - braveImage->getContentSize().height / 2)); this->addChild(braveImage); CCLabelTTF *messageLabel = CCLabelTTF::create(engine->getResultMessage(), DefaultFontName, FontSizeSmall); messageLabel->setPosition(ccp(windowSize.width / 2 + kBraveMessageAdjustX, windowSize.height - kBraveMessageMarginTop)); messageLabel->setAnchorPoint(ccp(0.0, 0.0)); this->addChild(messageLabel); float boxBottom = kCommandAreaHeight + kCommandAreaMarginTop + kCommandAreaMarginBottom; CCDrawNode *boxNode = CCDrawNode::create(); CCPoint contentBox[] = {ccp(kBoxMarginHorizontal, windowSize.height - kBoxMarginTop), ccp(windowSize.width - kBoxMarginHorizontal, windowSize.height - kBoxMarginTop), ccp(windowSize.width - kBoxMarginHorizontal, boxBottom), ccp(kBoxMarginHorizontal, boxBottom)}; boxNode->drawPolygon(contentBox, 4, kBoxFillColor, 1, kBoxBorderColor); this->addChild(boxNode); CCString *scoreText = CCString::createWithFormat("%s:%d", MessageScoreText, engine->getScore()); CCLabelTTF *scoreLabel = CCLabelTTF::create(scoreText->getCString(), DefaultFontName, FontSizeNormal); scoreLabel->setPosition(ccp(windowSize.width / 2, windowSize.height - kScoreLabelMarginTop)); this->addChild(scoreLabel); map<hiberlite::sqlid_t, int> *foundItems = engine->getFoundItems(); map<hiberlite::sqlid_t, int>::iterator foundItemsIterator = foundItems->begin(); vector<Item> *items = engine->getItems(); float itemImageAreaMarginLeft = windowSize.width / 2 - ((kNumberOfLineItems / 2 - 1) * (kItemImageSize + kItemImageMarginHorizontal)) - kItemImageMarginHorizontal / 2 - kItemImageSize / 2; int sumFoundItems = 0; int index = 0; while (foundItemsIterator != foundItems->end()) { hiberlite::sqlid_t itemID = (*foundItemsIterator).first; Item item = items->at(itemID - 1); int count = (*foundItemsIterator).second; const char *imageFileName = item->image.c_str(); int posX = index % kNumberOfLineItems; int posY = index / kNumberOfLineItems; CCPoint imagePosition = ccp(itemImageAreaMarginLeft + (kItemImageSize + kItemImageMarginHorizontal) * posX, windowSize.height - kItemImageAreaMarginTop - (kItemImageSize + kItemImageMarginVertical) * posY); CCSprite *itemImage = CCSprite::createWithSpriteFrameName(imageFileName); itemImage->setPosition(imagePosition); this->addChild(itemImage); sumFoundItems += count; if (count > kItemCountMax) { count = kItemCountMax; } CCString *countText = CCString::createWithFormat("%d", count); CCLabelTTF *countLabel = CCLabelTTF::create(countText->getCString(), DefaultFontName, FontSizeSmall); countLabel->setPosition(ccpAdd(imagePosition, ccp(0, -kItemImageSize / 2 - kItemImageCountLabelAdjustY))); this->addChild(countLabel); foundItemsIterator++; index++; } CCString *itemCountText = CCString::createWithFormat("%s:%d", MessageNumberOfFoundItemsText, sumFoundItems); CCLabelTTF *itemCountLabel = CCLabelTTF::create(itemCountText->getCString(), DefaultFontName, FontSizeNormal); itemCountLabel->setPosition(ccp(windowSize.width / 2, windowSize.height - kItemCountLabelMarginTop)); this->addChild(itemCountLabel); CCLayerColor *retryLayer = CCLayerColor::create((ccColor4B){0x00, 0x00, 0x00, 0x00}, kCommandButtonWidth, kCommandButtonHeight); CCLabelTTF *retryLabel = CCLabelTTF::create(MessageRetryButtonTitle, DefaultFontName, FontSizeNormal); retryLabel->setPosition(ccp(kCommandButtonWidth / 2, kCommandButtonHeight / 2)); retryLayer->addChild(retryLabel); CCMenuItem *retryItem = CCMenuItemLabel::create(retryLayer, GameEngine::sharedEngine(), menu_selector(GameEngine::startNewGame)); CCLayerColor *backTitleLayer = CCLayerColor::create((ccColor4B){0x00, 0x00, 0x00, 0x00}, kCommandButtonWidth, kCommandButtonHeight); CCLabelTTF *backTitleLabel = CCLabelTTF::create(MessageBackTitleButtonTitle, DefaultFontName, FontSizeNormal); backTitleLabel->setPosition(ccp(kCommandButtonWidth / 2, kCommandButtonHeight / 2)); backTitleLayer->addChild(backTitleLabel); CCMenuItem *backTitleItem = CCMenuItemLabel::create(backTitleLayer, GameEngine::sharedEngine(), menu_selector(GameEngine::showTitle)); CCMenu *menu = CCMenu::create(retryItem, backTitleItem, NULL); menu->alignItemsHorizontallyWithPadding(kCommandButtonPadding); menu->setPosition(ccp(windowSize.width / 2, kCommandAreaMarginBottom + kCommandAreaHeight / 2)); this->addChild(menu); } return result; }
bool ImageEditLayer::init(){ if (DialogLayer::init()) { this->setTitle("头像编辑"); CCPoint middle=ccp(286, 154); CCSize size=CCSizeMake(570,300); CCClippingNode* clippingNode = CCClippingNode::create(); //设置裁剪区域大小 clippingNode->setContentSize(size); clippingNode->setAnchorPoint(ccp(0.5, 0.5)); clippingNode->setPosition(middle); m_contentLayer->addChild(clippingNode); CCTexture2D* textrue=new CCTexture2D(); textrue->autorelease(); textrue->initWithImage(_image); _sprite=CCSprite::createWithTexture(textrue); _initPoint=ccp(clippingNode->getContentSize().width/2, clippingNode->getContentSize().height/2); _sprite->setPosition(_initPoint); CCSize spriteSize=_sprite->getContentSize(); if (size.width/spriteSize.width<size.height/spriteSize.height) { _scale=size.width/spriteSize.width; }else{ _scale=size.height/spriteSize.height; } _sprite->setScale(_scale); clippingNode->addChild(_sprite); //创建裁剪模板,裁剪节点将按照这个模板来裁剪区域 CCDrawNode *stencil = CCDrawNode::create(); CCPoint rectangle[4]; rectangle[0] = ccp(0, 0); rectangle[1] = ccp(clippingNode->getContentSize().width, 0); rectangle[2] = ccp(clippingNode->getContentSize().width, clippingNode->getContentSize().height); rectangle[3] = ccp(0, clippingNode->getContentSize().height); ccColor4F white = {1, 1, 1, 1}; //画一个多边形 这画一个200x200的矩形作为模板 stencil->drawPolygon(rectangle, 4, white, 1, white); clippingNode->setStencil(stencil); //用来设置显示裁剪区域还是非裁剪区域的 clippingNode->setInverted(false);//在裁剪区域内显示加入的内容 _clip=CCClippingNode::create();//创建裁剪节点,成员变量 _clip->setInverted(true);//设置底板可见 _clip->setAlphaThreshold(0.0f);//设置alpha为0 m_contentLayer->addChild(_clip);//添加裁剪节点 _mask=CCLayerColor::create(ccc4(0,0,0,160),size.width,size.height); CCPoint point=ccp(middle.x-size.width/2,middle.y-size.height/2); _mask->setPosition(point); _clip->addChild(_mask);//为裁剪节点添加一个黑色带透明(看起了是灰色)的底板 _stencil=CCSprite::create("default_avatar.png");//使用头像原图作为模板 _stencil->setPosition(middle); _stencil->setScale(STENCIL_SCALE); _clip->setStencil(_stencil);//设置模版 CCSprite* queding=CCSprite::createWithSpriteFrameName("touxiang_queding.png"); CCMenuItemSprite* item=CCMenuItemSprite::create(queding, queding, this, menu_selector(ImageEditLayer::menuCallback)); item->setPosition(ccp(290,-30));//注意contentlayer坐标原点位置 item->setScale(0.7); item->setTag(kTagConfirm); this->addMenuItem(item,true); CCSprite* fanhui=CCSprite::createWithSpriteFrameName("touxiang_fanhui.png"); fanhui->setScale(0.87); CCMenuItemSprite* fanhuiItem=CCMenuItemSprite::create(fanhui, fanhui, this, menu_selector(ImageEditLayer::menuCallback)); fanhuiItem->setPosition(ccp(19,368)); fanhuiItem->setTag(kTagBack); this->addMenuItem(fanhuiItem,true); CCPoint contentPoint=m_contentLayer->getPosition(); _rect=CCRectMake(contentPoint.x+point.x, contentPoint.y+point.y, size.width,size.height); return true; } return false; }