自己看回之前寫的代碼都有點吃力,早期代碼比較亂,很多東西都沒有封裝起來,但是一些功能性的東西還是有的,可以參考一下。
實例代碼:
ScrollViewTest2.h
#ifndef _ScrollViewTest2_H_
#define _ScrollViewTest2_H_
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
using namespace cocos2d;
using namespace cocos2d::extension;
extern int giftTag;
class ScrollViewTest2 :public CCLayer, public CCScrollViewDelegate
{
public:
static cocos2d::CCScene* createScene();//獲取歡迎畫面的CCScene
virtual bool init();
//scroll 委託
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);
virtual void scrollViewMoveOver(CCScrollView* view);
CCNode* create9Sprite(float pWith, float pHeight);//創建9妹精靈
virtual bool ccTouchBegan(CCTouch *CCTouch, CCEvent *pCCEvent);
virtual void ccTouchMoved(CCTouch *CCTouch, CCEvent *pCCEvent);
virtual void ccTouchEnded(CCTouch *CCTouch, CCEvent *pCCEvent);
CCLabelTTF* buy_information;
CCLabelTTF* buy_information1;
CCLabelTTF* buy_price;
//CCSprite*xuanding;//勾選
void getTestTag(int tag);//獲取道具描述的tag
void buyClick(CCObject*sender);//購買按鈕回調
void chooseClick(CCObject*sender);//選擇按鈕回調
CCSize winSize;
void homeClick(CCObject* sender);//返回主界面
CCLabelAtlas* goldNum;//金幣數量
virtual void update(float dt);//更新金幣數量
CCLabelAtlas* magnetNum;//磁鐵數量
CCLabelAtlas* shieldNum;//護盾數量
CCLabelAtlas* rocketNum;//火箭數量
CCLabelAtlas* bloodNum;//血瓶數量
CREATE_FUNC(ScrollViewTest2);
private:
//根據手勢滑動的距離和方向滾動圖層
void adjustCCScrollView(float offset);
private:
//存放所有圖片
CCArray* m_spVec;
//當前的CCScrollView
extension::CCScrollView *m_CCScrollView;
//CCTouchBegen時的觸摸位置
CCPoint m_CCTouchCCPoint;
//當前是第幾張圖片
int m_currentPage;
float thelastPos;
float xOffSet;//存儲偏離量
CCScrollView* scrollView;
int x; //CCScrollView的當前橫座標
private:
};
#endif
ScrollViewTest2.cpp
#include "ScrollViewTest2.h"
#include "StartGame.h"
#include "DataManager.h"
#include "BaseUtils.h"
#include "Hero.h"
#include "Goldlayer.h"
#include "JNIUtil.h"
USING_NS_CC;
/*
* 精靈真正的位置為:spr->getPositionX + 偏移量
*/
int giftTag;
int sprIdx;
std::string magnetStr;
std::string shieldStr;
std::string rocketStr;
std::string bloodStr;
//越向右偏移量越小
#define MIDDLE_POSITION 240
#define FIRST_OFFSET 160 //第一個spr置於中間的偏移量
#define LAST_OFFSET -1040 //最後一個spr置於中間的偏移量,計算:第一個減掉((spr總數-1) * spr寬度) 160-(13-1)*100=-1040
#define CELL_WIDTH 100
#define BIG_SCALE 3.0f
CCScene* ScrollViewTest2::createScene(){
CCLayer* layer = ScrollViewTest2::create();
CCScene* scene = CCScene::create();
scene->addChild(layer);
return scene;
}
void ScrollViewTest2::update(float dt){
countDownTime -= dt;
ONLINE_GIFT_TIME += dt;
int goldNum_xda = DataManager::getInstance()->getGoldNum();
std::string goldStr;
goldStr = CCString::createWithFormat("%d", goldNum_xda)->getCString();
goldNum->setString(goldStr.c_str());
//磁鐵數量
int magnetNum_xda = DataManager::getInstance()->getMagnetNum();
//std::string magnetStr;
magnetStr = CCString::createWithFormat("%d", magnetNum_xda)->getCString();
magnetNum->setString(magnetStr.c_str());
//護盾數量
int shieldNum_xda = DataManager::getInstance()->getShieldNum();
shieldStr = CCString::createWithFormat("%d", shieldNum_xda)->getCString();
shieldNum->setString(shieldStr.c_str());
//火箭數量
int rocketNum_xda = DataManager::getInstance()->getRocketNum();
rocketStr = CCString::createWithFormat("%d", rocketNum_xda)->getCString();
rocketNum->setString(rocketStr.c_str());
//血瓶數量
int bloodNum_xda = DataManager::getInstance()->getBloodNum();
bloodStr = CCString::createWithFormat("%d", bloodNum_xda)->getCString();
bloodNum->setString(bloodStr.c_str());
}
bool ScrollViewTest2::init()
{
if (!CCLayer::init())
{
return false;
}
scheduleUpdate();
CCLOG("11111111111111111");
//商城界面
winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite*shopLayer = CCSprite::create("beijing2_xda.png");
shopLayer->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(shopLayer);
//左上角商城圖標
CCSprite*shopPictuer = CCSprite::create("shop.png");
shopPictuer->setPosition(ccp(50, 280));
shopPictuer->setScale(0.8f);
this->addChild(shopPictuer);
//購買按鈕
CCMenuItemImage* buy_btn = CCMenuItemImage::create("buy.png", "buy.png", this, menu_selector(ScrollViewTest2::buyClick));
buy_btn->setPosition(ccp(winSize.width /2+20, 50));
buy_btn->setScale(0.7f);
CCMenu*shopMenu_2 = CCMenu::create(buy_btn, NULL);
shopMenu_2->setPosition(CCPointZero);
this->addChild(shopMenu_2);
shopMenu_2->setTag(216);
CCLOG("2");
//選擇按鈕按鈕
CCMenuItemImage* choose_btn = CCMenuItemImage::create("choose.png", "choose.png", this, menu_selector(ScrollViewTest2::chooseClick));
choose_btn->setPosition(ccp(winSize.width / 2+20, 50));
choose_btn->setScale(0.7f);
CCMenu*shopMenu_3 = CCMenu::create(choose_btn, NULL);
shopMenu_3->setPosition(CCPointZero);
this->addChild(shopMenu_3,1);
shopMenu_3->setVisible(false);
shopMenu_3->setTag(710);
//返回主頁按鈕
CCMenuItemImage* home_btn = CCMenuItemImage::create("home_btn.png", "home_btn.png", this, menu_selector(ScrollViewTest2::homeClick));
home_btn->setPosition(ccp(winSize.width - 40, 50));
home_btn->setScale(0.7f);
CCMenu*shopMenu_1 = CCMenu::create(home_btn, NULL);
shopMenu_1->setPosition(CCPointZero);
this->addChild(shopMenu_1);
//道具描述
buy_information = CCLabelTTF::create(GBK_TO_UTF8("金幣磁鐵").c_str(), "Arial", 24);
buy_information->setColor(ccYELLOW);
buy_information->setPosition(ccp(winSize.width / 2+20, 250));
this->addChild(buy_information);
buy_information->setTag(1212);
buy_information1= CCLabelTTF::create(GBK_TO_UTF8("不會加速但金幣滾滾來").c_str(), "Arial", 18);
//buy_information1->setColor(ccYELLOW);
buy_information1->setPosition(ccp(winSize.width / 2+20, 220));
this->addChild(buy_information1);
buy_information1->setTag(121295);
//道具價格
buy_price = CCLabelTTF::create(GBK_TO_UTF8("20.00RMB").c_str(), "Arial", 18);
buy_price->setColor(ccYELLOW);
buy_price->setPosition(ccp(winSize.width / 2+20, 80));
this->addChild(buy_price);
buy_price->setTag(8080);
CCLOG("31111111111111111");
//金幣
CCSprite * gold_png = CCSprite::create("gold.png");
gold_png->setScale(0.7f);
gold_png->setPosition(ccp(winSize.width - 30, 280));
this->addChild(gold_png);
goldNum = CCLabelAtlas::create("99999", "num1.png", 12, 17, 48);
goldNum->setPosition(ccp(winSize.width - 100, 270));
goldNum->setColor(ccYELLOW);
goldNum->setAnchorPoint(CCPointZero);
this->addChild(goldNum);
//磁鐵數量
magnetNum = CCLabelAtlas::create("99999", "num1.png", 12, 17, 48);
magnetNum->setPosition(ccp(winSize.width/2+60, 70));
magnetNum->setColor(ccYELLOW);
magnetNum->setAnchorPoint(CCPointZero);
this->addChild(magnetNum);
magnetNum->setTag(1007);
//護盾數量
shieldNum = CCLabelAtlas::create("99999", "num1.png", 12, 17, 48);
shieldNum->setPosition(ccp(winSize.width / 2 + 60, 70));
shieldNum->setColor(ccYELLOW);
shieldNum->setAnchorPoint(CCPointZero);
this->addChild(shieldNum);
//火箭數量
rocketNum = CCLabelAtlas::create("99999", "num1.png", 12, 17, 48);
rocketNum->setPosition(ccp(winSize.width / 2 + 60, 70));
rocketNum->setColor(ccYELLOW);
rocketNum->setAnchorPoint(CCPointZero);
this->addChild(rocketNum);
//血瓶數量
bloodNum = CCLabelAtlas::create("99999", "num1.png", 12, 17, 48);
bloodNum->setPosition(ccp(winSize.width / 2 + 60, 70));
bloodNum->setColor(ccYELLOW);
bloodNum->setAnchorPoint(CCPointZero);
this->addChild(bloodNum);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//首先創建CCScrollView
CCLayer* scroll_layer = CCLayer::create();//創建CCScrollView中的容器層
scroll_layer->setPosition(CCPoint(0, 0));
scroll_layer->setAnchorPoint(CCPoint(0, 0));
scroll_layer->setContentSize(winSize);//設置容器層大小為(CELL_WIDTH0,300)
scroll_layer->setTag(101);
scrollView = CCScrollView::create(CCSize(winSize.width, 300), scroll_layer);//創建scrollView,顯示窗口大小為(400,300)
scrollView->setDelegate(this);//設置委託
scrollView->setDirection(kCCScrollViewDirectionHorizontal);//設置滾動方向為水平
scrollView->setTouchEnabled(true);
scrollView->setPosition(CCPoint(0, 50));
scrollView->setBounceable(true);
scrollView->setTag(100);
this->addChild(scrollView, 2);
sp_vec = CCArray::createWithCapacity(100);
sp_vec->retain();
//增加商品
for (int i = 1; i < 14; i++){
char tmp[256];
sprintf(tmp, "p%d.png", i);
CCSprite* spr = CCSprite::create(tmp);
//spr->setAnchorCCPoint(CCPoint(0, 0));
spr->setPosition(CCPoint(i*CELL_WIDTH, 100));//沒設置錨點,則默認在正中央
spr->setTag(i);
scroll_layer->addChild(spr,2);
sp_vec->addObject(spr);
CCSprite* xuanding = CCSprite::create("gouxuan.png");//人物的勾選
xuanding->setPosition(ccp(winSize.width / 2 -200, -10));
xuanding->setVisible(false);
xuanding->setScale(0.5f);
spr->addChild(xuanding, 3);//放到道具圖標裡面
xuanding->setTag(i);
//if (DataManager::getHeroNum()+5==sprIdx)
//{
CCLOG("411111111111111111");
if (DataManager::getHeroNum() + 5 == i)
{
spr->getChildByTag(i)->setVisible(true);
}
else
{
spr->getChildByTag(i)->setVisible(false);
}
//}
CCSprite* xuanding_cj = CCSprite::create("gouxuan.png");//場景的勾選
xuanding_cj->setPosition(ccp(winSize.width / 2 - 200, -10));
xuanding_cj->setVisible(false);
xuanding_cj->setScale(0.5f);
spr->addChild(xuanding_cj, 3);//放到道具圖標裡面
xuanding_cj->setTag(i+100);
if (DataManager::getSceneNum() + 11 == i)
{
spr->getChildByTag(i+100)->setVisible(true);
}
else
{
spr->getChildByTag(i+100)->setVisible(false);
}
}
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, true, this);
//auto listener = EventListenerTouchOneByOne::create();
////設置是否想下傳遞觸摸
//listener->setSwallowTouches(true);
//listener->onTouchBegan = CC_CALLBACK_2(ScrollViewTest2::onTouchBegan, this);
//listener->onTouchMoved = CC_CALLBACK_2(ScrollViewTest2::onTouchMoved, this);
//listener->onTouchEnded = CC_CALLBACK_2(ScrollViewTest2::onTouchEnded, this);
////將觸摸監聽添加到eventDispacher中去
//_eventDispatcher->addEventListenerWithCCSceneGraphPriority(listener, this);
////註冊觸摸結束
//最開始把第一個商品偏移到正中央
scrollView->setContentOffset(CCPoint(FIRST_OFFSET, 0), false);
CCLOG("51111111111111111");
return true;
}
void ScrollViewTest2::getTestTag(int tag){
//getChildByTag(1212);
CCLabelTTF*test = (CCLabelTTF*)getChildByTag(1212);
CCLabelTTF*test1 = (CCLabelTTF*)getChildByTag(8080);
CCLabelTTF*test2 = (CCLabelTTF*)getChildByTag(121295);
//char tmp1[256];
////sprintf(tmp1, GBK_TO_UTF8("當前持有:%s").c_str(), magnetStr.c_str());//拼接字符串 ,magnetStr.c_str() 表示數量
//sprintf(tmp1, GBK_TO_UTF8("當前持有:%s").c_str());
//char tmp2[256];
//sprintf(tmp2, GBK_TO_UTF8("當前持有:%s").c_str());
//char tmp3[256];
//sprintf(tmp3, GBK_TO_UTF8("當前持有:%s").c_str());
//char tmp4[256];
//sprintf(tmp4, GBK_TO_UTF8("當前持有:%s").c_str());
switch (tag)
{
case 1:
test->setString(GBK_TO_UTF8("金幣磁鐵").c_str());
test1->setString(GBK_TO_UTF8("當前持有:").c_str());
test2->setString(GBK_TO_UTF8("不會加速但金幣滾滾來").c_str());
magnetNum->setVisible(true);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 2:
test->setString(GBK_TO_UTF8("護盾").c_str());
test1->setString(GBK_TO_UTF8("當前持有:").c_str());
test2->setString(GBK_TO_UTF8("無視障礙10秒").c_str());
shieldNum->setVisible(true);
magnetNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 3:
test->setString(GBK_TO_UTF8("火箭衝刺").c_str());
test1->setString(GBK_TO_UTF8("當前持有:").c_str());
test2->setString(GBK_TO_UTF8("進行加速10秒!").c_str());
rocketNum->setVisible(true);
shieldNum->setVisible(false);
magnetNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 4:
test->setString(GBK_TO_UTF8("加血道具").c_str());
test1->setString(GBK_TO_UTF8("當前持有:").c_str());
test2->setString(GBK_TO_UTF8("補滿血量!").c_str());
bloodNum->setVisible(true);
shieldNum->setVisible(false);
magnetNum->setVisible(false);
rocketNum->setVisible(false);
break;
case 5:
test->setString(GBK_TO_UTF8("小摔哥").c_str());
test1->setString(GBK_TO_UTF8(" ").c_str());
test2->setString(GBK_TO_UTF8("穿戴正常的小摔哥!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 6:
test->setString(GBK_TO_UTF8("兵馬俑小摔哥\n").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("秦始皇時代可流行了!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 7:
test->setString(GBK_TO_UTF8("財神皮膚").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("喜氣財氣!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 8:
test->setString(GBK_TO_UTF8("熊貓皮膚").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("竹子吃多了!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 9:
test->setString(GBK_TO_UTF8("金幣禮包").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("獲得金幣2000!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 10:
test->setString(GBK_TO_UTF8("金幣禮包").c_str());
test1->setString(GBK_TO_UTF8("10.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("獲得金幣1000!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 11:
test->setString(GBK_TO_UTF8("企鵝峰").c_str());
test1->setString(GBK_TO_UTF8(" ").c_str());
test2->setString(GBK_TO_UTF8("最原始的雪崩逃生體驗!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 12:
test->setString(GBK_TO_UTF8("咆哮山").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("開啟新場景,來挑戰吧!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
case 13:
test->setString(GBK_TO_UTF8("鷹嘯崖").c_str());
test1->setString(GBK_TO_UTF8("20.00.RMB").c_str());
test2->setString(GBK_TO_UTF8("開啟新場景,來挑戰吧!").c_str());
magnetNum->setVisible(false);
shieldNum->setVisible(false);
rocketNum->setVisible(false);
bloodNum->setVisible(false);
break;
/* case 14:
test->setString(GBK_TO_UTF8("").c_str());
test1->setString(GBK_TO_UTF8("").c_str());
test2->setString(GBK_TO_UTF8("").c_str());
break;
case 15:
test->setString(GBK_TO_UTF8("").c_str());
test1->setString(GBK_TO_UTF8("").c_str());
test2->setString(GBK_TO_UTF8("").c_str());
break;*/
default:
break;
}
}
//int sprIdx;
void ScrollViewTest2::scrollViewDidScroll(CCScrollView* view)
{
CCLOG("CCScrollViewDidScroll");
//在CCScrollView拖動時響應該函數
float offsetPosX = (view->getContentOffset()).x;//獲得偏移X座標(向右移動,偏移量為正數,向左則為負數)
//CCLOG("offset pos is %f , %f", view->getContentOffset().x, view->getContentOffset().y);
//for 循環遍歷容器中的每個精靈
CCObject* pObj;
//正向
CCARRAY_FOREACH(sp_vec, pObj)
{
CCSprite* e = (CCSprite*)pObj;
float CCPointX = e->getPositionX();//獲得當前對象的X座標(不管怎麼滾動,這個座標都是不變的)
float endPosX = CCPointX + offsetPosX;//將精靈的 X座標 + 偏移X座標
//當endPosX在 中間 範圍,則對象變大
if (endPosX > (MIDDLE_POSITION - CELL_WIDTH / 2) && endPosX < (MIDDLE_POSITION + CELL_WIDTH / 2))
{
e->setScale(1.0f);
sprIdx = e->getTag();//獲得正中間的商品的Tag
getTestTag(e->getTag());
if (sprIdx == 5 || sprIdx == 11)
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
}
else if (sprIdx == 6 && DataManager::getIsBuyGoldHero())
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
getChildByTag(8080)->setVisible(false);
}
else if (sprIdx == 7 && DataManager::getIsBuyFortuneHero())
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
getChildByTag(8080)->setVisible(false);
}
else if (sprIdx == 8 && DataManager::getIsBuyPandaHero())
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
getChildByTag(8080)->setVisible(false);
}
/*else if (sprIdx == 10)
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
}*/
else if (sprIdx == 12 && DataManager::getIsBuyPaoxiaoshan())
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
getChildByTag(8080)->setVisible(false);
}
else if (sprIdx == 13 && DataManager::getIsBuyYingxiaoya())
{
getChildByTag(710)->setVisible(true);
getChildByTag(216)->setVisible(false);
getChildByTag(8080)->setVisible(false);
}
else
{
getChildByTag(710)->setVisible(false);
getChildByTag(216)->setVisible(true);
getChildByTag(8080)->setVisible(true);
}
}
else
{
//不是在上面的範圍,則設置為正常大小
e->setScale(0.5f);
}
}
// for (auto e : sp_vec)
// {
// auto CCPointX = e->getPositionX();//獲得當前對象的X座標(不管怎麼滾動,這個座標都是不變的)
// float endPosX = CCPointX + offsetPosX;//將精靈的 X座標 + 偏移X座標
// //當endPosX在 中間 範圍,則對象變大
// if (endPosX > (MIDDLE_POSITION - CELL_WIDTH / 2) && endPosX < (MIDDLE_POSITION + CELL_WIDTH / 2))
// {
// e->setScale(BIG_SCALE);
// sprIdx = e->getTag();//獲得正中間的商品的Tag
// }
// else
// {
// //不是在上面的範圍,則設置為正常大小
// e->setScale(1.0f);
// }
//}
}
void ScrollViewTest2::scrollViewDidZoom(CCScrollView* view)
{
CCLOG("CCScrollViewDidZoom");
}
void ScrollViewTest2::scrollViewMoveOver(CCScrollView* view)
{
CCLOG("CCScrollViewMoveOver");
}
bool isTouchMovedMove;
bool ScrollViewTest2::ccTouchBegan(CCTouch *touch, CCEvent *pEvent){
isTouchMovedMove = false;
CCLOG("onTouchBegan");
return true;
}
void ScrollViewTest2::ccTouchMoved(CCTouch *touch, CCEvent *pEvent){
isTouchMovedMove = true;
CCLOG("onTouchMoved");
float offSetX = scrollView->getContentOffset().x;
//如果偏移量大於最初位置或者小於最末位置,都不再移動
if (offSetX >= FIRST_OFFSET){
scrollView->setContentOffset(CCPoint(FIRST_OFFSET, 0), false);
}
else if (offSetX <= LAST_OFFSET){
scrollView->setContentOffset(CCPoint(LAST_OFFSET, 0), false);
}
}
void ScrollViewTest2::ccTouchEnded(CCTouch *touch, CCEvent *pEvent){
CCLOG("onTouchEnded");
// 關閉CCCCScrollView中的自調整,如果沒有設置則進行回彈
scrollView->unscheduleAllSelectors();
CCLOG("offset pos is %f", scrollView->getContentOffset().x);
//如果沒有滑動,則根據點擊的位置進行判斷,將相應的商品移動到正中間
if (!isTouchMovedMove){
//沒有滑動的情況如下
int locatX = touch->getLocation().x;//獲取點擊的位置
int firstOffset = scrollView->getContentOffset().x;//當前的偏移量
int Offset = firstOffset - (locatX - MIDDLE_POSITION);//計算出將點擊位置移動到中間的最後偏移量 ,(locatX - MIDDLE_POSITION) 是中間距離
//以下計算 在寬度範圍內的偏移量,均調整為正中央的偏移量: eg:【260+-30】內的值都設為260
int i = ((FIRST_OFFSET + CELL_WIDTH/2) - Offset) / CELL_WIDTH;
int lastOffset = FIRST_OFFSET - i * CELL_WIDTH;
//如果超出偏移範圍
if (lastOffset >= FIRST_OFFSET){
scrollView->setContentOffset(CCPoint(FIRST_OFFSET, 0), true);
}
else if (lastOffset <= LAST_OFFSET){
scrollView->setContentOffset(CCPoint(LAST_OFFSET, 0), true);
}
else{
//根據調整後的偏移量進行偏移
scrollView->setContentOffset(CCPoint(lastOffset, 0), true);
}
}
else{
//滑動的情況
//以下計算 在寬度範圍內的偏移量,均調整為正中央的偏移量
/*int i = ((FIRST_OFFSET + CELL_WIDTH / 2) - scrollView->getContentOffset().x) / CELL_WIDTH;
unsigned int t = FIRST_OFFSET - i * CELL_WIDTH;
scrollView->setContentOffset(CCPoint(t, 0), true);*/
}
}
//返回主頁按鈕回調
void ScrollViewTest2::homeClick(CCObject* sender){
CCDirector::sharedDirector()->replaceScene(StartGame::scene());
}
//購買按鈕回調
void ScrollViewTest2::buyClick(CCObject*sender){
switch (sprIdx)
{
case 1:
if (DataManager::getGoldNum()<300)
{
CCLayer*layer = Goldlayer::create();
this->addChild(layer, 4);
}
else if (true)
{
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() - 300);
DataManager::getInstance()->setMagnetNum(DataManager::getInstance()->getMagnetNum()+1);
}
break;
case 2:
if (DataManager::getGoldNum()<600)
{
CCLayer*layer = Goldlayer::create();
this->addChild(layer,4);
}
else
{
DataManager::getInstance()->setShieldNum(DataManager::getInstance()->getShieldNum() + 1);
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() -600);
}
break;
case 3:
if (DataManager::getGoldNum()<500)
{
CCLayer*layer = Goldlayer::create();
this->addChild(layer,4);
}
else
{
DataManager::getInstance()->setRocketNum(DataManager::getInstance()->getRocketNum() + 1);
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() - 500);
}
break;
case 4:
if (DataManager::getGoldNum()<800)
{
CCLayer*layer = Goldlayer::create();
this->addChild(layer,4);
}
else
{
DataManager::getInstance()->setBloodNum(DataManager::getInstance()->getBloodNum() + 1);
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() - 800);
}
break;
case 5:
DataManager::setHeroNum(0);
//HeroTypeNum = 1;
break;
case 6:
{
std::function
DataManager::setIsBuyGoldHero(true);
DataManager::setHeroNum(1);
getChildByTag(216)->setVisible(false);
getChildByTag(710)->setVisible(true);
getChildByTag(8080)->setVisible(false);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_NEWHERO, successCallback, failCallback);
}
break;
case 7:
{
std::function
DataManager::setIsBuyFortunrHero(true);
DataManager::setHeroNum(2);
getChildByTag(216)->setVisible(false);
getChildByTag(710)->setVisible(true);
getChildByTag(8080)->setVisible(false);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_NEWHERO, successCallback, failCallback);
}
break;
case 8:
{
std::function
DataManager::setIsBuyPandaHero(true);
DataManager::setHeroNum(3);
//HeroTypeNum = 4;
getChildByTag(216)->setVisible(false);
getChildByTag(710)->setVisible(true);
getChildByTag(8080)->setVisible(false);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_NEWHERO, successCallback, failCallback);
}
break;
case 9:
{
std::function
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() + 2000);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_GOLD2, successCallback, failCallback);
}
break;
case 10:
{
std::function
DataManager::getInstance()->setGoldNum(DataManager::getInstance()->getGoldNum() + 1000);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_GOLD1, successCallback, failCallback);
}
break;
case 11:
DataManager::setSceneNum(0);
break;
case 12:
{
std::function
DataManager::getInstance()->setIsBuyPaoxiaoshan(true);
DataManager::setSceneNum(1);
getChildByTag(216)->setVisible(false);
getChildByTag(710)->setVisible(true);
getChildByTag(8080)->setVisible(false);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_NEWSCENE, successCallback, failCallback);
}
break;
case 13:
{
std::function
DataManager::getInstance()->setIsBuyYingxiaoya(true);
getChildByTag(216)->setVisible(false);
getChildByTag(710)->setVisible(true);
getChildByTag(8080)->setVisible(false);
DataManager::setSceneNum(2);
};
std::function
};
JNIUtil::pay(EnumPayType::GOODS_NEWSCENE, successCallback, failCallback);
}
break;
default:
break;
}
}
//選擇按鈕回調
void ScrollViewTest2::chooseClick(CCObject*sender){
switch (sprIdx)
{
case 1:
break;
case 5:
{
//HeroTypeNum = 1;
//CCDirector::sharedDirector()->replaceScene(StartGame::scene());
DataManager::setHeroNum(0);
for (int i = 1; i < 14; i++) {
CCNode* n = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i);
if (i == sprIdx) {
n->setVisible(true);
//DataManager::setIsHeroVisiable(true);
}
else {
n->setVisible(false);
}
}
//xuanding->setVisible(true);
//getChildByTag(963)->setVisible(true);
break;
}
case 6:
//HeroTypeNum = 2;
//CCDirector::sharedDirector()->replaceScene(StartGame::scene());
for (int i = 1; i < 14; i++) {
CCNode* n = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i);
if (i == sprIdx) {
n->setVisible(true);
}
else {
n->setVisible(false);
}
}
DataManager::setHeroNum(1);
break;
case 7:
/*HeroTypeNum = 3;
CCDirector::sharedDirector()->replaceScene(StartGame::scene());*/
DataManager::setHeroNum(2);
for (int i = 1; i < 14; i++) {
CCNode* n = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i);
if (i == sprIdx) {
n->setVisible(true);
}
else {
n->setVisible(false);
}
}
break;
case 8:
/*HeroTypeNum = 4;
CCDirector::sharedDirector()->replaceScene(StartGame::scene());*/
DataManager::setHeroNum(3);
for (int i = 1; i < 14; i++) {
CCNode* n = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i);
if (i == sprIdx) {
n->setVisible(true);
}
else {
n->setVisible(false);
}
}
break;
case 11:
DataManager::setSceneNum(0);
//CCDirector::sharedDirector()->replaceScene(StartGame::scene());
for (int i = 1; i < 14; i++) {
CCNode* n1 = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i + 100);
if (i == sprIdx) {
n1->setVisible(true);
}
else {
n1->setVisible(false);
}
}
//CCDirector::sharedDirector()->replaceScene(StartGame::scene());
break;
case 12:
DataManager::setSceneNum(1);
for (int i = 1; i < 14; i++) {
CCNode* n1 = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i + 100);
if (i == sprIdx) {
n1->setVisible(true);
}
else {
n1->setVisible(false);
}
}
break;
case 13:
DataManager::setSceneNum(2);
//getChildByTag(963)->setVisible(true);
//CCDirector::sharedDirector()->replaceScene(StartGame::scene());
for (int i = 1; i < 14; i++) {
CCNode* n1 = getChildByTag(100)->getChildByTag(101)->getChildByTag(i)->getChildByTag(i + 100);
if (i == sprIdx) {
n1->setVisible(true);
}
else {
n1->setVisible(false);
}
}
break;
default:
break;
}
/*DataManager::getHeroNum();
xuanding->setVisible(true);*/
}
閱讀更多 程序員小新人學習 的文章