简体   繁体   English

如何在p5.js(javascript)中将变量逐渐设置为0?

[英]How can i set a variable to 0 gradually in p5.js (javascript)?

I'm trying to control an ellipse with the up,down,left,right keys. 我正在尝试使用向上,向下,向左,向右键控制椭圆。 I want to make it smooth, so the ellipse doesn't stop immediately. 我想使其平滑,所以椭圆不会立即停止。 Also when its moving to the left and you press the right arrow, it turns around faster. 同样,当它向左移动并按向右箭头时,它转动得更快。

Minimal, Complete, and Verifiable example: 最小,完整和可验证的示例:

var x,y,speed,xspeed,yspeed,size;

function setup() {
createCanvas(window.innerWidth-20,window.innerHeight-20);
x = width / 2;
y = height - 20;
speed = 5.5;
yspeed = 0;
xspeed = 0;
size = 20;
}

function draw() {
background(0);
fill(255,0,0);
ellipse(x-size/2,y-size/2,size,size);
checkKeys();

x=constrain(xspeed+x,0+size,width);
y=constrain(yspeed+y,0+size,height);
}


function accelerateUP() {
  if(-1*(speed) <= yspeed && yspeed<= 0){yspeed-=0.5;}
  else if (0 < yspeed) yspeed=lerp(yspeed,0,0.5);
}
function accelerateDOWN(){
  if(yspeed >=0 && yspeed <= speed){yspeed+=0.5;}
  else if (yspeed <0) yspeed=lerp(yspeed,0,0.5);
}
function accelerateLEFT(){
  if(xspeed <=0 && xspeed >= -1*(speed)){xspeed-=0.5;}
  else if (xspeed >0) xspeed=lerp(xspeed,0,0.5);
}
function  accelerateRIGHT (){
  if(speed >= xspeed && xspeed >=0){xspeed+=0.5;}
  else if (0  > xspeed ) xspeed=lerp(xspeed,0,0.5);
}



function checkKeys (){

   if (keyIsDown(UP_ARROW)){
    accelerateUP();
   }          
   else if (keyIsDown(DOWN_ARROW)){
     this.accelerateDOWN(); 
   }else yspeed = lerp(yspeed,0,0.5);


   if (keyIsDown(LEFT_ARROW)){
     this.accelerateLEFT();
   }    
  else if (keyIsDown(RIGHT_ARROW)){
     accelerateRIGHT();
   }else
   xspeed = lerp(xspeed,0,0.5); 

 }

Lerp probably isn't suitable for this, I found a way to code it,that is close enough. Lerp可能不适合这个,我找到了一种足够接近的编码方式。 If you have a better way, please comment. 如果您有更好的方法,请发表评论。 I'm kind of new to this, so don't bash my code. 我对此很陌生,所以不要猛击我的代码。

var x, y, speed, xspeed, yspeed, size;

function setup() {
  createCanvas(window.innerWidth-20, window.innerHeight-20);
  x = width / 2;
  y = height - 20;
  speed = 7;
  yspeed = 0;
  xspeed = 0;
  size = 20;
}

function draw() {

  background(0);
  fill(255, 0, 0);
  ellipse(x-size/2, y-size/2, size, size);
  checkKeys();

  x=constrain(xspeed+x, 0+size, width);
  y=constrain(yspeed+y, 0+size, height);
}

function accelerateUP () {
  if (-1*(speed) <= yspeed && yspeed<= 0) {
    yspeed-=0.5;
  } else if (0 < yspeed) {  
    this.yspeed*=0.8; 
    if (-0.2 <yspeed && 0.2  > yspeed) yspeed =0;
  }
}

function accelerateDOWN() {
  if (yspeed >=0 && yspeed <= speed) {
    this.yspeed+=0.5;
  } else if (yspeed <0) { 
    yspeed*=0.8; 
    if (-0.2 <yspeed && 0.2  > yspeed) yspeed =0;
  }
}

function accelerateLEFT() {
  if (xspeed <=0 && xspeed >= -1*(speed)) {
    xspeed-=0.5;
  } else if (xspeed >0) { 
    xspeed*=0.8; 
    if (-0.2 <xspeed && 0.2  > xspeed) xspeed =0;
  }
}

function accelerateRIGHT() {
  if (speed >= xspeed && xspeed >=0) {
    xspeed+=0.5;
  } else if (0  > xspeed ) { 
    xspeed*=0.8; 
    if (-0.2 <xspeed && 0.2  > xspeed) xspeed =0;
  }
}



function checkKeys() {

  if (keyIsDown(UP_ARROW)) {
    accelerateUP();
  } else
    if (keyIsDown(DOWN_ARROW)) {
      accelerateDOWN();
    } else if (yspeed !=0) {
      if (yspeed > 0.2 || yspeed < -0.2)  yspeed*=0.9;
      else yspeed = 0;
    }



  if (keyIsDown(LEFT_ARROW)) {
    accelerateLEFT();
  } else
    if (keyIsDown(RIGHT_ARROW)) {
      accelerateRIGHT();
    } else if (xspeed !=0) {
      if (xspeed > 0.2 || xspeed < -0.2)  xspeed*=0.9; 
      else xspeed =0;
    }
};

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

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