cocos2d-x游戏开发之二

cocos2d-x游戏开发之滑动选择关卡界面




直接贴代码吧


//
//  ChapterSelect.cpp
//  LSCPractice
//
//  Created by Dagobert on 13-6-9.
//
//

#include "MyMenu.h"

#include "Constants.h"
#include "ChapterSelect.h"
#include "SimpleAudioEngine.h"
#include "cocos-ext.h"
#include "ShakeBellSecene.h"
#include "BellSecene.h"
#include "ToyMarimbaScene.h"
#include "AnimalPianoScene.h"
#include "PlayFireworksSecene.h"
#include "PlayfirecrackerSecene.h"
#include "BambooClapperScene.h"
#include "RattleDrumScene.h"
#include "BubblePoppingSecene.h"
#include "WhackingMoleSecene.h"

using namespace cocos2d;
using namespace CocosDenshion;

USING_NS_CC;
USING_NS_CC_EXT;

CCScene* ChapterSelect::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ChapterSelect *layer = ChapterSelect::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

void ChapterSelect::onEnter()
{
  
    CCLayer::onEnter();
  
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
}

// on "init" you need to initialize your instance
bool ChapterSelect::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
//    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
 
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "button_back-hd.png",
                                                          "button_back-hd.png",
                                                          this,
                                                          menu_selector(ChapterSelect::BackToRootScene) );
    pCloseItem->setPosition( ccp(size.width/2+440 , size.height/2+230) );
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);
 
    // add "HelloWorld" splash screen"
    CCSprite* bg_pSprite = CCSprite::create("bg_common-iphone5.png");
    
    // position the sprite on the center of the screen
    bg_pSprite->setPosition( ccp(size.width/2, size.height/2) );
    
    // add the sprite as a child to this layer
    this->addChild(bg_pSprite, 0);
    
    //添加选择界面下面的文字说明”swipe to choose other toys“
    
    CCLabelTTF* pLabel_select = CCLabelTTF::create("swipe to choose other toys", "ChalkboardSE-Bold", 34);
    
    pLabel_select->setPosition( ccp(size.width / 2, size.height / 2-240) );
    
    pLabel_select->setColor(ccc3(16,174,231));
    
    this->addChild(pLabel_select);
    
    std::cout<<"*";
 
 
    //摇铃界面
    scrollview_ = CCScrollView::create();//创建一个scrollVeiw
  
    CCLayer *continerLay_ = CCLayer::create();//  scrollview容器
    
//    ccColor4B color = ccc4(238, 118, 0, 255);
//    CCLayerColor *yaolingLayer_ = CCLayerColor::create(color);
    
    CCLayer *yaolingLayer_ = CCLayer::create();
    
    CCLabelTTF* pLabel_1 = CCLabelTTF::create("Shake Bell", "ChalkboardSE-Bold", 34);
    
    pLabel_1->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_1->setColor(ccc3(16,174,231));
    
    yaolingLayer_->addChild(pLabel_1);
 
    menuItem1_ = CCMenuItemImage::create(
                                                          "mini_bg_shakebell-hd.png",
                                                          "mini_bg_shakebell-hd.png",
                                                          this,
                                                            menu_selector(ChapterSelect::DidselelctToy));
  
    menuItem1_->setTag(1);

    menuItem1_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu *pMenu1_ = CCMenu::create(menuItem1_, NULL);
  
    pMenu1_->setPosition( CCPointZero );
    
    pMenu1_->setTouchPriority(1);
    
    yaolingLayer_->addChild(pMenu1_, 1);
 
    yaolingLayer_->setPosition(ccp(1136*0,0));
    
    continerLay_->addChild(yaolingLayer_);
    
    //铃铛界面
    
