您好,登錄后才能下訂單哦!
var WINDOW_WIDTH = 480.0;
var WINDOW_HEIGHT = 320.0;
var TOUCH_DELTA = 5;
var ScrollView = cc.Layer.extend({
//按下點
m_TouchDownPoint:0,
//抬起點
m_TouchUpPoint:0,
//當前觸摸點
m_TouchCurPoint:0,
//總頁數
m_Page:0,
//當前顯示頁數
m_CurPage:0,
//存儲所有的頁層
m_PageLayer:[],
ctor:function(){
this._super();
cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this,0,true);
this.isTouchEnabled();
},
//跳轉頁
goToPage:function(){
var moveTo = cc.MoveTo.create(0.2, cc.PointMake(-this.m_CurPage * WINDOW_WIDTH, 0));
this.runAction(moveTo);
},
// 觸摸事件相關
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();
// 計算按下和抬起的偏移量
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)) {
// 點擊
// 向子Layer發送Click消息
this.m_PageLayer[this.m_CurPage].onTouchBegan(touch,event);
}
else {
// 滑動結束
var offset = this.getPositionX() - this.m_CurPage * (-WINDOW_WIDTH);
if (offset > WINDOW_WIDTH / 2) {
// 上一頁
if (this.m_CurPage > 0) {
--this.m_CurPage;
cc.log("I am :"+this.m_CurPage);
}
}
else if (offset < -WINDOW_WIDTH / 2) {
// 下一頁
if (this.m_CurPage < (this.m_Page - 1)) {
++this.m_CurPage;
cc.log("I am :"+this.m_CurPage);
}
}
// 執行跳轉動畫
this.goToPage();
}
},
//添加頁
addPage:function(pPageLayer){
if (pPageLayer) {
// 設置成一頁大小
pPageLayer.setContentSize(cc.SizeMake(WINDOW_WIDTH, WINDOW_HEIGHT));
pPageLayer.setPosition(cc.p(WINDOW_WIDTH * this.m_Page, 0));
this.addChild(pPageLayer);
// 添加到頁
this.m_PageLayer.push(pPageLayer);
this.m_Page = this.m_PageLayer.length;
}
}
});
//在2.2里運行有BUG···以后在做修改吧····
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。