要做个显示游戏玩法的功能,但是因为规则多,一个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; class Test:public CCLayer,public CCScrollViewDelegate { public: virtual bool init(); static CCScene *scene(); CREATE_FUNC(Test); virtual void scrollViewDidScroll(CCScrollView* view); virtual void scrollViewDidZoom(CCScrollView* view); }; #endif // !__Test_H_
#include "Test.h" CCScene *Test::scene() { CCScene *scene = CCScene::create(); Test *layer = Test::create(); scene->addChild(layer); return scene; } bool Test::init() { if (!CCLayer::init()) return false; CCSize size = 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); return true; } void Test::scrollViewDidScroll(CCScrollView* view) { CCLog("fssdf"); } void Test::scrollViewDidZoom(CCScrollView* view) { CCLog("fsfsfsdfsdf"); }
时间: 2024-10-01 04:25:18