bool OptionLayer::init() { if (!CCLayer::init()) { return false; } _back = CCSprite::create("back.png"); _back->setPosition(ccp(24 + 40, 800 - 56.5 + 15)); this->addChild(_back); CCSprite * mode = CCSprite::create("mode.png"); mode->setPosition(ccp(132 + 111, 800 - 103 - 19.5)); this->addChild(mode); CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("dotted_line.png"); int dottedLineY[3] = {313, 420, 539}; for (int i = 0; i < 3; i++) { CCSprite * dotLine = CCSprite::createWithTexture(batchNode->getTexture()); dotLine->setPosition(ccp(27 + 210.5, 800 - dottedLineY[i] - 1.5)); this->addChild(dotLine); } _switchIndex = -1; _switches[0] = &Settings::showConflictedDigit; _switches[1] = &Settings::musicEnabled; _switches[2] = &Settings::soundEnabled; _switches[3] = &Settings::preventSleeping; _switches[4] = &Settings::showTimer; _switches[5] = &Settings::autoHighLight; _switches[6] = &Settings::autoRemoveNotes; _switches[7] = &Settings::removeCompleteDigits; CCSpriteBatchNode* maskBatchNode = CCSpriteBatchNode::create("mask.png"); CCSpriteBatchNode* switchBatch = CCSpriteBatchNode::create("switch.png"); _switchButtons = CCArray::createWithCapacity(7); _switchButtons->retain(); char opLabel[10]; int opLabelY[8] = {271, 332, 379, 445, 492, 566, 612, 657}; for(int i = 0; i < 8; i++) { sprintf(opLabel, "op%d.png", i); CCSprite * label = CCSprite::create(opLabel); label->setPosition(ccp(25 + label->boundingBox().size.width / 2, 800 - opLabelY[i] - label->boundingBox().size.height / 2)); this->addChild(label); CCSprite * stencil = CCSprite::createWithTexture(maskBatchNode->getTexture()); stencil->setPosition(ccp(380 + 39, 800 - opLabelY[i] - label->boundingBox().size.height / 2)); CCClippingNode * cliper = CCClippingNode::create(stencil); cliper->setAlphaThreshold(0); CCSprite * switchButton = CCSprite::createWithTexture(switchBatch->getTexture()); switchButton->setPosition(ccp(*(_switches[i]) ? 394 + 49 : 394, 800 - opLabelY[i] - label->boundingBox().size.height / 2)); _switchButtons->addObject(switchButton); cliper->addChild(switchButton); this->addChild(cliper); } this->setTouchEnabled(true); this->setKeypadEnabled(true); return true; }
NS_CC_EXT_BEGIN bool CCControlColourPicker::init() { if (CCControl::init()) { setTouchEnabled(true); // Cache the sprites CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("extensions/CCControlColourPickerSpriteSheet.plist"); // Create the sprite batch node CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("extensions/CCControlColourPickerSpriteSheet.png"); addChild(spriteSheet); // MIPMAP ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; spriteSheet->getTexture()->setAliasTexParameters(); spriteSheet->getTexture()->setTexParameters(¶ms); spriteSheet->getTexture()->generateMipmap(); // Init default color m_hsv.h = 0; m_hsv.s = 0; m_hsv.v = 0; // Add image m_background=CCControlUtils::addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, CCPointZero, ccp(0.5f, 0.5f)); CCPoint backgroundPointZero = ccpSub(m_background->getPosition(), ccp (m_background->getContentSize().width / 2, m_background->getContentSize().height / 2)); // Setup panels . currently hard-coded... float hueShift = 8; float colourShift = 28; m_huePicker=CCControlHuePicker::create(spriteSheet, ccp(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift)); m_colourPicker=CCControlSaturationBrightnessPicker::create(spriteSheet, ccp(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift)); // Setup events m_huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::hueSliderValueChanged), CCControlEventValueChanged); m_colourPicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::colourSliderValueChanged), CCControlEventValueChanged); // Set defaults updateHueAndControlPicker(); addChild(m_huePicker); addChild(m_colourPicker); // Set content size setContentSize(m_background->getContentSize()); return true; } else return false; }
bool Recipe30::init() { if ( !RecipeBase::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSpriteBatchNode* pBatchNode = CCSpriteBatchNode::create("recipe30_uhhoi.png"); this->addChild(pBatchNode); for (int i=0; i<300; i++) { CCSprite* sprite = CCSprite::createWithTexture(pBatchNode->getTexture()); float x = CCRANDOM_0_1()*size.width; float y = CCRANDOM_0_1()*size.height; sprite->setPosition(ccp(x, y)); pBatchNode->addChild(sprite); CCFadeOut* fade = CCFadeOut::create(CCRANDOM_0_1()*10.0f); sprite->runAction(fade); } return true; }
void SpriteBatchNodeTestPage::loadUI() { setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); auto winSize = CocosWindow::size(); auto origin = CocosWindow::origin(); const std::string pic("Images/grossini_dance_01.png"); size_t num(1000); /*for (size_t i=0; i<num; ++i) { auto dance = CCSprite::create(pic.c_str()); CCPoint pos; pos.x = winSize.width * CCRANDOM_0_1(); pos.y = winSize.height * CCRANDOM_0_1(); dance->setPosition(pos); addChildRaw(dance); }*/ CCSpriteBatchNode *batch = CCSpriteBatchNode::create(pic.c_str()); for (size_t i=0; i<num; ++i) { auto dance = CCSprite::createWithTexture(batch->getTexture()); CCPoint pos; pos.x = winSize.width * CCRANDOM_0_1(); pos.y = winSize.height * CCRANDOM_0_1(); dance->setPosition(pos); batch->addChild(dance); } ADD_CHILD(batch); }
HelloWorld::HelloWorld() { setTouchEnabled( true ); setAccelerometerEnabled( true ); setKeypadEnabled( true ); CCSize s = CCDirector::sharedDirector()->getWinSize(); // init physics this->initPhysics(); CCSpriteBatchNode *parent = CCSpriteBatchNode::create("blocks.png", 100); m_pSpriteTexture = parent->getTexture(); addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(ccp(s.width/2, s.height/2)); CCLabelTTF *label = CCLabelTTF::create("Tap screen", "Marker Felt", 32); addChild(label, 0); label->setColor(ccc3(0,0,255)); label->setPosition(ccp( s.width/2, s.height-50)); scheduleUpdate(); }
Box2DTestLayer::Box2DTestLayer() : m_pSpriteTexture(NULL) { setTouchEnabled( true ); setAccelerometerEnabled( true ); // init physics this->initPhysics(); // create reset button this->createResetButton(); //Set up sprite #if 1 // Use batch node. Faster CCSpriteBatchNode *parent = CCSpriteBatchNode::create("Images/blocks.png", 100); m_pSpriteTexture = parent->getTexture(); #else // doesn't use batch node. Slower m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("Images/blocks.png"); CCNode *parent = CCNode::create(); #endif addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(VisibleRect::center()); CCLabelTTF *label = CCLabelTTF::create("Tap screen", "Marker Felt", 32); addChild(label, 0); label->setColor(ccc3(0,0,255)); label->setPosition(ccp( VisibleRect::center().x, VisibleRect::top().y-50)); scheduleUpdate(); }
bool T08CCSpriteBatchNode::init() { if (!BaseLayer::init()) return false; CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCPoint ptCenter = ccp(winSize.width / 2, winSize.height / 2); /* CCSpriteBatchNode是一个容器,只能包容CCSprite对象,要求这些精灵来自同一个纹理 */ CCSpriteBatchNode* pBatch = CCSpriteBatchNode::create("CloseNormal.png"); addChild(pBatch); pBatch->setTag(10); CCSprite *pSprite = CCSprite::createWithTexture(pBatch->getTexture()); pBatch->addChild(pSprite); pSprite->setPosition(ptCenter); setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); return true; }
void Box2DTestLayer::addNewSpriteWithCoords(CCPoint p) { //UXLOG(L"Add sprite %0.2f x %02.f",p.x,p.y); CCSpriteBatchNode* batch = (CCSpriteBatchNode*)getChildByTag(kTagSpriteManager); //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just randomly picking one of the images int idx = (CCRANDOM_0_1() > .5 ? 0:1); int idy = (CCRANDOM_0_1() > .5 ? 0:1); CCSprite *sprite = CCSprite::spriteWithTexture(batch->getTexture(), CCRectMake(32 * idx,32 * idy,32,32)); batch->addChild(sprite); sprite->setPosition( CCPointMake( p.x, p.y) ); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = sprite; b2Body *body = world->CreateBody(&bodyDef); // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); }
//------------------------------------------------------------------ // // TMXOrthoTest // //------------------------------------------------------------------ TMXOrthoTest::TMXOrthoTest() { // // Test orthogonal with 3d camera and anti-alias textures // // it should not flicker. No artifacts should appear // //CCLayerColor* color = CCLayerColor::create( ccc4(64,64,64,255) ); //addChild(color, -1); CCTMXTiledMap* map = CCTMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); CCSize s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); CCArray * pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; CCObject* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = (CCSpriteBatchNode*)pObject; if(!child) break; child->getTexture()->setAntiAliasTexParameters(); }
HelloWorld::HelloWorld() { setTouchEnabled( true ); setAccelerometerEnabled( true ); CCSize s = CCDirector::sharedDirector()->getWinSize(); // init physics this->initPhysics(); CCSpriteBatchNode *parent = CCSpriteBatchNode::create("blocks.png", 100); m_pSpriteTexture = parent->getTexture(); addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(ccp(s.width/2, s.height/2)); CCLabelTTF *label = CCLabelTTF::create("Tap screen", "Marker Felt", 32); addChild(label, 0); label->setColor(ccc3(0,0,255)); label->setPosition(ccp( s.width/2, s.height-50)); // 增加一个sprite CCSprite *aSprite = CCSprite::create("an1_anim1.png"); aSprite->setPosition(ccp(s.width/2, s.height-50)); addChild(aSprite,0); // 移动sprite CCPoint target = ccp(100, 100); this->runAction(CCSequence::create( CCMoveTo::create(2 ,target), // CCCallFunc::create(this, callfunc_selector(Player::removeTarget)) NULL)); scheduleUpdate(); }
void GameLayer::updateNote(int x, int y) { int totalNote = Sudoku::notes[x][y].total(); int k = 0, offset = totalNote * (totalNote - 1) / 2; for(int i = 0; i < 9; i++) { if(Sudoku::notes[x][y].has(i+1)) { if(!_notes[x][y][i]) { CCSpriteBatchNode * batch = (CCSpriteBatchNode*)_numNoteBatch->objectAtIndex(i); CCSprite * littleNote = CCSprite::createWithTexture(batch->getTexture()); littleNote->setZOrder(2); _notes[x][y][i] = littleNote; this->addChild(littleNote); } // position it _notes[x][y][i]->setPosition(ccp( NOTE_NUM_RX[offset + k] + 26 + x * 49 + x / 3 * 3, -NOTE_NUM_RY[offset + k] + 800 - 141 - y * 49 - y / 3 * 3)); CCLog("total note: %d", totalNote); _notes[x][y][i]->setScale(SCALE[totalNote - 1]); CCLog("here"); k++; } else { if(_notes[x][y][i]) { this->removeChild(_notes[x][y][i]); _notes[x][y][i] = NULL; } } } }
JS_CLASS_METHOD(JsSpriteBatchNodeBinding,GetTexture) { if (argc == 0) { JSObject *thisObj = JS_THIS_OBJECT(context,vp); CCSpriteBatchNode *pBatchNode = static_cast<CCSpriteBatchNode*>(JS_GetPrivate(context,thisObj)); if (pBatchNode) { JSObject *newObj = JS_NewObject(context, &JsTexture2DBinding::clz, JsTexture2DBinding::obj, NULL); JS_SetPrivate(context,newObj, pBatchNode->getTexture()); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObj)); } } return JS_TRUE; }
ChipmunkTestLayer::ChipmunkTestLayer() { #if CC_ENABLE_CHIPMUNK_INTEGRATION // enable events setTouchEnabled(true); setAccelerometerEnabled(true); // title CCLabelTTF *label = CCLabelTTF::create("Multi touch the screen", "Marker Felt", 36); label->setPosition(ccp( VisibleRect::center().x, VisibleRect::top().y - 30)); this->addChild(label, -1); // reset button createResetButton(); // init physics initPhysics(); #if 1 // Use batch node. Faster CCSpriteBatchNode *parent = CCSpriteBatchNode::create("Images/grossini_dance_atlas.png", 100); m_pSpriteTexture = parent->getTexture(); #else // doesn't use batch node. Slower m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("Images/grossini_dance_atlas.png"); CCNode *parent = CCNode::create(); #endif addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(ccp(200,200)); // menu for debug layer CCMenuItemFont::setFontSize(18); CCMenuItemFont *item = CCMenuItemFont::create("Toggle debug", this, menu_selector(ChipmunkTestLayer::toggleDebugCallback)); CCMenu *menu = CCMenu::create(item, NULL); this->addChild(menu); menu->setPosition(ccp(VisibleRect::right().x-100, VisibleRect::top().y-60)); scheduleUpdate(); #else CCLabelTTF *pLabel = CCLabelTTF::create("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case", "Arial", 18); CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width/2, size.height/2)); addChild(pLabel); #endif }
void MPMapLayer::antiAliasMap(CCTMXTiledMap * map) { CCArray * pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; CCObject* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = (CCSpriteBatchNode*)pObject; if(!child) break; child->getTexture()->setAntiAliasTexParameters(); }
void GameLayer::_initPlatform() { CCRect rect; switch((int)CCRANDOM_0_1() * 2) { case 0: rect = CCRectMake(608, 64, 102, 36); break; case 1: rect = CCRectMake(608, 128, 90, 32); break; } CCSpriteBatchNode* batchNode = dynamic_cast<CCSpriteBatchNode*>(getChildByTag(kSpriteManager)); CCSprite* platform = CCSprite::createWithTexture(batchNode->getTexture(), rect); batchNode->addChild(platform, 3, currentPlatformTag); }
void LevelLoader::enableTileGrid(CCTMXTiledMap *map) { //디버깅용 타일 경계선 그리기 // All the tiles by default will be aliased. If you want to create anti-alias tiles, you should do: // iterate over all the "layers" (atlas sprite managers) // and set them as 'antialias' CCArray * pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; CCObject* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = (CCSpriteBatchNode*)pObject; if(!child) { break; } child->getTexture()->setAntiAliasTexParameters(); }
void Main::batching(const char* listName, const char* atlasName, const char* spriteName){ CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile(listName, atlasName); CCTexture2D* texture2D = CCTextureCache::sharedTextureCache()->addImage(spriteName); CCSpriteBatchNode* batchNode = CCSpriteBatchNode::createWithTexture(texture2D); this->addChild(batchNode, 0); for(int i = 0; i < 10; i++){ CCSprite* sprite = CCSprite::createWithTexture(batchNode->getTexture()); sprite->setPosition(ccp( rand() % (int)visibleSize.width, rand() % (int)visibleSize.height )); this->addChild(sprite); } }
bool layer_background::init() { if( !CCLayer::init() ) { return false; } bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); s = CCDirector::sharedDirector()->getWinSize(); CCSpriteBatchNode *batch = CCSpriteBatchNode::create("Space2.png"); m_map1 = CCSprite::createWithTexture(batch->getTexture()); m_map2 = CCSprite::createWithTexture(batch->getTexture()); m_map1->setAnchorPoint(ccp(0,0)); m_map2->setAnchorPoint(ccp(0,0)); // m_map1->setScaleX(0.95); // m_map1->setScaleY(1); // m_map2->setScaleX(0.95); // m_map2->setScaleY(1); m_map1->setPosition(ccp(0,0)); m_map2->setPosition(ccp(0,SCREEN_HIGH));//这里注意,我们把第二章地图的位置放在场景的最上方...因为地图是要往下走的..值根据屏幕高度 this->addChild(m_map1,0,1); this->addChild(m_map2,0,2); scheduleUpdate();//启动Update更新 bRet = true; } while (0); // CCSprite *s = CCSprite::create("Space2.png"); // this->addChild(s,0,1); // return true; return bRet; }
void HelloWorld::addPhysicSprite() { #if CC_ENABLE_CHIPMUNK_INTEGRATION // Use batch node. Faster CCSpriteBatchNode *parent = CCSpriteBatchNode::create(s_SpinPea, 100); m_pSpriteTexture = parent->getTexture(); addChild(parent, 100, kTagParentNode); CCPoint pos = ccp(200,200); int posx, posy; CCNode *parent = getChildByTag(kTagParentNode); posx = CCRANDOM_0_1() * 200.0f; posy = CCRANDOM_0_1() * 200.0f; posx = (posx % 4) * 85; posy = (posy % 3) * 121; int num = 4; cpVect verts[] = { cpv(-24,-54), cpv(-24, 54), cpv( 24, 54), cpv( 24,-54), }; cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); body->p = cpv(pos.x, pos.y); cpSpaceAddBody(m_pSpace, body); cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); shape->e = 0.5f; shape->u = 0.5f; cpSpaceAddShape(m_pSpace, shape); CCPhysicsSprite *sprite = CCPhysicsSprite::createWithTexture(m_pSpriteTexture, CCRectMake(posx, posy, 85, 121)); parent->addChild(sprite,50); sprite->setCPBody(body); sprite->setPosition(pos); #endif }
bool HelloWorld::init() { if (!CCLayer::init()) { return false; } // enable events setTouchEnabled(true); setAccelerometerEnabled(true); CCSize s = CCDirector::sharedDirector()->getWinSize(); // title CCLabelTTF *label = CCLabelTTF::create("Multi touch the screen", "Marker Felt", 36); label->setPosition(ccp( s.width / 2, s.height - 30)); this->addChild(label, -1); // init physics initPhysics(); #if 1 // Use batch node. Faster CCSpriteBatchNode *parent = CCSpriteBatchNode::create("grossini_dance_atlas.png", 100); m_pSpriteTexture = parent->getTexture(); #else // doesn't use batch node. Slower m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("grossini_dance_atlas.png"); CCNode *parent = CCNode::node(); #endif addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(ccp(200,200)); scheduleUpdate(); return true; }
// on "init" you need to initialize your instance bool HelloChipmunk::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); // enable events setTouchEnabled(true); setAccelerometerEnabled(true); // title CCLabelTTF *label = CCLabelTTF::create("Multi touch the screen", "Marker Felt", 36); label->setPosition(ccp( 240, 320 - 30)); this->addChild(label, -1); // init physics initPhysics(); // Use batch node. Faster CCSpriteBatchNode *parent = CCSpriteBatchNode::create("blocks.png", 100); m_pSpriteTexture = parent->getTexture(); addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(ccp(200,200)); scheduleUpdate(); bRet = true; } while (0); return bRet; }
CCSprite* DiggingWorld::s_createTileSprite( const char *pRectName, unsigned int nColumn, unsigned int nHeight, float fScaleX, float fScaleY, int nZorder ) { LayerGaming *pLayerGaming = LayerGaming::sharedLayerGaming(); CCSpriteBatchNode *pBatchNode = pLayerGaming->getTilesBatchNode(); DiggingWorld *pDiggingWorld = DiggingWorld::sharedDiggingWorld(); CCSprite *pRetSprite = NULL; CCRect RECT_TILE = LuaHelper::s_getRectVar( pRectName ); pRetSprite = CCSprite::createWithTexture( pBatchNode->getTexture(), RECT_TILE ); pBatchNode->addChild( pRetSprite, nZorder ); //¼ÆËãspriteµÄλÖà float fSoilSpriteX = 0.0f; float fSoilSpriteY = 0.0f; pDiggingWorld->convertToGLCoordinate( nHeight, nColumn, fSoilSpriteX, fSoilSpriteY ); float fTileSize = pDiggingWorld->getTileSize(); pRetSprite->setScaleX( fTileSize / RECT_TILE.size.width * fScaleX ); pRetSprite->setScaleY( fTileSize / RECT_TILE.size.height * fScaleY ); pRetSprite->setPosition( ccp( fSoilSpriteX, fSoilSpriteY ) ); return pRetSprite; }
// 初始化底部Bar void ActivityView::initBottomBar(CCSize winSize) { /////////////////////////////////////////// 底部条 //////////////////////////////////////// this->footerBarNode = CCNode::create() ; this->footerBarNode->setAnchorPoint(ccp(0, 0)) ; this->footerBarNode->setPosition(ccp(0, -5)) ; this->addChild(this->footerBarNode) ; /////////////////////////////////////// 定义背景 ///////////////////////////////////////// // 背景 CCSprite* bottomBarBg = CCSprite::create("bg_bottom_bar.png") ; bottomBarBg->setAnchorPoint(ccp(0.5, 0)) ; bottomBarBg->setPosition(ccp(winSize.width/2, 0)) ; this->footerBarNode->addChild(bottomBarBg, 1) ; // 定义底部条高度 const float footerBarNode_height = bottomBarBg->getContentSize().height ; // 设置底部条的高和宽 this->footerBarNode->setContentSize(CCSizeMake(winSize.width, footerBarNode_height)) ; // 背景遮罩层 CCSprite* bottomBg = CCSprite::create("bg_bottom.png") ; bottomBg->setAnchorPoint(ccp(0, 1)) ; bottomBg->setPosition(ccp(0, footerBarNode_height)) ; this->footerBarNode->addChild(bottomBg, 0) ; CCSpriteBatchNode* bottomIcon = CCSpriteBatchNode::create("bg_bottom_icon.png") ; // 左边的icon CCSprite* leftIcon = CCSprite::createWithTexture(bottomIcon->getTexture()) ; leftIcon->setAnchorPoint(ccp(0, 0)) ; leftIcon->setPosition(ccp(0, 0)) ; this->footerBarNode->addChild(leftIcon, 1) ; // 右边的icon CCSprite* rightIcon = CCSprite::createWithTexture(bottomIcon->getTexture()) ; rightIcon->setAnchorPoint(ccp(1, 0)) ; rightIcon->setPosition(ccp(winSize.width, 0)) ; rightIcon->setFlipX(true) ; // 水平翻转 this->footerBarNode->addChild(rightIcon, 1) ; const float menuItem_margin = 50.0f ; // 距离中间位置的距离 const float menuItem_padding = 100.0f ; // menuItem之间的间隔 // 底部菜单 CCMenu* footerMenu = CCMenu::create() ; footerMenu->setContentSize(CCSizeMake(winSize.width, footerBarNode_height) ) ; footerMenu->setAnchorPoint(ccp(0, 0)) ; footerMenu->setPosition(ccp(0, 0)) ; this->footerBarNode->addChild(footerMenu, 2) ; // 活动菜单 CCMenuItemImage* activityMenuItem = CCMenuItemImage::create("menu_activity_n.png", "menu_activity_s.png", this, menu_selector(ActivityView::menuActivityClickCallback)) ; activityMenuItem->setAnchorPoint(ccp(1, 1)) ; activityMenuItem->setPosition(ccp(winSize.width/2-menuItem_margin, footerBarNode_height-3)) ; footerMenu->addChild(activityMenuItem) ; // 获取menuItem宽度 const float menuItem_width = activityMenuItem->getContentSize().width ; // 信息菜单 CCMenuItemImage* infoMenuItem = CCMenuItemImage::create("menu_message_n.png", "menu_message_s.png", this, menu_selector(ActivityView::menuInfoClickCallback)) ; infoMenuItem->setAnchorPoint(ccp(1, 1)) ; infoMenuItem->setPosition(ccp(winSize.width/2-menuItem_margin-menuItem_width-menuItem_padding, footerBarNode_height-3)) ; footerMenu->addChild(infoMenuItem) ; // 商城菜单 CCMenuItemImage* shopMenuItem = CCMenuItemImage::create("menu_shop_n.png", "menu_shop_s.png", this, menu_selector(ActivityView::menuShopClickCallback)) ; shopMenuItem->setAnchorPoint(ccp(0, 1)) ; shopMenuItem->setPosition(ccp(winSize.width/2+menuItem_margin, footerBarNode_height-3)) ; footerMenu->addChild(shopMenuItem) ; // 菜单菜单 CCMenuItemImage* moreMenuItem = CCMenuItemImage::create("menu_more_n.png", "menu_more_s.png" , this, menu_selector(ActivityView::menuMoreClickCallback)) ; moreMenuItem->setAnchorPoint(ccp(0, 1)) ; moreMenuItem->setPosition(ccp(winSize.width/2+menuItem_margin+menuItem_width+menuItem_padding, footerBarNode_height-3)) ; footerMenu->addChild(moreMenuItem) ; // 更多菜单背景 CCSprite* moreMenuBg = CCSprite::create("bg_more_menu.png") ; moreMenuBg->setAnchorPoint(ccp(0,0)) ; moreMenuBg->setPosition(ccp(0, 0)) ; const float moreMenu_width = moreMenuBg->getContentSize().width ; const float moreMenu_height = moreMenuBg->getContentSize().height ; // 更多菜单节点 moreMenuNode = CCNode::create() ; moreMenuNode->setContentSize(moreMenuBg->getContentSize()) ; // 设置moreMenuNode的尺寸 moreMenuNode->setAnchorPoint(ccp(1, 0)) ; moreMenuNode->setPosition(ccp(winSize.width-80, 80)) ; moreMenuNode->retain() ; // this->addChild(moreMenuNode, 5, MORE_MENU_NODE_TAG) ; moreMenuNode->addChild(moreMenuBg, 0) ; // 将背景加入到更多菜单中 // 更多菜单 CCMenu* moreMenu = CCMenu::create() ; moreMenu->setAnchorPoint(ccp(0, 0)) ; moreMenu->setPosition(ccp(0, 0)) ; moreMenuNode->addChild(moreMenu, 1, MORE_MENU_TAG) ; // 排行榜菜单项 CCMenuItemImage* rankingMenuItem = CCMenuItemImage::create("menu_ranking_n.png", "menu_ranking_s.png" , this, menu_selector(GameMenuView::menuRankingClickCallback)) ; rankingMenuItem->setAnchorPoint(ccp(0, 1)) ; rankingMenuItem->setPosition(ccp(10, moreMenu_height-10)) ; moreMenu->addChild(rankingMenuItem) ; // 任务菜单 CCMenuItemImage* taskMenuItem = CCMenuItemImage::create("menu_task_n.png", "menu_task_s.png" , this, menu_selector(GameMenuView::menuTaskClickCallback)) ; taskMenuItem->setAnchorPoint(ccp(1, 1)) ; taskMenuItem->setPosition(ccp(moreMenu_width-10, moreMenu_height-10)) ; moreMenu->addChild(taskMenuItem) ; // 好友菜单 CCMenuItemImage* friendMenuItem = CCMenuItemImage::create("menu_friend_n.png", "menu_friend_s.png", this, menu_selector(GameMenuView::menuFriendClickCallback)) ; friendMenuItem->setAnchorPoint(ccp(0, 0)) ; friendMenuItem->setPosition(ccp(10, 5)) ; moreMenu->addChild(friendMenuItem) ; // 客服菜单 CCMenuItemImage* customserviceMenuItem = CCMenuItemImage::create("menu_cs_n.png", "menu_cs_s.png", this, menu_selector(GameMenuView::menuSettingClickCallback)) ; customserviceMenuItem->setAnchorPoint(ccp(1, 0)) ; customserviceMenuItem->setPosition(ccp(moreMenu_width-10, 5)) ; moreMenu->addChild(customserviceMenuItem) ; }
bool GameLayer::init(void) { bool bRet = false; do { CC_BREAK_IF(!CCLayer::init()); // 游戏场景背景图 CCSprite *bg = CCSprite::spriteWithFile("Space.png"); CC_BREAK_IF(!bg); bg->setAnchorPoint(CCPointZero); // 为了突出游戏场景中的精灵,将背景色彩变淡 bg->setOpacity(100); this->addChild(bg, 0, 1); // 使用位图字体显示游戏时间 CCLabelBMFont* lbScore = CCLabelBMFont::labelWithString("Time: 0", "font09.fnt"); CC_BREAK_IF(!lbScore); lbScore->setAnchorPoint(ccp(1, 1)); lbScore->setScale(0.6f); this->addChild(lbScore, 1, 3); lbScore->setPosition(ccp(310, 450)); // 载入飞船图像集 CCSpriteBatchNode* mgr = CCSpriteBatchNode::batchNodeWithFile("flight.png", 5); CC_BREAK_IF(!mgr); this->addChild(mgr, 0, 4); // 在状态栏显示一个飞船的图标 CCSprite* sprite = CCSprite::spriteWithTexture(mgr->getTexture(), CCRectMake(0, 0, 31, 30)); CC_BREAK_IF(!sprite); mgr->addChild(sprite, 1, 5); sprite->setScale(1.1f); sprite->setAnchorPoint(ccp(0, 1)); sprite->setPosition(ccp(10, 460)); // 显示当前飞船生命条数 CCLabelBMFont* lbLife = CCLabelBMFont::labelWithString("3", "font09.fnt"); CC_BREAK_IF(!lbLife); lbLife->setAnchorPoint(ccp(0, 1)); lbLife->setScale(0.6f); this->addChild(lbLife, 1, 6); lbLife->setPosition(ccp(50, 450)); // 设定时间回调函数,修改游戏用时显示 this->schedule(schedule_selector(GameLayer::step), 1.0f); // 显示飞船,飞船有不断闪烁的火焰喷射效果 flight = CCSprite::spriteWithTexture(mgr->getTexture(), CCRectMake(0, 0, 31, 30)); CC_BREAK_IF(!flight); flight->setPosition(ccp(160, 30)); flight->setScale(1.6f); mgr->addChild(flight, 1, 99); // 设定动画每一帧的内容 CCAnimation* animation = CCAnimation::animation(); CC_BREAK_IF(!animation); animation->setName("flight"); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 3; ++i) { int x = i % 3; animation->addSpriteFrameWithTexture(mgr->getTexture(), CCRectMake(x*32, 0, 31, 30)); } // 基于动画创建动作 CCAnimate* action = CCAnimate::actionWithAnimation(animation); CC_BREAK_IF(!action); // 主角精灵不断重复动作,实现动态飞行效果 flight->runAction(CCRepeatForever::actionWithAction(action)); // accept touch now this->setTouchEnabled(true); bRet = true; } while (false); return bRet; }
/** * 初始化方法,继承CCLayer的方法 */ bool LoginView::init() { if(!CCLayer::init()) { return false ; } CCSize winSize = CCDirector::sharedDirector()->getWinSize() ; // 背景 CCSprite* downBg = CCSprite::create("bg_welcome.png") ; downBg->setAnchorPoint(ccp(0.5, 0)) ; downBg->setPosition(ccp(winSize.width/2, 0)) ; this->addChild(downBg, 0) ; // 加载logo CCSprite* logo = CCSprite::create("logo_01.png") ; logo->setAnchorPoint(ccp(0.5, 0.5)) ; logo->setPosition(ccp(winSize.width/2, winSize.height/2+35)) ; this->addChild(logo, 1) ; ///////////////////////////////// 输入框 //////////////////////////////////////// const float input_width = 213.0f ; const float input_height = 39.0f ; const float labelNode_padding = 8.0f ; const float labelNode_width = winSize.width/3 ; const float labelNode_height = 2*input_height+labelNode_padding ; CCNode* labelNode = CCNode::create() ; labelNode->setContentSize(CCSizeMake(labelNode_width, labelNode_height)) ; labelNode->setAnchorPoint(ccp(0.5, 0)) ; labelNode->setPosition(ccp(winSize.width/2-5, 85)) ; this->addChild(labelNode, 2) ; CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("bg_textfield.png") ; // 用户名 CCSprite* txtUsernameBg = CCSprite::createWithTexture(batchNode->getTexture()) ; txtUsernameBg->setAnchorPoint(ccp(0, 0)) ; txtUsernameBg->setPosition(ccp(0, input_height+labelNode_padding)) ; labelNode->addChild(txtUsernameBg, 0) ; this->txtUsername = CCTextFieldTTF::textFieldWithPlaceHolder("Username", CCSizeMake(input_width, input_height), kCCTextAlignmentLeft, "font01.ttf", 26.0f) ; this->txtUsername->setVerticalAlignment(kCCVerticalTextAlignmentCenter) ; this->txtUsername->setAnchorPoint(ccp(0, 0.5)) ; this->txtUsername->setPosition(ccp((winSize.width-labelNode_width)/2+5, 85+labelNode_height*3/4)) ; this->addChild(this->txtUsername, 3) ; // 密码 CCSprite* txtPasswordBg = CCSprite::createWithTexture(batchNode->getTexture()) ; txtPasswordBg->setAnchorPoint(ccp(0, 0)) ; txtPasswordBg->setPosition(ccp(0, 0)) ; labelNode->addChild(txtPasswordBg, 0) ; this->txtPassword = CCTextFieldTTF::textFieldWithPlaceHolder("Password", CCSizeMake(input_width, input_height), kCCTextAlignmentLeft, "font01.ttf", 26.0f) ; this->txtPassword->setVerticalAlignment(kCCVerticalTextAlignmentCenter) ; this->txtPassword->setAnchorPoint(ccp(0, 0.5)) ; this->txtPassword->setPosition(ccp((winSize.width-labelNode_width)/2+5, 85+labelNode_height/4-2)) ; this->addChild(this->txtPassword,3) ; // 登录按钮 CCMenuItemImage* loginMenuItem = CCMenuItemImage::create("menu_login_n.png", "menu_login_s.png", this, menu_selector(LoginView::menuLoginCallback)) ; loginMenuItem->setAnchorPoint(ccp(0, 0)) ; loginMenuItem->setPosition(ccp(0, input_height+labelNode_padding)) ; // 注册按钮 CCMenuItemImage* registeMenuItem = CCMenuItemImage::create("menu_registe_n.png", "menu_registe_s.png", this, menu_selector(LoginView::menuRegisteCallback)) ; registeMenuItem->setAnchorPoint(ccp(0, 0)) ; registeMenuItem->setPosition(ccp(0, 0)) ; CCMenu* loginMenu = CCMenu::create(loginMenuItem, registeMenuItem, NULL) ; loginMenu->setAnchorPoint(ccp(0, 0)) ; loginMenu->setPosition(ccp(input_width+10, -5)) ; labelNode->addChild(loginMenu) ; // ////////////////////////////////////////////////////////////////////////////////////////////// const float loginNode_padding = 11.0f ; const float loginNode_margin_bottom = 32.0f ; const float menu_item_width = 101.0f ; CCNode* optionNode = CCNode::create() ; optionNode->setContentSize(CCSizeMake(winSize.width/3, 50)) ; optionNode->setAnchorPoint(ccp(0.5, 0)) ; optionNode->setPosition(ccp(winSize.width/2-5, loginNode_margin_bottom)) ; this->addChild(optionNode, 1) ; // 游客登录 CCMenuItemImage* guestLoginItem = CCMenuItemImage::create("menu_guest_login_n.png", "menu_guest_login_s.png", this, menu_selector(LoginView::menuGuestLoginCallback)) ; guestLoginItem->setAnchorPoint(ccp(0, 0)) ; guestLoginItem->setPosition(ccp(0, 0)) ; // 忘记密码 CCMenuItemImage* forgetPwdItem = CCMenuItemImage::create("menu_forget_pwd_n.png", "menu_forget_pwd_s.png", this, menu_selector(LoginView::menuForgetPwdCallback)) ; forgetPwdItem->setAnchorPoint(ccp(0, 0)) ; forgetPwdItem->setPosition(ccp(menu_item_width+loginNode_padding, 0)) ; // go CCMenuItemImage* goMenuItem = CCMenuItemImage::create("menu_go_n.png", "menu_go_s.png", this, menu_selector(LoginView::menuGoCallback)) ; goMenuItem->setAnchorPoint(ccp(0, 0)) ; goMenuItem->setPosition(ccp(2*(menu_item_width+loginNode_padding), 0)) ; CCMenu* goMenu = CCMenu::create(guestLoginItem, forgetPwdItem, goMenuItem, NULL) ; goMenu->setAnchorPoint(ccp(0, 0)) ; goMenu->setPosition(ccp(0, 0)) ; optionNode->addChild(goMenu) ; /* ///////////////////////////////////////////////////////////////////////////////////////////// CCLabelTTF* tipLabel = CCLabelTTF::create("小提示:在游戏中达到规定时间,可以获取神秘大奖哦!", CCSizeMake(winSize.width, 45), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "font01.ttf", 22.0f) ; tipLabel->setAnchorPoint(ccp(0.5, 0)) ; tipLabel->setPosition(ccp(winSize.width/2, 20)) ; this->addChild(tipLabel, 2) ; */ // 接收键盘点击事件 this->setKeypadEnabled(true) ; // 触摸可使用 this->setTouchEnabled(true); return true ; }
GameLayer::GameLayer() { gameSuspended = true; isGameOver = false; fuelInTank = 3600; // one minute worth CCSpriteBatchNode* batchNode = dynamic_cast<CCSpriteBatchNode*>(getChildByTag(kSpriteManager)); // Initialize all the platforms. Really? They predraw all the platforms?? _initPlatforms(); // We don't need a packed sprite, we can pick individual textures. Here we are getting the sprite for the bird CCSprite* bird = CCSprite::createWithTexture(batchNode->getTexture(), CCRectMake(608, 16, 44, 32)); batchNode->addChild(bird, 4, kBird); // add the exit arrow CCSprite* exit = CCSprite::create("exit_arrow.png"); exit->setTag(kExit); exit->setVisible(false); addChild(exit); CCSize landscapeSize = CCDirector::sharedDirector()->getVisibleSize(); CCSize visibleSize = CCSize(landscapeSize.width, landscapeSize.height); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); pHealthSprite = CCSprite::create("health_bar.png"); //pHealthSprite->setPosition(ccp(origin.x + 10, visibleSize.height - 20)); pHealthSprite->setAnchorPoint(ccp(0.0, 0.0)); //pHealthSprite->setScaleY(0.02); //pHealthSprite->setScaleX(100); pHealthBar = CCProgressTimer::create(pHealthSprite); pHealthBar->setScale(0.5, 0.5); pHealthBar->setType(kCCProgressTimerTypeBar); //pHealthBar->setMidpoint(ccp(0,0)); pHealthBar->setBarChangeRate(ccp(1,0)); pHealthBar->setPercentage(100); pHealthBar->setPosition(ccp(origin.x+10, visibleSize.height - 20)); this->addChild(pHealthBar); CCSprite* bonus; // the bonus sprites which say 5, 10 etc can be picked from here for(int i = 0; i < kNumBonuses; i++) { bonus = CCSprite::createWithTexture(batchNode->getTexture(), CCRectMake(608 + i * 32, 256, 25, 25)); batchNode->addChild(bonus, 4, kBonusStartTag + i); bonus->setVisible(false); } // TODO: read the sprite for the Exit door - add the tag "Exit to it" CCLabelBMFont* scoreLabel = CCLabelBMFont::create("0", "bitmapFont.fnt"); addChild(scoreLabel, 5, kScoreLabel); scoreLabel->setPosition(ccp(160, 430)); _startGame(); scheduleUpdate(); // We don't want touch interaction setTouchEnabled(false); setAccelerometerEnabled(true); }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayerColor::initWithColor( ccc4(255,255,255,255) ) ); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2, origin.y + pCloseItem->getContentSize().height/2)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); ///////////////////////////// // 2. add your codes below... CCSprite *tree1 = CCSprite::create("tree.png"); tree1->setPosition( ccp(20,20) ); tree1->setAnchorPoint( ccp(0.5f, 0) ); tree1->setScale(1.5f); this->addChild( tree1, 2, TAG_TREE_SPRITE_1); CCSprite *cat = CCSprite::create("cheshire_cat.png"); cat->setPosition( ccp(250, 180) ); cat->setScale( 0.4f ); this->addChild( cat, 3, TAG_CAT_SPRITE); //CCTexture2D CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("tree.png"); CCSprite *tree2 = CCSprite::createWithTexture( texture ); tree2->setPosition( ccp(300, 20) ); tree2->setAnchorPoint( ccp(0.5f, 0) ); tree2->setScale(2.0f); this->addChild( tree2, 2, TAG_TREE_SPRITE_2 ); //CCSpriteFrameCache CCSpriteFrame *frame = CCSpriteFrame::createWithTexture( texture, tree2->getTextureRect() ); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame( frame, "tree.png"); CCSprite *tree3 = CCSprite::createWithSpriteFrameName("tree.png"); tree3->setPosition( ccp(400, 20) ); tree3->setAnchorPoint( ccp(0.5f, 0) ); tree3->setScale(1.5f); this->addChild(tree3, 2, TAG_TREE_SPRITE_3); // Load a set of sprite frames from PLIST file CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("alice_scene_sheet.plist"); CCSprite *alice = CCSprite::createWithSpriteFrameName( "alice.png" ); alice->getTexture()->generateMipmap(); ccTexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE }; alice->getTexture()->setTexParameters( &texParams ); alice->setPosition( ccp(120,20) ); alice->setScale(0.4f); alice->setAnchorPoint( ccp(0.5f, 0) ); this->addChild( alice, 2, TAG_ALICE_SPRITE); CCSequence *alice_scale = CCSequence::create( CCScaleTo::create(4.0f, 0.7f), CCScaleTo::create(4.0f, 0.1f), NULL ); CCRepeatForever *repeat_alice_scale = CCRepeatForever::create( alice_scale ); alice->runAction( repeat_alice_scale ); CCSpriteBatchNode *cloudBatch = CCSpriteBatchNode::create("cloud_01.png", 10); this->addChild( cloudBatch, 1, TAG_CLOUD_BATCH ); for(int x=0; x<10; x++) { CCSprite *s= CCSprite::createWithTexture( cloudBatch->getTexture(), CCRectMake(0,0,64,64) ); s->setOpacity(100); cloudBatch->addChild(s); s->setPosition( ccp( getRandom(1, 0x7ffffff)%640-50, getRandom(1, 0x7ffffff)%150+200) ); } //draw colored rectangles using a 1px x 1px white texture CCSprite *sky = CCSprite::create("blank.png"); sky->setPosition( ccp(320, 240) ); sky->setTextureRect( CCRectMake(0,0,640,260) ); sky->setColor( ccc3(150, 200, 200) ); this->setTouchEnabled(true); this->addChild( sky, 0); // use updateGame instead of update, otherwise it will conflit with SelectorProtocol::update // see http://www.cocos2d-x.org/boards/6/topics/1478 //this->schedule( schedule_selector(HelloWorld::updateGame) ); //CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true); bRet = true; } while (0); return bRet; }
// on "init" you need to initialize your instance bool MainGame::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } _visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto bg = cocos2d::LayerColor::create(Color4B(255, 255, 255, 255)); this->addChild(bg, 0); CCSpriteBatchNode *batchNodeLine = CCSpriteBatchNode::create("line.png", 2000); _itemBatch = CCSpriteBatchNode::create("x.png", 2000); _dataBoardGame = new CELL_VALUE*[GAME_MATRIX_SIZE]; for (int i = 0;i < GAME_MATRIX_SIZE;i++) { _dataBoardGame[i] = new CELL_VALUE[GAME_MATRIX_SIZE]; for (int j = 0;j < GAME_MATRIX_SIZE;j++) { _dataBoardGame[i][j] = CELL_VALUE::UNSET; } } int numLineEachRow = GAME_MATRIX_SIZE*GAME_CELL_SIZE /_lineWidth + 1; //TODO get size int numLineCol = GAME_MATRIX_SIZE; _lineMatrixLenght = numLineEachRow*GAME_MATRIX_SIZE * 2; _lineMatrix = new cocos2d::Sprite*[_lineMatrixLenght]; _startLocationX = -(GAME_MATRIX_SIZE*GAME_CELL_SIZE - _visibleSize.width) / 2; _startLocationY = (GAME_MATRIX_SIZE*GAME_CELL_SIZE - _visibleSize.height) / 2 + _visibleSize.height; // Horizontal for (int i = 0;i < GAME_MATRIX_SIZE;i++) { for (int j = 0;j < numLineEachRow;j++) { _lineMatrix[i*numLineEachRow + j] = cocos2d::Sprite::createWithTexture(batchNodeLine->getTexture()); _lineMatrix[i*numLineEachRow + j]->setAnchorPoint(Vec2(0, 0)); _lineMatrix[i*numLineEachRow + j]->setPosition(_startLocationX+j*_lineWidth, _startLocationY-i*GAME_CELL_SIZE); this->addChild(_lineMatrix[i*numLineEachRow + j]); } } // Vertical for (int i = 0;i < GAME_MATRIX_SIZE;i++) { for (int j = 0;j < numLineEachRow;j++) { _lineMatrix[GAME_MATRIX_SIZE*numLineEachRow + i*numLineEachRow + j] = cocos2d::Sprite::createWithTexture(batchNodeLine->getTexture()); _lineMatrix[GAME_MATRIX_SIZE*numLineEachRow + i*numLineEachRow + j]->setAnchorPoint(Vec2(0, 0)); _lineMatrix[GAME_MATRIX_SIZE*numLineEachRow + i*numLineEachRow + j]->setRotation(90); _lineMatrix[GAME_MATRIX_SIZE*numLineEachRow + i*numLineEachRow + j]->setPosition(_startLocationX + i*GAME_CELL_SIZE, _startLocationY - j*_lineWidth); this->addChild(_lineMatrix[GAME_MATRIX_SIZE*numLineEachRow + i*numLineEachRow + j]); } } _itemMatrix = new Sprite*[GAME_MATRIX_SIZE*GAME_MATRIX_SIZE]; // Implement touch event auto singleTouchListener = EventListenerTouchOneByOne::create(); singleTouchListener->onTouchBegan = CC_CALLBACK_2(MainGame::onTouchBegan, this); singleTouchListener->onTouchMoved = CC_CALLBACK_2(MainGame::onTouchMoved, this); singleTouchListener->onTouchEnded = CC_CALLBACK_2(MainGame::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(singleTouchListener, this); return true; }
bool GameLayer::init() { if (!CCLayer::init()) { return false; } char buffer[30]; _numBoardBatch = CCArray::createWithCapacity(9); _numBoardBatch->retain(); for(int i = 1; i <= 9; i++) { sprintf(buffer, "tileStyle/pn/%d.png", i); CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create(buffer); _numBoardBatch->addObject(batchNode); } CCSprite * background = CCSprite::create("tileStyle/background.jpg"); background->setPosition(ccp(240, 400)); this->addChild(background); _back = CCSprite::create("back.png"); _back->setPosition(ccp(24 + 40, 800 - 56.5 + 15)); this->addChild(_back); _restart = CCSprite::create("tileStyle/restart.png"); _restart->setPosition(ccp(375 + 40, 800 - 56.5 + 15)); this->addChild(_restart); _timer = CCLabelTTF::create("0", "Verdana-Bold", 30); _timer->setPosition(ccp(240, 800 - 13 - 20)); _timer->setVisible(Settings::showTimer); this->addChild(_timer); _numButtons = CCArray::createWithCapacity(9); _numButtons->retain(); _numNote = CCArray::createWithCapacity(9); _numNote->retain(); _numNoteBatch = CCArray::createWithCapacity(9); _numNoteBatch->retain(); _backPlanes = CCArray::createWithCapacity(9); _backPlanes->retain(); CCSpriteBatchNode* backPlaneBatch = CCSpriteBatchNode::create("tileStyle/backplane.png"); for(int i = 1; i <= 9; i++) { sprintf(buffer, "tileStyle/numb/%d.png", i); CCSprite * num = CCSprite::create(buffer); num->setZOrder(4); _numButtons->addObject(num); sprintf(buffer, "tileStyle/noteb/%d.png", i); CCSpriteBatchNode* noteBatch = CCSpriteBatchNode::create(buffer); _numNoteBatch->addObject(noteBatch); CCSprite * note = CCSprite::createWithTexture(noteBatch->getTexture()); _numNote->addObject(note); CCSprite * backPlane = CCSprite::createWithTexture(backPlaneBatch->getTexture()); _backPlanes->addObject(backPlane); } int k; for (int order = 0; order < 2; order++) { for (int i = 0; i < 5 - order; i++) { k = 2 * i + order; CCSprite * num = (CCSprite*)_numButtons->objectAtIndex(k); num->setPosition(ccp(BX_START + k * BX_STEP, BY[order])); num->setVisible(Sudoku::state == Running); this->addChild(num); CCSprite * note = (CCSprite *)_numNote->objectAtIndex(k); note->setPosition(ccp(BX_START + k * BX_STEP, BY[order])); note->setVisible(Sudoku::state == Note); this->addChild(note); CCSprite * backPlane = (CCSprite *)_backPlanes->objectAtIndex(k); backPlane->setPosition(ccp(BX_START + k * BX_STEP, BY[order])); this->addChild(backPlane); } } updateBackplane(); _hint = CCLabelTTF::create("5", "Verdana-Bold", 32); _hint->setPosition(ccp(12 + 33 - 2, 800 - 704 - 33 + 2)); _hint->setColor(ccc3(0, 0, 0)); updateHint(); this->addChild(_hint); CCSprite * magnifierGlass = CCSprite::create("tileStyle/hint.png"); magnifierGlass->setPosition(ccp(12 + 33, 800 - 704 - 33)); this->addChild(magnifierGlass); _pencilToNote = CCSprite::create("tileStyle/pencil.png"); _pencilToNote->setPosition(ccp(401 + 33, 800 - 704 - 33)); _pencilToNote->setVisible(Sudoku::state == Running); this->addChild(_pencilToNote); _penToRun = CCSprite::create("tileStyle/pen.png"); _penToRun->setPosition(ccp(401 + 33, 800 - 704 - 33)); _penToRun->setVisible(Sudoku::state == Note); this->addChild(_penToRun); this->setTouchEnabled(true); this->setKeypadEnabled(true); this->schedule(schedule_selector(GameLayer::update)); return true; }
bool ToyLayer::init() { //=================== Box2D World ======================== b2Vec2 gravity; gravity.Set(0.0F, -10.0F); this->world = new b2World(gravity); world->SetAllowSleeping(true); world->SetContinuousPhysics(true); //ToyContact *contact = new ToyContact(); //world->SetContactListener(contact); CCPoint tmp; tmp = VisibleRect::leftBottom(); b2Vec2 lb = b2Vec2(tmp.x / PTM_RATIO, tmp.y / PTM_RATIO); tmp = VisibleRect::rightBottom(); b2Vec2 rb = b2Vec2(tmp.x / PTM_RATIO, tmp.y / PTM_RATIO); tmp = VisibleRect::leftTop(); b2Vec2 lt = b2Vec2(tmp.x / PTM_RATIO, tmp.y / PTM_RATIO); tmp = VisibleRect::rightTop(); b2Vec2 rt = b2Vec2(tmp.x / PTM_RATIO, tmp.y / PTM_RATIO); b2BodyDef bodyDef; this->groundBody = this->world->CreateBody(&bodyDef); //World Edge b2BodyDef bd; b2Body* ground = this->world->CreateBody(&bd); { b2EdgeShape shape; shape.Set(lb, rb); ground->CreateFixture(&shape, 0.0f); } { b2EdgeShape shape; shape.Set(lt, rt); ground->CreateFixture(&shape, 0.0f); } { b2EdgeShape shape; shape.Set(lb, lt); ground->CreateFixture(&shape, 0.0f); } { b2EdgeShape shape; shape.Set(rb, rt); ground->CreateFixture(&shape, 0.0f); } //enable this->setTouchEnabled( true ); this->setAccelerometerEnabled( true ); //=================== Others ======================= this->toyArray = CCArray::create(); this->toyArray->retain(); //CCSpriteBatchNode *boxBatch = CCSpriteBatchNode::create("Images/blocks.png", 100); CCSpriteBatchNode *boxBatch = CCSpriteBatchNode::create("Images/ToyBrick/Triangle/1.png", 100); boxBatch->setTag(1); this->addChild(boxBatch, 0); this->boxTexture = boxBatch->getTexture(); //init this->AddRandomToyBrick(ccp(200, 200)); this->AddRandomToyBrick(ccp(300, 300)); this->AddRandomToyBrick(ccp(500, 500)); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //Menu -Close CCMenuItemImage *closeItem = CCMenuItemImage::create( "close.png", "close.png", this, menu_selector(ToyLayer::Back)); closeItem->setAnchorPoint(CCPointZero); closeItem->setScale(2); CCSize itemSize = closeItem->getContentSize(); closeItem->setPosition(winSize.width - (itemSize.width + 15) * 2, winSize.height - (itemSize.height + 15) * 2); CCMenu* menu = CCMenu::create(closeItem, NULL); menu->setPosition(0, 0); this->addChild(menu); //=================== Common Setting ===================== scheduleUpdate(); return true; }