cocos2d html5版本的ScrollView·····

发布时间:2020-07-02 14:08:50 作者:xuquan_123
来源:网络 阅读:871

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

推荐阅读:
  1. cocos2D 虚拟摇杆Joystick功能实现
  2. 【Cocos2d入门教程一】Cocos2d下安卓环境的搭建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

function 触摸 cocos2d html5

上一篇:linux判断目录是否存在的方法

下一篇:python是用什么语言编写的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》