【小白】怎么样实现长按并持续触发事件

最近在cocos Creator里面学做一个微信小游戏,但不知道怎么样实现长按并持续触发事件(因为我想在游戏里面通过长按让球转动起来),在网上查了好多资料,找到这样一个样例
let that = this;
let time1 = setTimeout(function () {
cc.log(‘LongPress’);
let time2 = setInterval(that.turn, 10);
}, 10);
that.turn是一个让球转动的函数,但是也不行,就想请问下大家怎么样才能实现呢

用一个静态变量或全局变量 按钮按下的时候赋值为 自动转球状态 松开则变为默认状态 然后在每帧计时器里去判断这个状态是不是 自动转球 是的话就调用 转球 接口

额 首先你要控制 单点,默认的是多点, 接下来 简单点的就是 touchbegan 和 end 里做标记判断

/**
 * 注册长按
 * @param node          目标node
 * @param callback      回调
 * @param target
 * @param interval      回调时间间隔(秒),不填则只回调一次
 */
onLongTouch: function (node, callback, target, interval = 0) {
    //touchstart   touchend
    node.on(`touchstart`, () => {
        cc.director.getScheduler().schedule(callback, target, interval, interval ? cc.macro.REPEAT_FOREVER : 0, 1, false);
    }, this);

    node.on(`touchcancel`, () => {
        cc.director.getScheduler().unschedule(callback, target);
    }, this);

    node.on(`touchend`, () => {
        cc.director.getScheduler().unschedule(callback, target);
    }, this);
},

看你这个需求就是在抄《一起来捉妖》

https://forum.cocos.com/t/cocos-creator/74344

也可以看看Github 仓库中 pull request 的另外一种长按实现

1赞

正好以前写过一个这个组件, 你可以直接拿来用。 https://www.liuleispace.com/#/page/articleDetailPage/?articleId=5bed84a53d0309017c3cfc7b

不是,我自己刚开始学习 只是想做一个自己的游戏,况且跳一跳也有这一块东西

  1. 把下面的代码保存到你的项目
  2. 选中你要添加连击的按钮, 添加组件->VOYA/辅助插件/连点|ae_keep_click

const {ccclass, property, menu, requireComponent, disallowMultiple} = cc._decorator;

const CLICK_INTERVAL = 0.2;         // 激活间隔
const CLICK_ACTIVE_TIME = 1.0;      // 激活最短时间
const CLICK_BOOST_ACTIVE_TIME = 10; // 疯狂激活次数
const CLICK_INTERVAL_BOOST = 0.1;   // 疯狂激活间隔

@ccclass
@menu('VOYA/辅助插件/连点|ae_keep_click')
@disallowMultiple()
@requireComponent(cc.Button)
export default class CAEKeepClick extends cc.Component {

    private mButton: cc.Button = null;
    private mClickEvents: cc.Component.EventHandler[] = [];
    private mPassTime: number = 0;
    private mActiveTimes: number = 0;

    onLoad(): void {
        this.mButton = this.node.getComponent(cc.Button);

        this.mClickEvents = this.mButton.clickEvents;
        this.mButton.clickEvents = [];
    }

    onEnable(): void {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onPress, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onRelease, this);
    }

    onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onPress, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onRelease, this);
    }

    onPress(): void {
        this.mPassTime = 0;
        this.mActiveTimes = 0;
    }

    onRelease(event: cc.Event): void {
        if (this.mActiveTimes === 0) {
            // 如果没有触发连击, 则当作点击一次, (click会在Button里生效一次, 所以这里不调用click)
            if (this.mClickEvents.length > 0) {
                cc.Component.EventHandler.emitEvents(this.mClickEvents, null);
            }
        }
    }

    update(dt: number): void {
        const btn: any = this.mButton;
        if (btn.interactable && btn._pressed) {
            this.mPassTime += dt;
            if (this.mActiveTimes > 0) {
                if (this.mActiveTimes < CLICK_BOOST_ACTIVE_TIME) {
                    while (this.mPassTime >= CLICK_INTERVAL) {
                        this.mPassTime -= CLICK_INTERVAL;
                        this.mActiveTimes += 1;
                        this.emitClick();
                    }
                } else {
                    while (this.mPassTime >= CLICK_INTERVAL_BOOST) {
                        this.mPassTime -= CLICK_INTERVAL_BOOST;
                        this.mActiveTimes += 1;
                        this.emitClick();
                    }
                }
            } else if (this.mPassTime >= CLICK_ACTIVE_TIME) {
                this.mPassTime = 0;
                this.mActiveTimes = 1;
                this.emitClick();
            }
            
        }
    }

    emitClick(): void {
        if (this.mClickEvents.length > 0) {
            cc.Component.EventHandler.emitEvents(this.mClickEvents, null);
        }
        this.node.emit('click', this);
    }
}