[英]Setting values through slider in Javascript
我对 javascript 很陌生,目前正在开发乒乓球游戏。 它快完成了,我唯一还想添加的是一个改变机器人移动速度的滑块,但是由于某种原因,当我将机器人速度值设置为滑块的速度值时,机器人就消失了。 我对机器人击球的角度做了同样的事情,而且效果很好,所以我不知道为什么另一个滑块不起作用。 代码在“paddleLeft”函数中。
CSS/HTML
body{
width: 100vw;
height: 100vh;
}
<body onresize="resizeCanvas()">
Bot speed<input type="range" min="0" max="5" step="0.5" value="2.5" id="botSpeed">
<canvas id="canvas"></canvas>
<script>
</script>
</body>
JavaScript
var canvas, context;
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
var ballRadius;
var ballRadiusValue = .000005; // Radius of ball
var ballSpeed = 2.5; // Speed the ball moves with
var ballSpeedX;
var ballSpeedY;
var ballX;
var ballY;
var paddleWidth;
var paddleWidthValue = .01; // Width of paddles
var paddleHeight;
var paddleHeightValue = .125; // Height of paddles
var paddleMargin = .01; // Margin the paddles have from the wall
var paddleLeftX;
var paddleLeftY;
var angle = 5; // Angle the bot hits the ball with (lower number == higher difficulty)
var speed = 2.5; // Speed the bot follows the ball with (higher number == higher difficulty)
// Main function
window.onload = function() {
resizeCanvas();
startGame();
setInterval(drawCanvas, 1);
};
// Resizes the canvas depending on the screen resolution
function resizeCanvas() {
// Sets the canvas width and height to the users viewport
context.canvas.width = document.body.clientWidth;
context.canvas.height = document.body.clientHeight;
// Ball
ballRadius = (canvas.width * canvas.height) * ballRadiusValue;
ballX = (canvas.width / 2) - (ballRadius / 2); // Center of canvas width
ballY = (canvas.height / 2) - (ballRadius / 2); // center of canvas height
// Paddle
paddleWidth = canvas.width * paddleWidthValue;
paddleHeight = canvas.height * paddleHeightValue;
paddleLeftX = canvas.width * paddleMargin;
paddleLeftY = (canvas.height / 2) - (paddleHeight / 2); // Centers, center of paddle to height of canvas
}
// Draws everything that's displayed on the canvas
function drawCanvas() {
background();
paddleLeft();
ball();
ballMovement();
}
function background() {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "transparent";
context.fillRect(0, 0, canvas.width, canvas.height);
}
// Bot
function paddleLeft() {
context.fillStyle = "red";
context.fillRect(paddleLeftX, paddleLeftY, paddleWidth, paddleHeight);
var botSpeed = document.getElementById("botSpeed");
speed = 2.5; // This works
speed = botSpeed.value; // This doesn't work, even though "botSpeed.value" is the same value
var paddleLeftCenter = paddleLeftY + (paddleHeight / 2);
// How fast and at what angle the bot follows the ball
if (paddleLeftCenter < ballY - (paddleHeight / angle) && (paddleLeftY + paddleHeight) <= canvas.height) // Paddle is above ball
{
paddleLeftY += speed; // Moves the paddle down
}
else if (paddleLeftCenter > ballY + (paddleHeight / angle) && paddleLeftY >= 0) // Paddle is below ball
{
paddleLeftY -= speed; // Moves the paddle up
}
}
// Initial speed
function ballMovement() {
ballX += ballSpeedX;
ballY += ballSpeedY;
}
function ball() {
context.fillStyle = "black";
context.beginPath();
context.arc(ballX, ballY, ballRadius, 0, Math.PI * 2, true);
context.fill();
// Collision with border
if (ballX + ballRadius <= 0 ) // Left side
{
resetBall();
}
if (ballY - ballRadius <= 0) // Top
{
ballSpeedY = -ballSpeedY;
}
else if (ballY + ballRadius >= canvas.height) // Bottom
{
ballSpeedY = -ballSpeedY;
}
}
// Moves the ball in a random direction on game start
function startGame() {
ballSpeedX = -ballSpeed;
ballSpeedY = ballSpeed;
var random = Math.random();
if (random <= .5) // Moves ball down
{
ballSpeedY = ballSpeedY;
}
else if (random <= 1) // Moves ball up
{
ballSpeedY = -ballSpeedY;
}
}
// Resets the ball to its original position
function resetBall() {
ballX = canvas.width / 2;
ballY = canvas.height / 2;
startGame();
}
如果您需要查看它,这里是完整代码https://jsfiddle.net/Suppenteller/85y1j9zx/11/
抱歉,代码太长,相关部分来自 Javascript 部分的第 129-178 行。
编辑:控制台中没有错误消息,创建了一个新的 JsFiddle 链接,也许这个有效
好的,我让它工作了。 出于某种原因,我不能只设置新值,我必须减去/添加它们...
这是新代码。
var isMovingSlider = false; // Slider function will only get fired once on change
if (!isMovingSlider)
{
// Sets the "botSpeed" value
botSpeed.addEventListener("change", function () {
isMovingSlider = true;
if (botSpeed.value < 2.5)
{
// For every step the slider gets moved below 2.5, subtract 0.5 from "speed"
speed = 2.5;
speed -= (2.5 - botSpeed.value);
}
else if (botSpeed.value > 2.5)
{
// For every step the slider gets moved aboce 2.5, add 0.5 to "speed"
speed = 2.5;
speed += (botSpeed.value - 2.5);
}
else if (botSpeed.value == 2.5)
{
speed = 2.5;
}
});
isMovingSlider = false;
}
botSpeedValue.innerHTML = botSpeed.value; // Displays the value of "botSpeed"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.