var WINDOW_WIDTH = 480.0;
var WINDOW_HEIGHT = 320.0;
var TOUCH_DELTA = 5;
var ScrollView = cc.Layer.extend({
//按下點(diǎn)
m_TouchDownPoint:0,
//抬起點(diǎn)
m_TouchUpPoint:0,
//當(dāng)前觸摸點(diǎn)
m_TouchCurPoint:0,
//總頁(yè)數(shù)
m_Page:0,
//當(dāng)前顯示頁(yè)數(shù)
m_CurPage:0,
//存儲(chǔ)所有的頁(yè)層
m_PageLayer:[],
ctor:function(){
this._super();
cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this,0,true);
this.isTouchEnabled();
},
//跳轉(zhuǎn)頁(yè)
goToPage:function(){
var moveTo = cc.MoveTo.create(0.2, cc.PointMake(-this.m_CurPage * WINDOW_WIDTH, 0));
this.runAction(moveTo);
},
// 觸摸事件相關(guān)
onTouchBegan:function(touch, event){
this.m_TouchDownPoint = touch.getLocation();
this.m_TouchCurPoint = this.m_TouchDownPoint;
return true;
},
onTouchMoved:function(touch, event){
var touchPoint = touch.getLocation();
var posPoint = cc.PointMake(this.getPositionX() + touchPoint.x - this.m_TouchCurPoint.x,this.getPositionY());
this.setPosition(posPoint);
this.m_TouchCurPoint = touchPoint;
},
onTouchEnded:function(touch, event){
this.m_TouchUpPoint = touch.getLocation();
// 計(jì)算按下和抬起的偏移量
var offset = (this.m_TouchUpPoint.x - this.m_TouchDownPoint.x) * (this.m_TouchUpPoint.x - this.m_TouchDownPoint.x) + (this.m_TouchUpPoint.y - this.m_TouchDownPoint.y) * (this.m_TouchUpPoint.y - this.m_TouchDownPoint.y);
if (offset < (TOUCH_DELTA * TOUCH_DELTA)) {
// 點(diǎn)擊
// 向子Layer發(fā)送Click消息
this.m_PageLayer[this.m_CurPage].onTouchBegan(touch,event);
}
else {
// 滑動(dòng)結(jié)束
var offset = this.getPositionX() - this.m_CurPage * (-WINDOW_WIDTH);
if (offset > WINDOW_WIDTH / 2) {
// 上一頁(yè)
if (this.m_CurPage > 0) {
--this.m_CurPage;
cc.log("I am :"+this.m_CurPage);
}
}
else if (offset < -WINDOW_WIDTH / 2) {
// 下一頁(yè)
if (this.m_CurPage < (this.m_Page - 1)) {
++this.m_CurPage;
cc.log("I am :"+this.m_CurPage);
}
}
// 執(zhí)行跳轉(zhuǎn)動(dòng)畫
this.goToPage();
}
},
//添加頁(yè)
addPage:function(pPageLayer){
if (pPageLayer) {
// 設(shè)置成一頁(yè)大小
pPageLayer.setContentSize(cc.SizeMake(WINDOW_WIDTH, WINDOW_HEIGHT));
pPageLayer.setPosition(cc.p(WINDOW_WIDTH * this.m_Page, 0));
this.addChild(pPageLayer);
// 添加到頁(yè)
this.m_PageLayer.push(pPageLayer);
this.m_Page = this.m_PageLayer.length;
}
}
});
//在2.2里運(yùn)行有BUG···以后在做修改吧····
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:cocos2dhtml5版本的ScrollView·····-創(chuàng)新互聯(lián)
分享地址:http://www.chinadenli.net/article16/degcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、電子商務(wù)、小程序開發(fā)、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容