//    ccColor4B color1 = ccc4(238, 85, 0, 255);
//    CCLayerColor *lingdangLayer_ = CCLayerColor::create(color1);
    CCLayer *lingdangLayer_ = CCLayer::create();
    
    CCLabelTTF* pLabel_2 = CCLabelTTF::create("Toy Bell", "ChalkboardSE-Bold", 34);
    
    pLabel_2->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_2->setColor(ccc3(16,174,231));
    
    lingdangLayer_->addChild(pLabel_2);
    
    menuItem2_ = CCMenuItemImage::create(
                                                        "mini_bg_bell-hd.png",
                                                        "mini_bg_bell-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem2_->setTag(2);
 
    menuItem2_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu2_ = CCMenu::create(menuItem2_, NULL);
    
    pMenu2_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu2_->setTouchPriority(1);
    
    lingdangLayer_->addChild(pMenu2_, 1);
    
    lingdangLayer_ ->setPosition(ccp(568*1,0));
    
    continerLay_->addChild(lingdangLayer_);

    //木琴界面
    CCLayer *muqinLayer_ = CCLayer::create();
  
    CCLabelTTF* pLabel_3 = CCLabelTTF::create("Toy Marimba", "ChalkboardSE-Bold", 34);
    
    pLabel_3->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_3->setColor(ccc3(16,174,231));
    
    muqinLayer_->addChild(pLabel_3);
    
    menuItem3_ = CCMenuItemImage::create(
                                                        "mini_bg_marimba-hd.png",
                                                        "mini_bg_marimba-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem3_->setTag(3);
    
    menuItem3_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu3_ = CCMenu::create(menuItem3_, NULL);
    
    pMenu3_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu3_->setTouchPriority(1);
    
    muqinLayer_->addChild(pMenu3_, 1);
   
    muqinLayer_->setPosition(ccp(568*2,0));
    
    continerLay_->addChild(muqinLayer_);
    
    //拨浪鼓
    CCLayer *bolangguLayer_ = CCLayer::create();
  
    CCLabelTTF* pLabel_4 = CCLabelTTF::create("Rattle Drum", "ChalkboardSE-Bold", 34);
    
    pLabel_4->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_4->setColor(ccc3(16,174,231));
    
    bolangguLayer_->addChild(pLabel_4);
    
    menuItem4_ = CCMenuItemImage::create(
                                                        "mini_bg_rattledrum-hd.png",
                                                        "mini_bg_rattledrum-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem4_->setTag(4);
    
    menuItem4_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu4_ = CCMenu::create(menuItem4_, NULL);
    
    pMenu4_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu4_->setTouchPriority(1);
    
    bolangguLayer_->addChild(pMenu4_, 1);
    
    bolangguLayer_->setPosition(ccp(568*3,0));
    
    continerLay_->addChild(bolangguLayer_);
    
    //卡通钢琴
    CCLayer *gangqinLayer_ = CCLayer::create();
    
    CCLabelTTF* pLabel_5 = CCLabelTTF::create("Animal Piano", "ChalkboardSE-Bold", 34);
    
    pLabel_5->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_5->setColor(ccc3(16,174,231));
    
    gangqinLayer_->addChild(pLabel_5);
    
    menuItem5_ = CCMenuItemImage::create(
                                                        "mini_bg_cartoonphone-hd.png",
                                                        "mini_bg_cartoonphone-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem5_->setTag(5);
    
    menuItem5_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu5_ = CCMenu::create(menuItem5_, NULL);
    
    pMenu5_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu5_->setTouchPriority(1);
    
    gangqinLayer_->addChild(pMenu5_, 1);
    
    gangqinLayer_->setPosition(ccp(568*4,0));
    
    continerLay_->addChild(gangqinLayer_);
    
    //快板
    CCLayer *kuaibanLayer_ = CCLayer::create();
  
    CCLabelTTF* pLabel_6 = CCLabelTTF::create("Bamboo Clapper", "ChalkboardSE-Bold", 34);
    
    pLabel_6->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_6->setColor(ccc3(16,174,231));
    
    kuaibanLayer_->addChild(pLabel_6);
    
    menuItem6_ = CCMenuItemImage::create(
                                                        "mini_bg_bambooclapper-hd.png",
                                                        "mini_bg_bambooclapper-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem6_->setTag(6);
    
    menuItem6_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu6_ = CCMenu::create(menuItem6_, NULL);
    
    pMenu6_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu6_->setTouchPriority(1);
    
    kuaibanLayer_->addChild(pMenu6_, 1);
    
    kuaibanLayer_->setPosition(ccp(568*5,0));
    
    continerLay_->addChild(kuaibanLayer_);
    
    //挤压泡泡
    
    CCLayer *paopaoLayer_ = CCLayer::create();
  
    CCLabelTTF* pLabel_7 = CCLabelTTF::create("Bubble Popping", "ChalkboardSE-Bold", 34);
    
    pLabel_7->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_7->setColor(ccc3(16,174,231));
    
    paopaoLayer_->addChild(pLabel_7);
    
    menuItem7_ = CCMenuItemImage::create(
                                                        "mini_bg_bubble-hd.png",
                                                        "mini_bg_bubble-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem7_->setTag(7);
    
    menuItem7_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu7_ = CCMenu::create(menuItem7_, NULL);
    
    pMenu7_->setPosition( CCPointZero );
    
    //设置触摸级别
    pMenu7_->setTouchPriority(1);
    
    paopaoLayer_->addChild(pMenu7_, 1);
    
    paopaoLayer_->setPosition(ccp(568*6,0));
    
    continerLay_->addChild(paopaoLayer_);
    
    //打地鼠
    CCLayer *dishuLayer_ = CCLayer::create();
     
    CCLabelTTF* pLabel_8 = CCLabelTTF::create("Whacking Mole", "ChalkboardSE-Bold", 34);
    
    pLabel_8->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_8->setColor(ccc3(16,174,231));
    
    dishuLayer_->addChild(pLabel_8);
    
    menuItem8_ = CCMenuItemImage::create(
                                                        "mini_bg_whackingmole-hd.png",
                                                        "mini_bg_whackingmole-hd.png",
                                                        this,
                                                        menu_selector(ChapterSelect::DidselelctToy));
    menuItem8_->setTag(8);
    
    menuItem8_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu8_ = CCMenu::create(menuItem8_, NULL);
    
    pMenu8_->setPosition( CCPointZero );
    
    dishuLayer_->addChild(pMenu8_, 1);
    
    //设置触摸级别
    pMenu8_->setTouchPriority(1);
    
    dishuLayer_->setPosition(ccp(568*7,0));
    
    continerLay_->addChild(dishuLayer_);
    
    //新年
    CCLayer *xinianLayer_ = CCLayer::create();
    
    CCLabelTTF* pLabel_9 = CCLabelTTF::create("Play firecracker", "ChalkboardSE-Bold", 34);
    
    pLabel_9->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_9->setColor(ccc3(16,174,231));
    
    xinianLayer_->addChild(pLabel_9);
    
    CCMenuItemImage *menuItem9_ = CCMenuItemImage::create(
                                                          "mini_bg_firecracker-hd.png",
                                                          "mini_bg_firecracker-hd.png",
                                                          this,
                                                          menu_selector(ChapterSelect::DidselelctToy));
    menuItem9_->setTag(9);
    
    menuItem9_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu9_ = CCMenu::create(menuItem9_, NULL);
    
    pMenu9_->setPosition( CCPointZero );
    
    xinianLayer_->addChild(pMenu9_, 1);
    
    xinianLayer_->setPosition(ccp(568*8,0));
    
    continerLay_->addChild(xinianLayer_);
    
    //粒子界面
    CCLayer *liziLayer_ = CCLayer::create();
     
    CCLabelTTF* pLabel_10 = CCLabelTTF::create("Play Fireworks", "ChalkboardSE-Bold", 34);
    
    pLabel_10->setPosition( ccp(size.width / 2, size.height / 2+200) );
    
    pLabel_10->setColor(ccc3(16,174,231));
    
    liziLayer_->addChild(pLabel_10);
    
    CCMenuItemImage *menuItem10_ = CCMenuItemImage::create(
                                                          "mini_bg_fireworks-hd.png",
                                                          "mini_bg_fireworks-hd.png",
                                                          this,
                                                          menu_selector(ChapterSelect::DidselelctToy));
    menuItem10_->setTag(10);
    
    menuItem10_->setPosition(ccp(size.width / 2, size.height / 2));
    
    CCMenu* pMenu10_ = CCMenu::create(menuItem10_, NULL);
    
    pMenu10_->setPosition( CCPointZero );
    
    liziLayer_->addChild(pMenu10_, 1);
    
    liziLayer_->setPosition(ccp(568*9,0));
    
    continerLay_->addChild(liziLayer_);
    
    
  
    continerLay_->setAnchorPoint(CCPointZero);
    
    continerLay_->setPosition(CCPointZero);
    
    scrollview_->setAnchorPoint(CCPointZero);
    
    scrollview_->setPosition(CCPointZero);
                           
    //设置显示区域
    
    scrollview_->setViewSize(CCSizeMake(1136, 640));
    
    scrollview_->setContentOffset(CCPointZero);
    
    continerLay_->setContentSize(CCSizeMake(568+590*10, 640));
    
    //  显示滑动的区域大小 scrollview的实际大小
    
    scrollview_->setContentSize(CCSizeMake(1136*1.5, 640));//设置scrollview区域的大小
    
    scrollview_->setContainer(continerLay_);
    
    //因为要自己实现触摸消息,所以这里设为false//设置需要滚动的内容
    
    scrollview_->setTouchEnabled(true);
    
    scrollview_->setDirection(kCCScrollViewDirectionHorizontal);  //设置滚动的方向,有三种可以选择
    
    scrollview_->setDelegate(this);
    
 
    this->addChild(scrollview_);
    
    return true;
}
 
//滑动的时候调用
void ChapterSelect::scrollViewDidScroll(CCScrollView* view)
{
    
    int x = view->getContentOffset().x;
    
    CCLog("滑动区域的x    ===   %d",x);
    
    int offset = (int) x % 568;
    
    CCLog("测试    滚动的范围   ===   %d",offset);
    
    
    std::cout<<"滑动";
}
//缩放的时候调用
void ChapterSelect::scrollViewDidZoom(CCScrollView* view)
{
    std::cout<<"缩放";
}

//设置Scrollview自动调整大小

 

bool ChapterSelect::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

{
    
    return true;
    
}



void ChapterSelect::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

{
    
    CCLOG("move");
  
}



void ChapterSelect::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

{
    
    //这里获取的是设备的UIView坐标,原点在左上角,X向右侧递增,Y向下递增。
    CCPoint touchLoaction = pTouch->getLocationInView();
    
    //获取节点坐标 节点坐标的计算同GL坐标,它以所在节点的左下角为原点,X向右递增,Y向上递增
    touchLoaction = CCDirector::sharedDirector()->convertToGL(touchLoaction);
    
    //获取锚点坐标
    //一个节点,所有的变换都是基于锚点,比如平移,旋转,缩放等。可以通过函数setAnchorPoint(CCPoint var);来改变锚点。cocos2d-x里节点默认的锚点是中心点,即等同于setArchorPoint(ccp(0.5, 0.5));其中(0,0)代表左下角,(1,1)代表右上角。
    touchLoaction = this->convertToNodeSpace(touchLoaction);
    
    
    CCLog("触摸点的坐标   X === %f   Y === %f",touchLoaction.x,touchLoaction.y);
    
    
    
    adjustScrollView(touchLoaction);
    
}



void ChapterSelect::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

{
    
//     adjustScrollView(touchLoaction);
    
}
 
void ChapterSelect::adjustScrollView(CCPoint touchLoaction)

{
     
    // 关闭CCScrollView中的自调整
    
    scrollview_->unscheduleAllSelectors();
    
    int x = scrollview_->getContentOffset().x;
    
    CCLOG("offset=%d",x);
    
    int offset = (int) x % 568;
    
    CCLog("滚动的范围   ===   %d",offset);
    
    if(offset == 0)
    {
        //判断点击坐标是否在精
        if(menuItem2_->boundingBox().containsPoint(touchLoaction))
        {
            
            if(abs(x)==568*0)
            {
               ChapterSelect::DidselelctToy(menuItem1_);
            }
            
            else if(abs(x)==568*1)
            {
                ChapterSelect::DidselelctToy(menuItem2_);
            }
            
            else if(abs(x)==568*2)
            {
                ChapterSelect::DidselelctToy(menuItem3_);
            }
            
            else if(abs(x)==568*3)
            {
                ChapterSelect::DidselelctToy(menuItem4_);
            }
            
            else if(abs(x)==568*4)
            {
                ChapterSelect::DidselelctToy(menuItem5_);
            }
            
            else if(abs(x)==568*5)
            {
                ChapterSelect::DidselelctToy(menuItem6_);
            }
            
            else if(abs(x)==568*6)
            {
                ChapterSelect::DidselelctToy(menuItem7_);
            }
            
            else if(abs(x)==568*7)
            {
                ChapterSelect::DidselelctToy(menuItem8_);
            }
            
            else
            {
                CCLog("nothing");
            }
      
        }
 
    }
    
    else
        
    {
        
    }
    
    // 调整位置
    
    CCPoint adjustPos;
    
    // 调整动画时间
    
    float adjustAnimDelay;
    
    //   向右滑动是正向左滑动是负
    
    if (offset < -284) {
        
        // 计算下一页位置,时间
        
        adjustPos = ccpSub(scrollview_->getContentOffset(), ccp(568 + offset, 0));
        
        adjustAnimDelay = (float) (1136 + offset) / 800;
        
    }
    
    else {
        
        // 计算当前页位置,时间
        
        adjustPos = ccpSub(scrollview_->getContentOffset(), ccp(offset, 0));
        
        // 这里要取绝对值,否则在第一页往左翻动的时,保证adjustAnimDelay为正数
        
        adjustAnimDelay = (float) abs(offset) / 1000;
        
    }
  
    // 调整位置
    
    scrollview_->setContentOffsetInDuration(adjustPos, adjustAnimDelay);
  
}
 
void ChapterSelect::BackToRootScene(CCObject* pSender)
{
    
    CCDirector::sharedDirector()->popScene();
    
}

//选择哪个玩具
#pragma -mark- 选择关卡

void ChapterSelect::DidselelctToy(CCObject* pSender)
{
    CCMenuItemImage *idex =  (CCMenuItemImage*)pSender;
    
    int tag_ = idex->getTag();
    
    CCLog("%d",tag_);
 
    switch (tag_) {
        case kSceneTypeShakeBell:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, ShakeBell::scene()));
            break;
            
        case kSceneTypeBell:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, Bell::scene()));
            break;
            
        case kSceneTypeToyMarimba:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, ToyMarimba::scene()));
            break;
            
        case kSceneTypeRattleDrm:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, RattleDrum::scene()));
            break;
            
        case kSceneTypeAnimalPiano:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, AnimalPiano::scene()));
            break;
            
        case kSceneTypeBambooClapper:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, BambooClapper::scene()));
            break;
            
        case kSceneTypeBubbleTouch:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, BubblePopping::scene()));
            break;
            
        case kSceneTypeMolePoping:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, WhackingMole::scene()));
            break;
            
        case kSceneTypeFireCrack:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, PlayFirecracker::scene()));
            break;
            
        case kSceneTypeFireworks:
            CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, PlayFireworks::scene()));
            break;
            
        default:
            break;
    }
    
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

这个是自己google写出来的 ,总感觉有点不流畅,大牛有这方面的demo或例子还望赏赐一份,如果还有其他指教还望多多交流,交流中的菜鸟变老鸟      谢谢  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值