cocos2d html5版本的ScrollView·····

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章