void GuessSpellItem::onFlipFinished(CCNode *node,void *data) { CardSprite *card = (CardSprite*)node; card->setVertexZ(0); if(m_won) { const std::vector<std::string> &props = ResourceMgr::getInstance()->propsNames(); std::string anim,first; anim = props[m_selectNum]; anim += "exec"; first = anim; ResourceMgr::getInstance()->getFrameSpriteFirstFrame(first); m_anim = new BasAnimSprite(anim,first); if(m_anim->isLoaded()) { m_anim->setAnimFinishCB(this,callfuncND_selector(GuessSpellItem::onAnimFinished)); CCSprite *front = card->frontSprite(); CCSize size = front->getContentSize(); front->addChild(m_anim); m_anim->setAnchorPoint(ccp(0.5,0.5)); m_anim->setPosition(ccp(size.width/2,size.height/2)); m_anim->playInLoop(1); CCSize animSize = m_anim->getContentSize(); m_anim->setScale(size.width / animSize.width); m_won = false; } else { delete m_anim; m_anim = 0; } } }
void HelloWorld::okNetworks() { //uodate the score to the server //flip the poker card CardSprite *pcardRight = (CardSprite*)this->getChildByTag(RIGHTPOKERTAG); CardSprite *pcardLeft = (CardSprite*)this->getChildByTag(LEFTPOKERTAG); pcardRight->openCard(); pcardLeft->openCard(); //find the winer and update score char *temp = new char[64]; if (_myNumber > _compuerNumber) { sprintf(temp, "%d",_myNumber); std::string myscore = temp; cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest(); std::string str1 = "http://localhost/gen.php?"; std::string str2 = str1+"password="******"ok"); cocos2d::extension::CCHttpClient::getInstance()->send(request); request->release(); } }
void GuessSpellItem::closeAllCard() { for(unsigned int i = 0; i < m_cards.size(); i++) { CardSprite *card = (CardSprite*)m_cards[i]->getNode(); if(!card->isClosed()) { card->setVertexZ(m_anchorWidth / 10); card->closeCard(); } } }
CardSprite* CardSprite::create(GameScene *gameScene, int level) { CardSprite *pSprite = new CardSprite(); if (pSprite && pSprite->init(gameScene, level)) { pSprite->autorelease(); return pSprite; } CC_SAFE_DELETE(pSprite); return NULL; }
CardSprite* CardSprite::create(int value, const CCSize& size) { CardSprite* cardSprite = new CardSprite(); if (cardSprite && cardSprite->init(value, size)) { cardSprite->autorelease(); return cardSprite; } CC_SAFE_DELETE(cardSprite); return NULL; }
CardSprite* CardSprite::createWithFrame(CCSpriteFrame *pSpriteFrame) { CardSprite *sprite = new CardSprite(); if (sprite&&sprite->initWithSpriteFrame(pSpriteFrame)) { sprite->autorelease(); return sprite; } return NULL; }
CardSprite* CardSprite::createCardSprite(int number, int wight, int height, float CardSpriteX, float CardSpriteY) { //这里是创建一个自定义精灵类,格式很规范化 CardSprite *enemy = new CardSprite(); if (enemy && enemy->init()) { enemy->autorelease(); enemy->enemyInit(number,wight,height,CardSpriteX,CardSpriteY); return enemy; } CC_SAFE_DELETE(enemy); return NULL; }
CardSprite* CardSprite::createCardSprite(int number, int wight, int height, Point position) { //这里是创建一个自定义精灵类,格式很规范化 CardSprite *card = new CardSprite(); if (card && card->init()) { card->autorelease(); card->initCard(number,wight,height,position); return card; } CC_SAFE_DELETE(card); return NULL; }
void TLMienNam::createCards(PositionIndex positionIndex, int tag){ Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); double cardScale = this->cardScale(); auto card = this->getCard(); CardSprite* cardSprite = CardSprite::create(card, positionIndex); Size cardSize = Size(cardSprite->getContentSize().width * cardScale, cardSprite->getContentSize().height * cardScale); cardSprite->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y + visibleSize.height/2)); cardSprite->setScaleX(cardScale); cardSprite->setScaleY(cardScale); cardSprite->setTag(tag); auto delay = DelayTime::create(1.5f); MoveTo* moveTo; moveTo = MoveTo::create(0.5f, Vec2(origin.x + visibleSize.width/2 + (positionIndex.x-6) * cardSize.width/3, origin.y + 50 +cardSize.height/2 )); auto sequence = Sequence::create(delay,moveTo, NULL); cardSprite->runAction(sequence); this->card_tag.push_back(cardSprite); }
void GuessSpellItem::onCardClicked(CCNode *node,void *data) { if(!m_isGuessing) return; SoundMgr::getInstance()->playEffect(SoundEnum::BTN_EFFECT_NORMAL); FSizeCCNodeDelegate *click = dynamic_cast<FSizeCCNodeDelegate*>(node); if(click == 0) return; CardSprite *card = (CardSprite*)click->getNode(); card->setVertexZ(m_anchorWidth / 10); card->openCard(); int num = click->getTag(); if(num == m_selectNum) { //success m_won = true; sendGift(); } else { //failed m_won = false; startTimePool(); } m_isGuessing = false; }
// on "init" you need to initialize your instance bool Game::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } loadBg(); loadCards(); CCSize size = CCDirector::sharedDirector()->getWinSize(); char name[20]; CCSpriteFrameCache* cache =CCSpriteFrameCache::sharedSpriteFrameCache(); CCArray * sArr = CCArray::create(); for(int i=1;i<14;i++){ sprintf(name,"FP%d.BMP",i); CCSpriteFrame* frame =cache->spriteFrameByName(name); CardSprite* sprite =CardSprite::createWithFrame(frame); sprite->setPosition(ccp(-50,-50)); sprite->setTag(i); sArr->addObject(sprite); addChild(sprite); } CCObject* pObj; int i=1; CCARRAY_FOREACH(sArr, pObj) { CardSprite* cs = (CardSprite*)pObj; CCActionInterval *actionTo = CCMoveTo::create(4.0f, ccp(140+i*25,size.height/4)); cs->runAction(actionTo); i++; }
void GuessSpellItem::init(int dummy) { if(m_itemInited) return; float srate = CCDirector::sharedDirector()->getWinSize().height / 1280.0; if(1) { float height = m_anchorHeight * 0.75; float width = m_anchorWidth / 5; const std::vector<std::string> props = ResourceMgr::getInstance()->propsNames(); m_selectNum = rand() % props.size(); std::vector<int> runNums; runNums.push_back(rand() % props.size()); runNums.push_back(rand() % props.size()); runNums.push_back(rand() % props.size()); runNums[rand() % runNums.size()] = m_selectNum; if(1) { //selected spell position CCSprite *posSprite = CCSprite::createWithSpriteFrameName("guessspellpos.png"); CCSize posSize = posSprite->getContentSize(); m_posDele = new BasNodeDelegateWidget(posSprite,CCSizeMake(posSize.width * srate,posSize.height * srate)); this->addChild(m_posDele); m_posDele->setTop("parent",uilib::Top); m_posDele->setLeft("parent",uilib::Left); m_posDele->setTopMargin(25); m_posDele->setLeftMargin(25); std::string icon = props[m_selectNum]; icon += ".png"; m_selIcon = CCSprite::createWithSpriteFrameName(icon.data()); m_selDele = new FSizeCCNodeDelegate(m_selIcon,CCSizeMake(60,60)); m_selDele->setCanTouch(true); m_selDele->setClickCB(this,callfuncND_selector(GuessSpellItem::onSpellClicked)); m_posDele->addChild(m_selDele); m_selDele->setCenterIn("parent"); } for(unsigned int i = 0; i < runNums.size(); i++) { CardSprite *card = new CardSprite(CCSizeMake(width,height),0.5); card->setFlipListener(this,callfuncND_selector(GuessSpellItem::onFlipFinished)); card->setCards("cardfg.png","cardbg.png"); FSizeCCNodeDelegate *dele = new FSizeCCNodeDelegate(card,CCSizeMake(width,height)); dele->setCanTouch(true); dele->setTag(runNums[i]); dele->setClickCB(this,callfuncND_selector(GuessSpellItem::onCardClicked)); m_cards.push_back(dele); this->addChild(dele); } FSizeCCNodeDelegate *mid = m_cards[1]; FSizeCCNodeDelegate *left = m_cards[0]; FSizeCCNodeDelegate *right = m_cards[2]; mid->setCenterIn("parent"); left->setRight(mid->getName(),uilib::Left); left->setRightMargin(10); left->setVertical("parent",0.5); right->setLeft(mid->getName(),uilib::Right); right->setLeftMargin(10); right->setVertical("parent",0.5); m_isGuessing = true; } if(1) { //count downer std::stringstream ss; ss << "Time:" << m_interval; m_countDowner = CCLabelBMFont::create(ss.str().data(),"fonts/uifont24.fnt"); m_countDowner->setVisible(false); FSizeCCNodeDelegate *dele = new FSizeCCNodeDelegate(m_countDowner); this->addChild(dele); dele->setTop(m_posDele->getName(),uilib::Bottom); dele->setTopMargin(10); dele->setHorizontal(m_posDele->getName(),0.5); } m_itemInited = true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setTouchEnabled(true); ///////////////////////////// // 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 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition(MENU_POSITION_CLOSE ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); // create gen menu CCMenuItemImage *ptestItem1 = CCMenuItemImage::create("gen.png", "gen.png",this,menu_selector(HelloWorld::menuOfGen)); ptestItem1->setPosition(MENU_POSITION_GEN); CCMenu *ptestMenu1 = CCMenu::create(ptestItem1,NULL); ptestMenu1->setPosition(CCPointZero); this->addChild(ptestMenu1,5); ///////////////////////////// // 3. add your codes below... // create label of pokers _pRightPoker = CCLabelAtlas::create("0123456789", "bignumber.png", 36, 80, '0') ; _pLeftPoker = CCLabelAtlas::create("0123456789", "bignumber.png", 36, 80, '0') ; _pRightPoker->setPosition(LABEL_POSITION_RIGHTPOKER); _pLeftPoker->setPosition( LABEL_POSITION_LEFTPOKER); _pLeftPoker->setString(""); _pRightPoker->setString(""); this->addChild(_pRightPoker,10); this->addChild(_pLeftPoker,11); //create label of Wins _pWins = CCLabelTTF::create("0","Marker Felt",50); _pWins->setPosition(LABEL_POSITION_WINS); this->addChild(_pWins); //create label of mynumber _pMyNumberLabel = CCLabelAtlas::create("0123456789", "scorenumber.png", 34, 60, '0') ; _pMyNumberLabel->setPosition( LABEL_POSITION_MYNUMBER); _pMyNumberLabel->setString("98"); this->addChild(_pMyNumberLabel, 1); // add my number sprite CCSprite *pScore = CCSprite::create("scoreframe.png"); pScore->setPosition(SPRITE_POSITION_SCORE); pScore->setTag(SCORETAG); this->addChild(pScore); // add two poker sprites CardSprite *pPokerLeftSide = CardSprite::create("poker.png","poker.png",1,callfuncN_selector(HelloWorld::flipLeftCard),this); CardSprite *pPokerRightSide = CardSprite::create("poker.png","poker.png",1,callfuncN_selector(HelloWorld::flipRightCard),this); pPokerLeftSide->setPosition(SPRITE_POSITION_LEFTPOKER); pPokerRightSide->setPosition(SPRITE_POSITION_RIGHTPOKER); pPokerLeftSide->setTag(LEFTPOKERTAG); pPokerRightSide->setTag(RIGHTPOKERTAG); this->addChild(pPokerRightSide, 1); this->addChild(pPokerLeftSide, 2); // add get and ok item for action CCSprite *getItem = CCSprite::create("get.png"); getItem->setPosition(SPRITE_POSITION_GET); getItem->setTag(GETTAG); CCSprite *okItem = CCSprite::create("ok.png"); okItem->setPosition(SPRITE_POSITION_OK); okItem->setTag(OKTAG); this->addChild(getItem); this->addChild(okItem); return true; }
// Animation loop void DisplayIntro::loop() { int no = mCards.size(); // Catch no card error if (no<1) return; // Retrieve theme data KConfigGroup cardconfig = thememanager()->config(QLatin1String( "card" )); double card_width = cardconfig.readEntry("width", 1.0); KConfigGroup config = thememanager()->config(id()); QPointF start_shift = config.readEntry("start-shift", QPointF(1.0,1.0)); QPointF start_pos = config.readEntry("start-pos", QPointF(1.0,1.0)); double time_clear_in = config.readEntry("time-clear-in", 1.0); double time_clear_out = config.readEntry("time-clear-out", 1.0); double aspectRatio = thememanager()->aspectRatio(); // Display the intro text delayed if (mAnimCnt == 2 && mState == Putting && !mTextShown) { mTextShown = true; QString s1 = i18nc("Title of the game - line 1", "Lieutenant Skat"); QString s2 = i18nc("Title of the game - line 2", "for"); QString s3 = i18nc("Title of the game - line 3", "K D E"); // Text sprite title foreground TextSprite* text1a = new TextSprite(s1, QLatin1String( "name-front" ), mTheme, scene()); mSprites.append(text1a); text1a->show(); // Text sprite title background TextSprite* text1b = new TextSprite(s1, QLatin1String( "name-back" ), mTheme, scene()); mSprites.append(text1b); text1b->show(); // Text sprite title foreground TextSprite* text2a = new TextSprite(s2, QLatin1String( "for-front" ), mTheme, scene()); mSprites.append(text2a); text2a->show(); // Text sprite title background TextSprite* text2b = new TextSprite(s2, QLatin1String( "for-back" ), mTheme, scene()); mSprites.append(text2b); text2b->show(); // Text sprite title foreground TextSprite* text3a = new TextSprite(s3, QLatin1String( "kde-front" ), mTheme, scene()); mSprites.append(text3a); text3a->show(); // Text sprite title background TextSprite* text3b = new TextSprite(s3, QLatin1String( "kde-back" ), mTheme, scene()); mSprites.append(text3b); text3b->show(); } // Display a card if (mAnimCnt < no && mState == Putting) { double factor = double(mAnimCnt)/double(no-1); double fsin = sin(factor*M_PI); CardSprite* sprite = mCards[mAnimCnt]; QPointF pos; if (mAnimCnt %2 == 0) { pos = QPointF(start_pos.x(), start_pos.y()); pos += QPointF(start_shift.x() * fsin, start_shift.y() * factor); } else { pos = QPointF(1.0-start_pos.x()-card_width, start_pos.y()); pos += QPointF(-start_shift.x() * fsin, start_shift.y() * factor); } sprite->setBackside(); sprite->setPosition(pos); sprite->setZValue(50+mAnimCnt); sprite->show(); mAnimCnt++; } // Change state to turning else if (mState == Putting) { mState = Turning; mAnimCnt = 0; } // Turn cards else if (mAnimCnt < no && mState == Turning) { CardSprite* sprite = mCards[mAnimCnt]; sprite->setTurning(true); mAnimCnt++; } // Change state to waiting else if (mState == Turning) { mState = Waiting; mAnimCnt = 0; } // Wait else if (mAnimCnt < WAIT_CNT && mState == Waiting) { mAnimCnt++; } // Change state to clearing the board else if (mState == Waiting) { mState = Clearing; mAnimCnt = 0; } // Clear the board, step 1 else if (mAnimCnt == 0 && mState == Clearing) { for (int i=0; i<no; i++) { CardSprite* sprite = mCards[i]; sprite->setMove(QPointF((1.0-card_width)/2.0, (1.0/aspectRatio-card_width)/2.0), time_clear_in); } mAnimCnt++; } // Clear the board, step 2 else if (mAnimCnt < 30 && mState == Clearing) { mAnimCnt++; } // Clear the board, step 3 and change state to waiting else if (mState == Clearing) { for (int i=0; i<no; i++) { double r = 1.0; double x = r*cos(double(i)/double(no-1)*M_PI*2.0) + 0.5; double y = r*sin(double(i)/double(no-1)*M_PI*2.0) + 0.5; CardSprite* sprite = mCards[i]; sprite->setMove(QPointF(x,y/aspectRatio), time_clear_out); } mState = Waiting2; mAnimCnt = 0; } // Wait else if (mAnimCnt < WAIT_CNT && mState == Waiting2) { mAnimCnt++; } // Restart cycle else if (mState == Waiting2) { for (int i=0; i<no; i++) { CardSprite* sprite = mCards[i]; sprite->stop(); } mState = Putting; mAnimCnt = 0; } }