//------------------------------------------------------------------ // // TMXOrthoTest // //------------------------------------------------------------------ TMXOrthoTest::TMXOrthoTest() { // // Test orthogonal with 3d camera and anti-alias textures // // it should not flicker. No artifacts should appear // //auto color = LayerColor::create( Color4B(64,64,64,255) ); //addChild(color, -1); auto map = TMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); auto pChildrenArray = map->getChildren(); SpriteBatchNode* child = NULL; Object* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = static_cast<SpriteBatchNode*>(pObject); if(!child) break; child->getTexture()->setAntiAliasTexParameters(); }
void HelloWorld::createMapAndGetTile() { auto mapnotchange = TMXTiledMap::create("orthogonal-test4.tmx"); addChild(mapnotchange, 0, 1); mapnotchange->setPosition(50,240); auto map = TMXTiledMap::create("orthogonal-test4.tmx"); addChild(map, 0, 2); map->setPosition(570,240); SpriteBatchNode* child = nullptr; auto& children = map->getChildren(); for(const auto &node : children) { child = static_cast<SpriteBatchNode*>(node); child->getTexture()->setAntiAliasTexParameters(); } map->setAnchorPoint(Point(0, 0)); auto layer = map->getLayer("Layer 0"); auto s = layer->getLayerSize(); Sprite* sprite; sprite = layer->getTileAt(Point(0,0)); sprite->setScale(2); sprite = layer->getTileAt(Point(s.width-1,0)); sprite->setScale(2); sprite = layer->getTileAt(Point(0,s.height-1)); sprite->setScale(2); sprite = layer->getTileAt(Point(s.width-1,s.height-1)); sprite->setScale(2); }
//------------------------------------------------------------------ // // TMXOrthoTest4 // //------------------------------------------------------------------ TMXOrthoTest4::TMXOrthoTest4() { auto map = TMXTiledMap::create("TileMaps/orthogonal-test4.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s1 = map->getContentSize(); CCLOG("ContentSize: %f, %f", s1.width,s1.height); SpriteBatchNode* child = nullptr; auto& children = map->getChildren(); for(const auto &node : children) { child = static_cast<SpriteBatchNode*>(node); child->getTexture()->setAntiAliasTexParameters(); } map->setAnchorPoint(Vec2(0, 0)); auto layer = map->getLayer("Layer 0"); auto s = layer->getLayerSize(); Sprite* sprite; sprite = layer->getTileAt(Vec2(0,0)); sprite->setScale(2); sprite = layer->getTileAt(Vec2(s.width-1,0)); sprite->setScale(2); sprite = layer->getTileAt(Vec2(0,s.height-1)); sprite->setScale(2); sprite = layer->getTileAt(Vec2(s.width-1,s.height-1)); sprite->setScale(2); schedule( schedule_selector(TMXOrthoTest4::removeSprite), 2 ); }
void HelloWorld::addBird(cocos2d::Point p) { SpriteBatchNode *parent = SpriteBatchNode::create("bird_hero.png"); m_pSpriteTexture = parent->getTexture();//获取纹理贴图 //将精灵批处理节点添加到层 this->addChild(parent,0,kTagParentNode); // Sprite *sprite = Sprite::createWithTexture(m_pSpriteTexture,Rect(32*idx, 32*idy, 32, 32)); bird = Sprite::createWithTexture(m_pSpriteTexture); parent->addChild(bird); Size birdSize = m_pSpriteTexture->getContentSize(); b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = bird; b2Body *body = world->CreateBody(&bodyDef); //定义盒子形状 b2PolygonShape dynamicBox; dynamicBox.SetAsBox(birdSize.width/2/PTM_RATIO, birdSize.height/2/PTM_RATIO); b2FixtureDef fixtureDef; // fixtureDef.filter.groupIndex = 0; // fixtureDef.filter.maskBits = 0x0002;//掩码 // fixtureDef.filter.categoryBits = 0x0003;//设置分类码 fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.f;//密度 fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.f;//弹性系数 body->CreateFixture(&fixtureDef);//用刚体创建夹具 //事件监听 auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); listener1->onTouchBegan =[=](Touch *touch,Event *event) { //设置线性速度 body->SetLinearVelocity(b2Vec2(0, 7)); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this); }
//------------------------------------------------------------------ // // TMXOrthoTest2 // //------------------------------------------------------------------ TMXOrthoTest2::TMXOrthoTest2() { auto map = TMXTiledMap::create("TileMaps/orthogonal-test1.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); auto& children = map->getChildren(); SpriteBatchNode* child = NULL; for(const auto &obj : children) { child = static_cast<SpriteBatchNode*>(obj); child->getTexture()->setAntiAliasTexParameters(); } map->runAction( ScaleBy::create(2, 0.5f) ) ; }
Box2DTestLayer::Box2DTestLayer() : _spriteTexture(NULL) , world(NULL) { #if CC_ENABLE_BOX2D_INTEGRATION setTouchEnabled( true ); setAccelerometerEnabled( true ); // init physics this->initPhysics(); // create reset button this->createResetButton(); //Set up sprite #if 1 // Use batch node. Faster SpriteBatchNode *parent = SpriteBatchNode::create("Images/blocks.png", 100); _spriteTexture = parent->getTexture(); #else // doesn't use batch node. Slower _spriteTexture = TextureCache::getInstance()->addImage("Images/blocks.png"); Node *parent = Node::create(); #endif addChild(parent, 0, kTagParentNode); addNewSpriteAtPosition(VisibleRect::center()); LabelTTF *label = LabelTTF::create("Tap screen", "Marker Felt", 32); addChild(label, 0); label->setColor(Color3B(0,0,255)); label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y-50)); scheduleUpdate(); #else LabelTTF *label = LabelTTF::create("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case", "Arial", 18); Size size = Director::getInstance()->getWinSize(); label->setPosition(Point(size.width/2, size.height/2)); addChild(label); #endif }
//------------------------------------------------------------------ // // TMXOrthoTest3 // //------------------------------------------------------------------ TMXOrthoTest3::TMXOrthoTest3() { auto map = TMXTiledMap::create("TileMaps/orthogonal-test3.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); auto& children = map->getChildren(); SpriteBatchNode* child = NULL; for(const auto &node : children) { child = static_cast<SpriteBatchNode*>(node); child->getTexture()->setAntiAliasTexParameters(); } map->setScale(0.2f); map->setAnchorPoint( Vec2(0.5f, 0.5f) ); }
/* -(void) runDisappearAnimation : (BlockType )type : (CGPoint)pos : (b2World *)world : (b2Body *)body { if (type == Stone) { width = 15; height = 15; frameCount = 4; delay = 0.5f; stoneType = [NSString stringWithFormat:@"stoneAni_%d.png",GLOBAL_MAPINFO[GLOBAL_GAMEINFO.currentLevel-1]]; } else if (type == Item) { width = height = 27; frameCount = 8; delay = 0.2f; stoneType = [NSString stringWithFormat:@"Tannenzapfen.png"]; } SpriteBatchNode *parent = [SpriteBatchNode batchNodeWithFile:stoneType]; spriteTexture_ = [parent texture]; Sprite *sp1 = [Sprite spriteWithTexture:spriteTexture_ rect:CGRectMake( 0 , 0 , width, height)]; Sprite *sp2 = [Sprite spriteWithTexture:spriteTexture_ rect:CGRectMake( 15 , 0 , width, height)]; uint offsetY = 20; uint offsetX = 10; sp1.position = ccp(sp1.position.x +offsetX,sp1.position.y+offsetY); sp2.position = ccp(sp2.position.x+offsetX,sp2.position.y+offsetY); //sp3.position = ccp(sp3.position.x+offsetX,sp3.position.y+offsetY); //sp4.position = ccp(sp4.position.x+offsetX,sp4.position.y+offsetY); [self.parent addChild: sp1 z:15]; // [self addChild: sp2 z:15]; b2Body *body_StonePiece1; b2BodyDef bodyDef_StonePiece1; b2FixtureDef fixtureDef_StonePiece1; b2PolygonShape shape_StonePiece1; bodyDef_StonePiece1.type = b2_dynamicBody; bodyDef_StonePiece1.position.Set(sp1.position.x/PTM_RATIO, sp1.position.y/PTM_RATIO); bodyDef_StonePiece1.userData = sp1; body_StonePiece1 = world->CreateBody(&bodyDef_StonePiece1); shape_StonePiece1.SetAsBox(sp1.contentSize.width/PTM_RATIO*0.5, sp1.contentSize.height/PTM_RATIO*0.5); fixtureDef_StonePiece1.restitution = 0.5f; fixtureDef_StonePiece1.friction = 0.5f; fixtureDef_StonePiece1.density = 1.0f; fixtureDef_StonePiece1.shape = &shape_StonePiece1; body_StonePiece1->CreateFixture(&fixtureDef_StonePiece1); body_StonePiece1->ApplyForce(b2Vec2(1,15), body_StonePiece1->GetWorldCenter()); [self removeFromParentAndCleanup:YES]; } */ CCAnimation * BlockSprite::createAnimationFromCachedImage(BlockType type, unsigned int stage) { unsigned int smallStage = GLOBAL_MAPINFO[stage - 1]; if (type == Stone) { frameCount = 5; delay = 0.3f; width = 27 * RATE; height = 36 * RATE; if (smallStage == 1 || smallStage == 2 || smallStage == 6) stoneType = String::createWithFormat("Stone_%d_%d-hd.png", smallStage, rand() % 3 + 1); //[NSString stringWithFormat : @"Stone_%d_%d.png", smallStage, rand() % 3 + 1]; else stoneType = String::createWithFormat("Stone_%d_%d-hd.png", smallStage, 1); //[NSString stringWithFormat : @"Stone_%d_%d.png", smallStage, 1]; } else if (type == Item) { frameCount = 5; delay = 0.3f; width = height = 27 * RATE; stoneType = String::createWithFormat("Item_%d-hd.png", smallStage); //[NSString stringWithFormat : @"Item_%d.png", smallStage]; } SpriteBatchNode *parent = SpriteBatchNode::create(stoneType->getCString()); //[SpriteBatchNode batchNodeWithFile : stoneType]; spriteTexture_ = parent->getTexture(); //[parent texture]; //NSMutableArray *cachedImage = [NSMutableArray array]; Vector<SpriteFrame*> cachedImage; for (int i = 0; i < frameCount; i++) { //[cachedImage addObject : //[SpriteFrame frameWithTexture : spriteTexture_ rect : CGRectMake(i * 27 * RATE, 0, width, height)]]; cachedImage.pushBack(SpriteFrame::createWithTexture(spriteTexture_, Rect(i * 27 * RATE, 0, width, height))); } CCAnimation *animation = Animation::createWithSpriteFrames(cachedImage, delay); //[CCAnimation animationWithSpriteFrames : cachedImage delay : delay]; return animation; }
void TileMapLayer::createWithTMX(const char* tmxFile) { // create a TMX map --- WARNING : if tsx path in the tmx file is subclass of Resources, libs can't find *.tsx, *.png map = TMXTiledMap::create(tmxFile); prepareLayers(); addChild(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' Array * pChildrenArray = map->getChildren(); SpriteBatchNode* child = NULL; Object* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = (SpriteBatchNode*)pObject; if(!child) break; child->getTexture()->setAntiAliasTexParameters(); }
// on "init" you need to initialize your instance bool CShop::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 3. add your codes below... auto m_textureCache = TextureCache::getInstance(); //添加背景 auto bg = Sprite::createWithTexture(m_textureCache->getTextureForKey("select_degree.jpg")); bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(bg); //////////道具种类///////////// //双倍分数 图标 auto scoreSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_score.png")); scoreSprite->setPosition(Vec2(visibleSize.width/4,visibleSize.height*0.6)); this->addChild(scoreSprite); //双倍金币 图标 auto goldSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_gold.png")); goldSprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height)); this->addChild(goldSprite); //延时 图标 auto delaySprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_delay.png")); delaySprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 2)); this->addChild(delaySprite); //提示 图标 auto hintSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_hint.png")); hintSprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 3)); this->addChild(hintSprite); //////////玩家当前道具数量///////////// std::string num; //玩家金币数量 auto m_goldSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("rest_gold.png")); m_goldSprite->setPosition(Vec2(visibleSize.width - m_goldSprite->getContentSize().width*0.6, visibleSize.height - m_goldSprite->getContentSize().height*0.6)); m_goldSprite->runAction(Sequence::create(JumpTo::create(1.0,m_goldSprite->getPosition(),m_goldSprite->getContentSize().height,2),nullptr)); this->addChild(m_goldSprite); num = String::createWithFormat("%d", CScore::getInstance()->getGold())->_string; auto remainLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); remainLabel->setString(num); remainLabel->setScale(1.5); remainLabel->setAlignment(TextHAlignment::RIGHT); remainLabel->setAnchorPoint(Vec2(1,1)); remainLabel->setPosition(Vec2(visibleSize.width, visibleSize.height-m_goldSprite->getContentSize().height)); this->addChild(remainLabel,1,101); /* //金币积分兑换 auto totalSprite1 = Sprite::create("total_change.png"); auto totalSprite2 = Sprite::create("total_change.png"); totalSprite2->setScale(1.2); auto totalGoldItem = MenuItemSprite::create( totalSprite1, totalSprite2 ); totalGoldItem->setCallback(CC_CALLBACK_1(CShop::onChangeItemCallback, this)); totalGoldItem->setPosition(Vec2( visibleSize.width - totalGoldItem->getContentSize().width / 2, visibleSize.height - m_goldSprite->getContentSize().height-remainLabel->getContentSize().height*2)); num = String::createWithFormat("%d", AdHelp::queryPoints())->_string; totalLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); totalLabel->setString(num); totalLabel->setScale(1.5); totalLabel->setAlignment(TextHAlignment::RIGHT); totalLabel->setAnchorPoint(Vec2(1, 1)); totalLabel->setPosition(Vec2(visibleSize.width, visibleSize.height - m_goldSprite->getContentSize().height - remainLabel->getContentSize().height-totalSprite1->getContentSize().height)); this->addChild(totalLabel, 1,100); */ //返回主界面 按钮 auto spriteNor0 = Sprite::createWithTexture( m_textureCache->getTextureForKey("return.png")); auto spriteSel0 = Sprite::createWithTexture( m_textureCache->getTextureForKey("return.png")); spriteSel0->setScale(1.2); auto backItem = MenuItemSprite::create( spriteNor0, spriteSel0 ); backItem->setCallback(CC_CALLBACK_1(CShop::onReturnItemCallback, this)); backItem->setPosition(Vec2(visibleSize.width / 8, visibleSize.height * 0.8)); // auto menu0 = Menu::create(totalGoldItem, backItem, nullptr); auto menu0 = Menu::create(backItem, nullptr); menu0->setPosition(Vec2::ZERO); addChild(menu0); //双倍分数 道具数量 num = String::createWithFormat("%d", CScore::getInstance()->getMultScore())->_string; auto scoreLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); scoreLabel->setString(num); scoreLabel->setScale(1.5); scoreLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6)); this->addChild(scoreLabel,1,102); //双倍金币 num = String::createWithFormat("%d", CScore::getInstance()->getMultGold())->_string; auto goldLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); goldLabel->setString(num); goldLabel->setScale(1.5); goldLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height)); this->addChild(goldLabel,1, 103); //延时 num = String::createWithFormat("%d", CScore::getInstance()->getPropDelay())->_string; auto delayLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); delayLabel->setString(num); delayLabel->setScale(1.5); delayLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 2)); this->addChild(delayLabel, 1, 104); //提示 num = String::createWithFormat("%d", CScore::getInstance()->getPropHint())->_string; auto hintLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); hintLabel->setString(num); hintLabel->setScale(1.5); hintLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 3)); this->addChild(hintLabel, 1, 105); /* auto t = Label::createWithTTF( "test", "fonts/DFPShaoNvW5-GB.ttf", 40); t->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(t, 1);*/ //////////购买道具按钮///////////// SpriteBatchNode* spriteBatchNode = SpriteBatchNode::create("100.png"); this->addChild(spriteBatchNode); auto spriteNor1 = Sprite::createWithTexture(spriteBatchNode->getTexture()); auto spriteSel1 = Sprite::createWithTexture(spriteBatchNode->getTexture()); spriteSel1->setScale(1.2); // spriteBatchNode->addChild(spriteNor1); // spriteBatchNode->addChild(spriteSel1); auto spriteNor2 = Sprite::createWithTexture(spriteBatchNode->getTexture()); auto spriteSel2 = Sprite::createWithTexture(spriteBatchNode->getTexture()); spriteSel2->setScale(1.2); /* spriteBatchNode->addChild(spriteNor2); spriteBatchNode->addChild(spriteSel2); */ SpriteBatchNode* spriteBatchNode1 = SpriteBatchNode::create("300.png"); this->addChild(spriteBatchNode1); auto spriteNor3 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); auto spriteSel3 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); spriteSel3->setScale(1.2); /* spriteBatchNode->addChild(spriteNor3); spriteBatchNode->addChild(spriteSel3); */ auto spriteNor4 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); auto spriteSel4 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); spriteSel4->setScale(1.2); /* spriteBatchNode->addChild(spriteNor4); spriteBatchNode->addChild(spriteSel4); */ //购买道具 分数加倍 按钮 auto scoreItem = MenuItemSprite::create( spriteNor1, spriteSel1 ); scoreItem->setCallback(CC_CALLBACK_1(CShop::onScoreItemCallback, this)); //购买道具 金币加倍 按钮 auto goldItem = MenuItemSprite::create( spriteNor2, spriteSel2 ); goldItem->setCallback(CC_CALLBACK_1(CShop::onGoldItemCallback, this)); //购买道具 延时 按钮 auto delayItem = MenuItemSprite::create( spriteNor3, spriteSel3 ); delayItem->setCallback(CC_CALLBACK_1(CShop::onDelayItemCallback, this)); //购买道具 提示 按钮 auto hintItem = MenuItemSprite::create( spriteNor4, spriteSel4 ); hintItem->setCallback(CC_CALLBACK_1(CShop::onHintItemCallback, this)); auto menu = Menu::create(scoreItem, goldItem, delayItem, hintItem, nullptr); menu->alignItemsVerticallyWithPadding(scoreItem->getContentSize().height); menu->setPosition(Vec2(visibleSize.width *3/ 4, visibleSize.height*0.43)); this->addChild(menu); return true; }
//实现PianoLayer类中的init方法,初始化布景 bool PianoLayer::init() { //调用父类的初始化 if ( !Layer::init() ) { return false; } degree = 0; musicFlag = false; pauseFlag = false; instrumentName = "piano"; instrumentNumber = 1; changeFlag = true; musicNum = 0; //获取可见区域尺寸 Size visibleSize = Director::getInstance()->getVisibleSize(); //获取可见区域原点坐标 Point origin = Director::getInstance()->getVisibleOrigin(); Sprite* volume = Sprite::create(pic_RESOURE_PATH + "volume_cn.png"); //音量 volumeSize = volume->getContentSize(); volume->setScale(1.2); volume->setPosition(Point(volumeSize.width/2 + 20, visibleSize.height - volumeSize.height/2)); this->addChild(volume, 2); exit = Sprite::create(pic_RESOURE_PATH + "exit.png"); //退出 Size exitSize = exit->getContentSize(); exit->setScale(1.2); exit->setPosition(Point(visibleSize.width - exitSize.width/2, visibleSize.height - exitSize.height/2)); this->addChild(exit, 2); slider = Slider::create(); slider->loadBarTexture(pic_RESOURE_PATH + "soundBackGround.png"); slider->loadSlidBallTextures(pic_RESOURE_PATH + "transparent.png",pic_RESOURE_PATH + "transparent.png", ""); slider->loadProgressBarTexture(pic_RESOURE_PATH + "sound.png"); slider->setAnchorPoint(Point(0, 0.5)); sliderSize = slider->getContentSize(); slider->setPercent(CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume()*100); slider->setPosition(Point(volumeSize.width*1.2 + 20, visibleSize.height - volumeSize.height/2)); slider->addEventListener(CC_CALLBACK_2(PianoLayer::sliderEvent, this)); this->addChild(slider, 5); instrument = Sprite::create(pic_RESOURE_PATH + "piano_cn.png"); //选择 Size selectSize = instrument->getContentSize(); instrument->setPosition( Point( visibleSize.width - exitSize.width - selectSize.width/2 -20, visibleSize.height - selectSize.height/2 )); this->addChild(instrument, 2); selection = Sprite::create(pic_RESOURE_PATH + "back.png"); //背景 selection->setPosition( Point( visibleSize.width - exitSize.width - selectSize.width/2 -20, visibleSize.height - selectSize.height/2 )); this->addChild(selection, 1); Sprite* left = Sprite::create(pic_RESOURE_PATH + "left.png"); //左键 leftSize = left->getContentSize(); left->setScale(SCALE); left->setPosition( Point( leftSize.width/2*SCALE + 10, visibleSize.height - volumeSize.height - leftSize.height*SCALE/2 - 5 )); this->addChild(left, 2); Sprite* right = Sprite::create(pic_RESOURE_PATH + "right.png"); //右键 rightSize = right->getContentSize(); right->setScale(SCALE); right->setPosition( Point( visibleSize.width - rightSize.width*SCALE/2 - 10, visibleSize.height - volumeSize.height - rightSize.height*SCALE/2 - 5 )); this->addChild(right, 2); //第二个参数为最大储存数 SpriteBatchNode* batchNode = SpriteBatchNode::create(pic_RESOURE_PATH + "white_small.jpg", 50); //小键盘 batchNode->setAnchorPoint(Point(0, 0)); batchNode->setPosition( Point( leftSize.width*SCALE + LEFT_INTERVAL, visibleSize.height - volumeSize.height - ABOVE_INTERVAL )); this->addChild(batchNode); for(int i = 0; i<PIANO_KEY; i++) //小键盘 { Sprite* whiteSmall = Sprite::createWithTexture(batchNode->getTexture(), Rect(0, 0, 34, 57)); whiteSmall->setScale(SCALE); whiteSmall->setAnchorPoint(Point(0, 0)); smallSize = whiteSmall->getContentSize(); whiteSmall->setPosition(Point(i*smallSize.width*SCALE,0)); batchNode->addChild(whiteSmall, 2); } selectBack = Sprite::create(pic_RESOURE_PATH + "selectBack.png"); backSize = selectBack->getContentSize(); selectBack->setPosition( Point( leftSize.width*SCALE + LEFT_INTERVAL + smallSize.width*SCALE*7 + backSize.width/2, visibleSize.height - volumeSize.height - 37 )); this->addChild(selectBack, 4); float width = PIANO_KEY*smallSize.width*SCALE - backSize.width; //总长 percent = (selectBack->getPosition().x - backSize.width/2 - leftSize.width*SCALE - LEFT_INTERVAL) / width; float positionX = -percent*WHITE_INTERVAL*13; // for(int i=0; i<PIANO_KEY; i++) { Sprite* white = Sprite::create(pic_RESOURE_PATH + "white.png"); white->setScale(1.12); white->setAnchorPoint(Point(0, 0)); white->setPosition(Point(positionX + i*WHITE_INTERVAL, 0)); string tempNumber = StringUtils::format("%d", i); whiteSize = white->getContentSize(); piano.insert(pair<string,Sprite*>(tempNumber, white)); } for(int i = PIANO_KEY,j=0;i<38; i++) { Sprite* black = Sprite::create(pic_RESOURE_PATH + "black.png"); black->setScale(1.12); black->setAnchorPoint(Point(0.5, 0)); blackSize = black->getContentSize(); black->setPosition(Point(positionX + (i-21+j)*WHITE_INTERVAL, (whiteSize.height - blackSize.height)*1.12)); string tempNumber = StringUtils::format("%d", i); if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12) { j++; } piano_black.insert(pair<string,Sprite*>(tempNumber, black)); } map<string, Sprite*>::iterator iter; for(iter=piano.begin(); iter != piano.end();iter++) { this->addChild(iter->second, 5); } for(iter=piano_black.begin(); iter != piano_black.end(); iter++) { this->addChild(iter->second, 6); } for(int i = PIANO_KEY,j = 0; i<38; i++) { Sprite* blackSmall; float positionX; if(i==37) { blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small_half.jpg"); Size size = blackSmall->getContentSize(); positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE-size.width/2; }else { blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small.jpg"); positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE; } blackSmall->setScale(SCALE); blackSmall->setAnchorPoint(Point(0.5, 0)); blackSmall->setPosition(Point( positionX, visibleSize.height - volumeSize.height - ABOVE_INTERVAL + smallSize.height*SCALE/2)); if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12) { j++; } this->addChild(blackSmall, 2); } //设置定时回调指定方法干活 auto director = Director::getInstance(); schedRound = director->getScheduler(); //创建退出单点触摸监听 EventListenerTouchOneByOne* listenerExit = EventListenerTouchOneByOne::create(); //开始触摸时回调onTouchBegan方法 listenerExit->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchExitBegan, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerExit, exit); //创建乐器单点触摸监听 EventListenerTouchOneByOne* listenerSelect = EventListenerTouchOneByOne::create(); listenerSelect->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerSelect->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchBegan, this); listenerSelect->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerSelect, instrument); //创建左箭头单点触摸监听 EventListenerTouchOneByOne* listenerLeft = EventListenerTouchOneByOne::create(); listenerLeft->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerLeft->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchLeftBegan, this); listenerLeft->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchLeftEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerLeft, left); //创建右箭头单点触摸监听 EventListenerTouchOneByOne* listenerRight= EventListenerTouchOneByOne::create(); listenerRight->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerRight->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchRightBegan, this); listenerRight->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchRightEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerRight, right); //创建单点触摸监听 EventListenerTouchOneByOne* listenerBack = EventListenerTouchOneByOne::create(); //设置下传触摸 listenerBack->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerBack->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchSelectBegan, this); listenerBack->onTouchMoved = CC_CALLBACK_2(PianoLayer::onTouchSelectMoved, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerBack, selectBack); //---------------------------------单点触控,用于选择钢琴键----------------------------- //创建单点触摸监听 EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create(); //开始触摸时回调onTouchBegan方法 listener->onTouchBegan = CC_CALLBACK_2(PianoLayer::onMyTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //---------------------------------多点触控,用于点击钢琴键------------------------------ //创建一个多点触摸监听 auto listenerTouch = EventListenerTouchAllAtOnce::create(); //开始触摸时回调onTouchBegan方法 listenerTouch->onTouchesBegan = CC_CALLBACK_2(PianoLayer::onMyTouchesBegan, this); //触摸移动时回调onTouchMoved方法 listenerTouch->onTouchesMoved = CC_CALLBACK_2(PianoLayer::onMyTouchesMoved, this); //触摸结束时回调onTouchEnded方法 listenerTouch->onTouchesEnded = CC_CALLBACK_2(PianoLayer::onMyTouchesEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, this); return true; }
StonePieceSprite* StonePieceSprite::initStonePieceWithType(BlockType type, b2World * world, StonePieceType piece, Point pos) { int width, height; String *stoneType; CCTexture2D *spriteTexture_; int level = (g_nBigLevel - 1) * 8 + g_nSmallLevel; if (type == Stone) { width = 15 * RATE; height = 15 * RATE; stoneType = String::createWithFormat("stoneAni_%d-hd.png", GLOBAL_MAPINFO[level - 1]); } else if (type == Item) { width = height = 15 * RATE; stoneType = String::createWithFormat("itemAni_%d-hd.png", GLOBAL_MAPINFO[level - 1]); } else if (type == Platform_Breakable) { width = 40 * RATE; height = 27 * RATE; stoneType = String::createWithFormat("FlyingPlatform_break-hd.png"); //stoneType = String::createWithFormat("itemAni_%d-hd.png", GLOBAL_MAPINFO[level - 1]); } SpriteBatchNode *parent = SpriteBatchNode::create(stoneType->getCString()); //[SpriteBatchNode batchNodeWithFile : stoneType); spriteTexture_ = parent->getTexture();//[parent texture); if (Sprite::initWithTexture(spriteTexture_, Rect(piece*width, 0, width, height))) { BodyInfo info; if (type == Platform_Breakable) { this->setTag(tag_pb); info.type = b2_staticBody; info.categoryBit = CATEGORY_SCENE; info.maskBit = ~(CATEGORY_SCENE); info.density = 0.1f; if (piece == piece1) this->setPosition(pos.x - this->getContentSize().width / 2 * G_SCALEX, pos.y - this->getContentSize().height / 2 * G_SCALEY); else this->setPosition(pos.x + this->getContentSize().width / 2 * G_SCALEX, pos.y - this->getContentSize().height / 2 * G_SCALEY); } else if (type == Platform_Down_1) { this->setPosition(pos.x, pos.y - this->getContentSize().height / 2 * G_SCALEY); info.type = b2_kinematicBody; info.categoryBit = CATEGORY_SCENE; info.maskBit = ~CATEGORY_SCENE; auto moveX1 = MoveBy::create(2.0f,Point(this->getPosition().x, this->getPosition().y - 50 * G_SCALEY)); auto moveX2 = MoveBy::create(2.0f,Point(this->getPosition().x, this->getPosition().y + 100 * G_SCALEY)); auto moveX3 = MoveBy::create(2.0f,Point(this->getPosition().x, this->getPosition().y - 100 * G_SCALEY)); auto action = RepeatForever::create( Sequence::create(moveX2, moveX3, NULL)); this->runAction( Sequence::create( moveX1, action, NULL)); } else if (type == Platform_Down_2) { this->setPosition(pos.x, pos.y - this->getContentSize().height / 2 * G_SCALEY); info.type = b2_kinematicBody; info.categoryBit = CATEGORY_SCENE; info.maskBit = ~CATEGORY_SCENE; auto moveX1 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y - 50 * G_SCALEY)); auto moveX2 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y + 100 * G_SCALEY)); auto moveX3 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y - 100 * G_SCALEY)); auto action = RepeatForever::create(Sequence::create(moveX2, moveX3, NULL)); this->runAction( Sequence::create( moveX1, action, NULL)); } else if (type == Platform) { this->setPosition(pos.x, pos.y - this->getContentSize().height / 2 * G_SCALEY); info.type = b2_kinematicBody; info.categoryBit = CATEGORY_SCENE; info.maskBit = ~CATEGORY_SCENE; auto moveX1 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y - 50 * G_SCALEY)); auto moveX2 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y + 100 * G_SCALEY)); auto moveX3 = MoveBy::create(2.0f, Point(this->getPosition().x, this->getPosition().y - 100 * G_SCALEY)); auto action = RepeatForever::create(Sequence::create(moveX2, moveX3, NULL)); this->runAction( Sequence::create( moveX1, action, NULL)); } else { info.type = b2_dynamicBody; this->setTag(tag_piece); info.friction = 0.5f; info.density = 1.0f; info.restitution = 0.5f; info.linearVelocityY = 5.0f; info.categoryBit = CATEGORY_SONTEPIECE; info.maskBit = CATEGORY_SCENE; this->setPosition(pos.x, pos.y); if (piece == piece1) { info.linearVelocityX = -1.0f; info.angularVelocity = 0.1f; } else { info.linearVelocityX = 1.0f; info.angularVelocity = -0.1f; } } info.userData = this; //[self attachBodyToWorld : info : world); this->attachBodyToWorld(info, world); } if (type != Platform_Breakable) msetScale(this, RATIO_XY); return this; }