要做个显示游戏玩法的功能,但是因为规则多,一个sprite显示不完。有几种解决办法,CCScrollView、listView,tableView。后面那两个还没研究,先试了下CCScrollView。
CCScrollView可以用来做游戏关卡选择。
#ifndef__Test_H_ #define__Test_H_ #include"cocos2d.h" #include"../extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; classTest:publicCCLayer,publicCCScrollViewDelegate { public: virtualboolinit(); staticCCScene*scene(); CREATE_FUNC(Test); virtualvoidscrollViewDidScroll(CCScrollView*view); virtualvoidscrollViewDidZoom(CCScrollView*view); }; #endif//!__Test_H_
#include"Test.h" CCScene*Test::scene() { CCScene*scene=CCScene::create(); Test*layer=Test::create(); scene->addChild(layer); returnscene; } boolTest::init() { if(!CCLayer::init()) returnfalse; CCSizesize=CCDirector::sharedDirector()->getWinSize(); CCLog("winSize=%d",size); CCScrollView*scroview=CCScrollView::create(); CCSprite*sp=CCSprite::create("HelloWorld.png");//添加两个精灵 sp->setPosition(ccp(size.width*0.5,size.height*0.5)); CCSprite*sp1=CCSprite::create("HelloWorld.png"); sp1->setPosition(ccp(size.width*0.5,size.height*1.5)); containLayer->addChild(sp); containLayer->addChild(sp1); containLayer->setContentSize(CCSizeMake(size.width,size.height)); containLayer->setAnchorPoint(CCPointZero); containLayer->setPosition(CCPointZero); scroview->setPosition(CCPointZero); scroview->setContentOffset(CCPointZero); containLayer->setContentSize(CCSizeMake(480,320*2));//设置滚动区域的大小 scroview->setViewSize(CCSizeMake(480,320)); //设置显示区域的大小 scroview->setContainer(containLayer);//设置需要滚动的内容 scroview->setTouchEnabled(true);//设置为false的话,可以自己实现触摸事件 scroview->setDelegate(this); scroview->setBounceable(false);//是否有弹跳效果,默认为true scroview->setDirection(kCCScrollViewDirectionVertical);//设置滚动的方向,有三种可以选择,水平,垂直呀什么的 this->addChild(scroview); setTouchEnabled(false); setTouchMode(kCCTouchesOneByOne); returntrue; } voidTest::scrollViewDidScroll(CCScrollView*view) { CCLog("fssdf"); } voidTest::scrollViewDidZoom(CCScrollView*view) { CCLog("fsfsfsdfsdf"); }原文链接:https://www.f2er.com/cocos2dx/338751.html