Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

javascript - Arkanoid Mobile - Device orientation control - paddle leaving canvas

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;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...