![](/img/trans.png)
[英]How to allow the user to press a button by both clicking on it or by pressing the space bar
[英]Change Press Space Bar to Onscreen Button on Mobile
我正在嘗試使用Aframe進行射擊游戲。
我希望以下代碼中的項目符號組件使用屏幕上的按鈕而不是空格鍵來創建。
AFRAME.registerComponent('gun', {
schema: {
bulletTemplate: {default: '#bullet-template'},
triggerKeyCode: {default: 32} // spacebar
},
init: function() {
var that = this;
document.body.onkeyup = function(e){
if(e.keyCode == that.data.triggerKeyCode){
that.shoot();
}
}
},
shoot: function() {
this.createBullet();
},
createBullet: function() {
var el = document.createElement('a-entity');
el.setAttribute('networked', 'template:' + this.data.bulletTemplate);
el.setAttribute('remove-in-seconds', 3);
el.setAttribute('forward', 'speed:0.3');
var tip = document.querySelector('#player');
el.setAttribute('position', this.getInitialBulletPosition(tip));
el.setAttribute('rotation', this.getInitialBulletRotation(tip));
var scene = document.querySelector('a-scene');
scene.appendChild(el);
},
看起來像這樣:
init: function() {
var that = this;
document.getElementById("shootbutton").onclick = function(e){
that.shoot();
}
},
使用HTML:
<button id="shootbutton" style="float: right;height: 50px;width: 100px;font-size : 19px;color:red;">SHOOT</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.