溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

cocos2d html5版本的ScrollView·····

發(fā)布時(shí)間:2020-07-02 14:08:50 來(lái)源:網(wǎng)絡(luò) 閱讀:871 作者:xuquan_123 欄目:移動(dòng)開(kāi)發(fā)

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)畫(huà)

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···以后在做修改吧····

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI