简体   繁体   English

球在 p5.js 中碰撞后没有回来

[英]Ball not coming back after colliding in p5.js

var x, y, speed, speed2, speedx, speedy;

function setup() {
    createCanvas(1200, 630);
    x = 50;
    y = 300;

    speed = 20;
    speed2 = 20;

    speedx = createSlider(2, 50, 20);
    speedx.position(1000, 100);
    speedx.style('width', '200px');

    speedy = createSlider(10, 50, 20);
    speedy.position(1000, 150);
    speedy.style('width', '200px');
}

function draw() {
    background("white");

    x = x + speedx.value();
    y = y + speedy.value();
    if (x > 1170) {
        x = x - speedx.value();
    }
    if (x < 10) {
        x = x + speedx.value();
    }
    if (y > 610) {
        y = y - speedy.value();
    }
    if (y < 15) {
        x = x + speedx.value();
    }
    let color1 = color("black");
    fill(color1);
    ellipse(x, y, 20);
}

** I am new to p5.js and made this (my first code) but it is not working as I expected ** 我是 p5.js 的新手并做了这个(我的第一个代码),但它没有按我预期的那样工作

Please help me by answering this code **请通过回答此代码来帮助我**

Currently your code isn't going to make the ball "come back" because while it is limiting the x and y positions, it doesn't change the x and y velocities (so once the ball gets to the edge it is just going to stick there).目前,您的代码不会让球“回来”,因为虽然它限制了 x 和 y 位置,但它不会改变 x 和 y 速度(所以一旦球到达边缘,它只会坚持在那里)。 You also have a few defects in to edge limiting logic.您在边缘限制逻辑方面也有一些缺陷。

  1. Your sliders are always positive so the ball can only move down and to the right.您的滑块始终为正,因此球只能向下和向右移动。
  2. When you check the y position against the minimum you modify the x position instead of the y position.当您根据最小值检查 y position 时,您修改 x position 而不是 y position。
  3. When you check the x position against the minimum value you are adding the speeds, but presumably when this happens speed would be negative (ie moving to the left), so you still want to subtract.当您对照最小值检查 x position 时,您正在添加速度,但大概发生这种情况时,速度将为负数(即向左移动),所以您仍然想减去。

 var x, y, speedx, speedy; function setup() { // make the canvas cover the window createCanvas(windowWidth, windowHeight); x = width / 2; y = height / 2; // create slider ranges such that things don't always go down and to the right speedx = createSlider(-10, 10, 2); speedx.position(10, 10); speedx.style("width", "200px"); speedy = createSlider(-10, 10, 2); speedy.position(10, 50); speedy.style("width", "200px"); } function draw() { background("white"); x = x + speedx.value(); y = y + speedy.value(); if (x > width - 10) { x = x - speedx.value(); } if (x < 10) { // presumably the reason we're going off the screen is that speedx is negative. x = x - speedx.value(); } if (y > height - 10) { y = y - speedy.value(); } if (y < 10) { y = y - speedy.value(); } let color1 = color("black"); fill(color1); ellipse(x, y, 20); }
 html, body {margin:0}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

You can do something like this (visit codecademy for details: https://www.codecademy.com/courses/learn-p5js/lessons/p5js-animation )你可以这样做(访问 codecademy 了解详情: https://www.codecademy.com/courses/learn-p5js/lessons/p5js-animation

     speedx *= -1

You can do the same thing with speedy你可以用speedy做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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