I have an Arkanoid game written in javascript canvas, I am trying to steer by device orientation.
It works, but I want to avoid the fact that my paddle is leaving canvas ( it slides off the screen ).
It's a math problem. Can someone help out?
The orientation control happens here :
function handleOrientation(e) {
var x = e.gamma; // range [-90,90], left-right
var y = e.beta; // range [-180,180], top-bottom
var z = e.alpha; // range [0,360], up-down
if(window.innerHeight < window.innerWidth){
paddleX += y*0.35;
} else {
paddleX += x*0.35;
}
}
Edit. I've managed to do this, that the paddle won't leave the canvas. Now its just to do this on landscape mode.
if(window.innerHeight < window.innerWidth){
paddleX += y*0.35;
} else {
if (x > 0 && paddleX + paddleWidth < canvas.width) {
paddleX += x*0.35;
}
else if(x < 0 && paddleX > 0) {
paddleX += x*0.35;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…