我正在尝试使用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);
},
答案 0 :(得分:0)
看起来像这样:
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>