简体   繁体   中英

Setting values through slider in Javascript

Im pretty new to javascript and am currently working on a pong game. It's nearly finished, the only thing i still want to add is a slider that changes the bots movement speed, but for some reason when i set the bots speed value to that of the slider, the bot just vanishes. I did the same thing for the angle the bot hits the ball with and it worked fine, so i have no idea why the other slider isn't working. The code is in the "paddleLeft" function.

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

Here is the full code if you need to look at it https://jsfiddle.net/Suppenteller/85y1j9zx/11/

Sorry for the long code, the relevant part is from line 129-178 in the Javascript section.

Edit: No error messages in the console, made a new JsFiddle link, maybe this one works

Ok, i got it to work. For some reason i can't just set the new value, i have to substract/add them...

Here is the new code.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM