Open weijiahui1 opened 5 years ago
handleDirectionTopStart () { const e = this.renderer; const t = window.jQuery.simulate.keyCode; $(e).simulate('keydown', {keyCode: t.UP}); } handleDirectionTopEnd () { const e = this.canvas; const t = window.jQuery.simulate.keyCode; $(e).simulate('keyup', {keyCode: t.UP}); } 这样写不会报错,可是点击没有效果:(
舞台的一些设备事件是由scratch-vm控制的,可以在scratch-vm/src/io/keyboard.js中查看 不需要用jQuery插件来额外模拟处理,通过这样来模拟键盘事件:
const KEYS_MAP = {
'space': ' ',
'left arrow': 'Left',
'up arrow': 'Up',
'right arrow': 'Right',
'down arrow': 'Down'
};
function triggerKeyboardEvent (key, down = true) {
// vm是当前舞台运行环境中scratch-vm的实例
vm.postIOData('keyboard', {
key: KEYS_MAP[key] || key,
isDown: down
});
}
triggerKeyboardEvent('a', true)
万分感谢,实现了!
我用的scratch-render怎么实现鼠标点击事件?
引入scratch-vm 可以实现
引入了,就是手机模式点击事件无效 canvas.addEventListener('mousedown', e => { const rect = canvas.getBoundingClientRect(); const data = { isDown: true, x: e.clientX - rect.left, y: e.clientY - rect.top, canvasWidth: rect.width, canvasHeight: rect.height }; console.log(data); vm.postIOData('mouse', data); e.preventDefault(); }); canvas.addEventListener('mouseup', e => { const rect = canvas.getBoundingClientRect(); const data = { isDown: false, x: e.clientX - rect.left, y: e.clientY - rect.top, canvasWidth: rect.width, canvasHeight: rect.height }; console.log(data);
vm.postIOData('mouse', data);
e.preventDefault();
});
手机需要绑定触摸事件 touchstart、touchmove、touchend
事件执行了,只是舞台没效果
用过jquery.simulate.js实现模拟键盘事件吗? 我现在要自己写个div 点击这个div相当于出发了键盘的上键,我用了这个jquery.simulate.js,但是却一直报错simulate不是个function 在index.ejs引用的这个:
在stage.jsx中写的这个事件: handleDirectionTopStart () { const t = window.jQuery.simulate.keyCode; const e = document.getElementById('canvasbox'); e.simulate('keydown', {keyCode: t.UP}); } handleDirectionTopEnd () { const t = window.jQuery.simulate.keyCode; const e = document.getElementById('canvasbox'); e.simulate('keyup', {keyCode: t.UP}); }