我可以'我在HTML5游戏中没有鼠标输入
I can't get mouse input in my HTML5 game
我正试图在游戏中获得鼠标输入;任何帮助都将不胜感激。
我在init()函数中调用事件侦听器,并且我有mouseMoved()和mouseClicked()函数。但我一直没有得到任何回应。
(我被要求为这个项目制作一个jsFiddle,所以它就在这里。出于某种原因,它没有渲染图像。但一旦有输入,左上角应该有文本显示鼠标坐标。此外,当你点击画布时,你应该会收到警报。)
var canvasBg = document.getElementById('canvasBg');
var ctxBg = canvasBg.getContext('2d');
var canvasEntities = document.getElementById('entities');
var entitiesCtx = canvasEntities.getContext('2d');
var isPlaying = false;
var player;
var enemy;
var mouseX, mouseY;
var playerImg = new Image();
playerImg.src = 'http://placekitten.com/g/50/50';
var enemyImg = new Image();
enemyImg.src = 'http://placehold.it/50x50';
window.onload = init;
var requestAnimFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
// main functions
function init() {
console.debug('init()');
player = new Entity(250, // xpos
225, // ypos
0, // xd
0, // yd
3, // speed
50, // width
50, // height
playerImg, // imgSrc
true); // player?
enemy = new Entity(500,225,0,0,1,25,25,enemyImg,false);
canvasBg.addEventListener('mousemove', mouseMoved, false);
canvasBg.addEventListener('click', mouseClicked, false);
startLoop();
}
function loop() {
// console.debug('game loop');
if(isPlaying){
update();
draw();
requestAnimFrame(loop);
}
}
function startLoop() {
isPlaying = true;
loop();
}
function stopLoop() {
isPlaying = false;
}
function clearAllCtx() {
ctxBg.clearRect(0, 0, canvasBg.width, canvasBg.height);
Entity.clearCtx();
}
function draw(){
clearAllCtx();
player.draw();
enemy.draw();
}
function update(){
player.update();
}
// end of main functions
// input handling
function mouseMoved(e) {
mouseX = e.layerX - canvasBg.offsetLeft;
mouseY = e.layerY - canvasBg.offsetTop;
document.getElementById('mouseCoors').innerHTML = 'X: ' + mouseX + ' Y: ' + mouseY;
}
function mouseClicked(e) {
alert('You clicked the mouse!');
}
// end of input handling
// Entity functions
function Entity(xpos, ypos, xd, yd, speed, width, height, imagesrc, player) {
this.xpos = xpos;
this.ypos = ypos;
this.xd = xd;
this.yd = yd;
this.speed = speed;
this.width = width;
this.height = height;
this.imagesrc = imagesrc;
this.player = player;
}
Entity.clearCtx = function(){
entitiesCtx.clearRect(0,0,canvasBg.width,canvasBg.height);
};
Entity.prototype.draw = function () {
entitiesCtx.drawImage(this.imagesrc, this.xpos, this.ypos);
};
Entity.prototype.update = function () {
this.xpos += this.xd;
this.ypos -= this.yd;
};
// end of Entity functions
所以发生了一些事情,首先小提琴加载设置错误,一旦我将其更改为无包裹体,一切都正常。
您遇到的实际问题是,背景画布位于实体画布下,因此无法获取任何鼠标事件。
一种解决方案是使用指针事件并将其设置为none,就像实体画布上的pointer-events: none
一样。
实时演示
#entities {
margin: -500px auto;
pointer-events: none;
}
如果您需要更广泛的浏览器支持,另一个选项是让entities
画布捕获鼠标事件。
选项2的实时演示
相关文章:
- 鼠标输入在 YUI 的 chrome 中不起作用
- 使用鼠标输入图表时禁用自动点悬停
- 鼠标输入和鼠标离开不起作用
- 鼠标输入鼠标离开没有按照我认为应该的方式工作
- 鼠标输入悬停分区显示/隐藏
- 清除间隔在鼠标输入上不起作用
- 鼠标输入事件上的jQuery延迟出现问题
- jQuery-鼠标输入闪烁图像
- 鼠标输入事件未触发
- 临时取消绑定鼠标输入从元素
- 悬停(),鼠标输入(),鼠标悬停()等来回跳跃
- 如何在鼠标输入时对字体大小进行动画处理
- 在单击时对元素进行动画处理,并在该元素和其他元素上单击鼠标输入/鼠标离开
- JS:鼠标输入的触摸等效项
- 防止“鼠标输入”回调函数,直到“突出显示”效果完成
- FF 鼠标离开/鼠标输入事件
- jQuery在鼠标输入时更改css,但仅输入内容而不是全部
- 如何使鼠标输入功能具有淡入淡出效果并保持在那里
- jQuery:单击时,防止鼠标输入事件
- 鼠标输入/鼠标离开产生闪烁效果