creator 滑动循环展示图片 自动展示

news2024/9/23 21:29:47

在这里插入图片描述


import MyBaseView from "./MyBaseView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ScrollCard extends MyBaseView {
    @property({ tooltip: "是否自动展示" })
    Move_zidong: boolean = true;
    @property({ tooltip: "自动展示速度" })
    Speed_zidong: number = 0.05;

    @property({ tooltip: "是否纵方向" })
    Direction_isHorizontal: boolean = true;
    @property({ tooltip: "每个item的间隔" })
    itemOffset: number = 0;
    @property({ tooltip: "移动速度" })
    speed: number = 500;
    @property({ tooltip: "减速频率" })
    rub: number = 1.0;
    @property({ tooltip: "缩放最小值" })
    scaleMin: number = 1.0;
    @property({ tooltip: "缩放最大值" })
    scaleMax: number = 1.0;
    @property({ type: [cc.Node], tooltip: "滚动item" })
    item: cc.Node[] = [];
    _startTime = 0;
    _moveSpeed = 0;
    itemList = [];
    _maxSize;
    _screenRect;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this._initItemPos();
        this.updateScale();
    }
    public onTouchBegan(event: cc.Event.EventTouch): boolean {
        this.clickNode = null;
        if (this.getIsClickNode(this.node, event.getLocation())) {
            this.clickNode = this.node;
            this._moveSpeed = 0;
            this._startTime = new Date().getTime();
            return true;
        }
        return false;
    }
    public onTouchMoved(event: cc.Event.EventTouch): void {
        if (this.clickNode) {
            let movePos = event.getDelta();
            this.itemMoveBy(movePos);
        }
    }
    public onTouchEnd(event: cc.Event.EventTouch): void {
        if (this.clickNode) {
            this.clickNode = null;
            let curpos = event.getLocation();
            let startpos = event.getStartLocation();

            let dis;
            if (this.Direction_isHorizontal) {
                dis = startpos.x - curpos.x;

            } else {
                dis = startpos.y - curpos.y;
            }

            let curTime = new Date().getTime();
            let disTime = curTime - this._startTime;
            //v = s/t
            this._moveSpeed = dis / disTime;
        }
    }
    ontou
    _initItemPos() {
        this.node.anchorY = 0.5;
        this.node.anchorX = 0.5;
        this._maxSize = new cc.Size(0, 0);
        for (let i = 0; i < this.item.length; i++) {
            this._maxSize.width += this.item[i].width;
            this._maxSize.height += this.item[i].height;
            this._maxSize.width += this.itemOffset;
            this._maxSize.height += this.itemOffset;
        }
        let startPos;
        if (this.Direction_isHorizontal) {
            startPos = cc.v2(-this._maxSize.width * this.node.anchorX, -this._maxSize.height * this.node.anchorY);
        } else {
            startPos = cc.v2(this._maxSize.width * this.node.anchorX, this._maxSize.height * this.node.anchorY);
        }
        this._screenRect = new cc.Rect(startPos.x, startPos.y, this._maxSize.width, this._maxSize.height);
        this.itemList = [];
        for (let i = 0; i < this.item.length; i++) {
            let anchor = this.item[i].getAnchorPoint();
            let itemSize = this.item[i].getContentSize();

            if (this.Direction_isHorizontal) {
                startPos.addSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                this.item[i].x = startPos.x;
                // cc.log('x:'+startPos.x);
                this.item[i].y = 0;
                startPos.addSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                startPos.addSelf(cc.v2(this.itemOffset, this.itemOffset));
            } else {
                startPos.subSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                this.item[i].x = 0;
                this.item[i].y = startPos.y;
                startPos.subSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                startPos.subSelf(cc.v2(this.itemOffset, this.itemOffset));
            }
            this.itemList[i] = this.item[i];
        }

    }
    itemMoveBy(pos) {
        for (let i = 0; i < this.item.length; i++) {
            if (this.Direction_isHorizontal) {
                this.item[i].x += pos.x;
            } else {
                this.item[i].y += pos.y;
            }
        }
        this.updatePos();
    }
    updatePos() {

        let startItem = this.itemList[0];
        let endItem = this.itemList[this.itemList.length - 1];

        let startout = false;
        if (this.Direction_isHorizontal) {
            if (startItem.x < -this._maxSize.width / 2) {
                startout = true;
            }
        } else {
            if (startItem.y > this._maxSize.width / 2) {
                startout = true;
            }
        }

        //left
        if (startout) {
            let item = this.itemList.shift();
            this.itemList.push(item);

            if (this.Direction_isHorizontal) {
                item.x = endItem.x + endItem.width + this.itemOffset;
            } else {
                item.y = endItem.y - endItem.height - this.itemOffset;
            }
        }

        let endout = false;
        if (this.Direction_isHorizontal) {
            if (endItem.x > this._maxSize.width / 2) {
                endout = true;
            }
        } else {
            if (endItem.y < -this._maxSize.height / 2) {
                endout = true;
            }
        }

        //right
        if (endout) {
            let item = this.itemList.pop();
            this.itemList.unshift(item);

            if (this.Direction_isHorizontal) {
                item.x = startItem.x - startItem.width - this.itemOffset;
            } else {
                item.y = startItem.y + startItem.height + this.itemOffset;
            }
        }

        this.updateScale();
    }
    updateScale() {
        if (this.scaleMax < this.scaleMin || this.scaleMax == 0) {
            return;
        }
        for (let i = 0; i < this.item.length; i++) {

            let pre;
            if (this.Direction_isHorizontal) {
                let x = this.item[i].x + this._maxSize.width / 2;
                if (this.item[i].x < 0) {
                    pre = x / this._maxSize.width;
                }
                else {
                    pre = 1 - x / this._maxSize.width;
                }

            } else {
                let y = this.item[i].y + this._maxSize.height / 2;
                if (this.item[i].y < 0) {
                    pre = y / this._maxSize.height;
                }
                else {
                    pre = 1 - y / this._maxSize.height;
                }
            }
            pre *= 2;
            let scaleTo = this.scaleMax - this.scaleMin;
            scaleTo *= pre;
            scaleTo += this.scaleMin;
            scaleTo = Math.abs(scaleTo);
            this.item[i].scaleX = scaleTo;
            this.item[i].scaleY = scaleTo;
        }
    }
    update(dt) {
        if (this._moveSpeed == 0) {
            if (this.Move_zidong && !this.clickNode) {
                for (let i = 0; i < this.item.length; i++) {

                    if (this.Direction_isHorizontal) {
                        this.item[i].x -= this.Speed_zidong * dt * this.speed;
                    } else {
                        this.item[i].y -= this.Speed_zidong * dt * this.speed;
                    }
                }
                let moveTo = -this._moveSpeed * dt * this.speed;
                this.itemMoveBy(cc.v2(moveTo, moveTo))
                this.updatePos();
            }
            return;
        }
        for (let i = 0; i < this.item.length; i++) {

            if (this.Direction_isHorizontal) {
                this.item[i].x -= this._moveSpeed * dt * this.speed;
            } else {
                this.item[i].y -= this._moveSpeed * dt * this.speed;
            }
        }
        if (this._moveSpeed > 0) {
            this._moveSpeed -= dt * this.rub;
            if (this._moveSpeed < 0) {
                this._moveSpeed = 0;
            }
        } else {
            this._moveSpeed += dt * this.rub;
            if (this._moveSpeed > 0) {
                this._moveSpeed = 0;
            }
        }
        let moveTo = -this._moveSpeed * dt * this.speed;
        this.itemMoveBy(cc.v2(moveTo, moveTo))
        this.updatePos();
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/749070.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【机器学习】特征降维 - 方差选择法VarianceThreshold

「作者主页」&#xff1a;士别三日wyx 「作者简介」&#xff1a;CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」&#xff1a;对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 方差选择法 一、方差科普二、方差选择API三、获取数…

炎炎夏日,相约Polkadot Decoded 2023上海

​上周&#xff0c;Moonbeam现身胡志明&#xff0c;参与GM Vietnam峰会活动&#xff0c;并与Chainlink和AWS等知名公司的代表一同畅谈Web3。 这周&#xff0c;Moonbeam中文社区将出席Polkadot未来论坛上海站&#xff0c;与来自波卡生态中不同平行链的代表一同探讨波卡新一代生…

小区物业管理系统需求分析

小区物业管理系统核心在于加强管理&#xff0c;提升效率&#xff0c;降低成本。实现物业核心业务信息化&#xff0c;为员工提供流畅运营模式&#xff0c;为业主提供高品质服务&#xff0c;有助于公司做强做大&#xff0c;系统优势主要有以下几方面&#xff1a; • 服务数字化&a…

Systemd服务单元

Systemd服务单元 service服务文件基本格式自定义service文件systemctl定时重启service文件中的type systemctl管理service查看服务状态查看服务是否运行查看服务是否被启用 查看开机自启服务列表设置开机启动开机启动的原理&#xff1a; 取消开机启动启动/暂停/重启服务重新加载…

day04 重新学python——数据容器

文章目录 一、数据容器1.数据容器&#xff1a;list(列表)2.list(列表)的遍历3.数据容器&#xff1a;tuple(元组)4.数据容器&#xff1a;str(字符串)5.数据容器的切片6.数据容器&#xff1a;set(集合)7.数据容器&#xff1a;dict(字典、映射)8.数据容器的通用操作 一、数据容器 …

Vue2:简单使用Animate.css动画效果

简单的使用一下Animate的动画效果&#xff08;安装&#xff0c;使用&#xff0c;速度&#xff09; 官网&#xff1a;动画.css |CSS动画的跨浏览器库。 (animate.style)https://animate.style/ 1、通过npm安装&#xff1a; npm install animate.css --save 2、导入到组件 i…

第一百零六天学习记录:数据结构与算法基础:单链表(王卓教学视频)

线性表的链式表示和实现 结点在存储器中的位置是任意的&#xff0c;即逻辑上相邻的数据元素在物理上不一定相邻 线性表的链式表示又称为非顺序映像或链式映像。 用一组物理位置任意的存储单元来存放线性表的数据元素。 这组存储单元既可以是连续的&#xff0c;也可以是不连续的…

Thymeleaf th名称空间-表达式语法-访问域对象-获取请求参数-内置对象

基本语法&#xff1a;th名称空间 基本语法&#xff1a;表达式语法 修改标签文本值 代码示例&#xff1a; <p th:text"标签体新值">标签体原始值</p>th:text作用 不经过服务器解析&#xff0c;直接用浏览器打开HTML文件&#xff0c;看到的是『标签体原…

使用C语言连接MySQL

目录 一、引入库 1.1 下载库文件 1.2 在项目中引入库 二、使用库 2.1 连接数据库 2.2 SQL请求 2.3 获取查询结果 2.4 使用案例 一、引入库 1.1 下载库文件 要使用C语言连接MySQL&#xff0c;需使用MySQL官网提供的库 MySQL :: Download Connector/Chttps://dev.mysq…

家政服务小程序开发预约功能

家政服务的需求也越来越大&#xff0c;为了更加方便用户预约服务&#xff0c;很多家政服务平台开始开发微信小程序&#xff0c;为用户提供在线预约服务。那么&#xff0c;如何开发家政服务小程序的预约功能呢&#xff1f;下面我们将结合一些参考信息&#xff0c;为大家详细介绍…

vue-element-admin || 后台管理三级/多级菜单设置

如图&#xff0c;基于vue-element-admin前端框架实现三级菜单&#xff0c;其中页面只对应三级菜单&#xff0c;无二级菜单的页面。 文件组织&#xff0c;在views文件夹下如下组织文件结构&#xff0c;其中第三级的菜单就是具体的.vue文件 在一级菜单hxb_sys下&#xff0c;要…

PROFIBUS-DP主站转ETHERCAT网关连接canopen协议报文解析实例

大家好&#xff0c;今天要给大家介绍一款远创智控的神秘产品&#xff0c;它的名字叫YC-DPM-ECT&#xff0c;是一款兼具PROFIBUS-DP主站功能的通讯网关。想象一下&#xff0c;它既能和PROFIBUS总线打交道&#xff0c;又能与ETHERCAT网络愉快地交流&#xff0c;是不是感觉很神奇&…

【Arduino小车实践】PID应用之四驱小车

一、 PID公式 二、 PID应用的必要性 1. 四驱小车运动 左边两个驱动轮和右边两个驱动轮的速度相同直线右边轮子的速度大于左边轮子的速度左偏右边轮子的速度小于左边轮子的速度 右偏 2. 产生多种运动的原因 小车的4个电机&#xff0c;减速箱以及车轮在物理层面上存在误差&am…

Spark(21):SparkStreaming之DStream入门

目录 0. 相关文章链接 1. WordCount 案例实操 1.1. 需求 1.2. 添加依赖 1.3. 编写代码 1.4. 启动程序并通过netcat发送数据 2. WordCount 解析 0. 相关文章链接 Spark文章汇总 1. WordCount 案例实操 1.1. 需求 使用 netcat 工具向 9999 端口不断的发送数据&#xf…

Flutter系列文章-Flutter环境搭建和Dart基础

Flutter是Google推出的一个开源的、高性能的移动应用开发框架&#xff0c;可以用一套代码库开发Android和iOS应用。Dart则是Flutter所使用的编程语言。让我们来看看如何搭建Flutter开发环境&#xff0c;并了解Dart语言的基础知识。 一、Flutter环境搭建 1. 安装Flutter SDK …

Blender 3.6 LTS更新的5个新功能,一定要试试

Blender基金会已正式发布Blender 3.6 LTS&#xff08;长期支持&#xff09;。它是备受期待的该公司开源 3D 软件的长期支持版本&#xff0c;也是 Blender 3.x 系列的最终 LTS 版本。Blender 3.6有一个用于设置基于粒子的模拟的模拟节点和一个升级的 UV 封装系统&#xff0c;其中…

IDEA自动添加注释作者版本时间等信息

File | Settings | Editor | Live Templates 点击加号&#xff0c;选择第二项 设置一个名称 再次点击加号&#xff0c;选择第一项 填写名称&#xff08;设置完成后再代码中输入该名称即可插入该注释内容&#xff09;&#xff0c;描述&#xff0c;及内容 /*** author 名字…

深度图像Range Image

从点云创建深度图并显示 函数原型 RangeImage::createFromPointCloud (const PointCloudType& point_cloud, float angular_resolution, float max_angle_width, float max_angle_height, …

Leaflet Ant Path(水系流动效果)

一、源代码&#xff1a; 用leaflet库中的Leaflet.AntPath插件 将通量动画&#xff08;如蚂蚁行走&#xff09;放入折线中 <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><…

【UE】运行游戏时就获取鼠标控制

问题描述 我们经常在点击运行游戏后运行再在视口界面点击一下才能让游戏获取鼠标控制。其实只需做一个设置就可以在游戏运行后自动获取鼠标控制。 解决步骤 点击编辑器偏好设置 如下图&#xff0c;点击“播放”&#xff0c;再勾选“游戏获取鼠标控制” 这样当你运行游戏后直…