您好,登錄后才能下訂單哦!
要做個顯示游戲玩法的功能,但是因為規則多,一個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"); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。