登录
  • 欢迎访问 Sharezer Blog

Creator无限滚动中间放大控件

Cocos sharezer 11749次浏览 已收录 0个评论

如下图所示:

Creator无限滚动中间放大控件

把脚本拖动到节点上,再在节点下增加item就可以了

源码:

cc.Class({
    extends: cc.Component,

    properties: {
        items: {
            default: [],
            type: [cc.Node],
            visible: false,
        },
        itemSize: cc.size(100, 100),
        itemStep: {
            default: 150,
            displayName: "间距",
        },
        selectIndex: {
            default: 0,
            visible: false,
        },
        resetMoveSpeed: {
            default: 100,
            displayName: "回复速度",
        },
    },

    onLoad: function () {
        this._initItems();
        this._initSender();
        this._initEvent();
    },

    onDestroy: function () {
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this);
    },

    _initItems: function () {
        this.items = this.node.children;

        this.selectIndex = Math.floor(this.items.length * 0.5);
        this.arr = [];
        for (var i in this.items) {
            var item = this.items[i];
            item.setContentSize(this.itemSize);
            item.x = this.itemStep * (i - this.selectIndex);

            this._flashScaleAndOrder(item);
        }
    },

    _initSender: function () {
        this.isTouchMove = false;
        this.isWaitReset = false;
        this.halfCount = this.items.length * 0.5;
    },

    _initEvent: function () {
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this);
    },

    _onTouchBegan: function (event) {
        this.isTouchMove = true;
        this.isWaitReset = false;
        return true;
    },

    _onTouchMoved: function (event) {
        this.itemsMove(event.getDeltaX());
    },

    _onTouchEnded: function (event) {
        this._resetMoveState();
    },

    _onTouchCancelled: function (event) {
        this._resetMoveState();
    },

    _resetMoveState: function () {
        this.isTouchMove = false;
        this.isWaitReset = true;
    },

    itemsMove: function (ds) {
        for (var i in this.items) {
            var item = this.items[i];
            item.x += ds;
        }
    },

    _flashScaleAndOrder: function (item) {
        item.setLocalZOrder(this.items.length * this.itemStep - Math.abs(item.x));
        item.scale = 1 - Math.abs(item.x) / this.itemStep * 0.2;
    },

    _updateItems: function (dt) {

        for (var i in this.items) {
            var item = this.items[i];
            var halfStep = this.itemStep * 0.5;
            if (item.x > -halfStep && item.x < halfStep) {
                this.selectIndex = i;
            }

            //超过处理
            if (item.x < -this.halfCount * this.itemStep)
                item.x += this.itemStep * (this.items.length);
            if (item.x > this.halfCount * this.itemStep)
                item.x -= this.itemStep * (this.items.length);

            this._flashScaleAndOrder(item);
        }

        //回弹
        var resetMoveSpeed = this.resetMoveSpeed * dt;
        if (this.isWaitReset && !this.isTouchMove) {
            if (this.items[this.selectIndex].x == 0) {
                this.isWaitReset = false;
                return;
            }

            var ds = this.items[this.selectIndex].x;
            var moveSpeed = 0;
            if (Math.abs(ds) >= resetMoveSpeed)
                moveSpeed = ds > 0 ? -resetMoveSpeed : resetMoveSpeed;
            else
                moveSpeed = -ds;

            this.itemsMove(moveSpeed);
        }
    },

    update: function (dt) {
        if (this.isWaitReset || this.isTouchMove)
            this._updateItems(dt);
    },
});


Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Creator无限滚动中间放大控件
喜欢 (2)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址