简体   繁体   English

我如何编码以便每次运行时单击 p5.js 中的按钮?

[英]how can i code so that when i click on button in p5.js everytime it runs?

is there any way so that whenever I click on the button it must run continuously.I have to refresh it every time in order to run it every time.有什么办法可以让我每次单击按钮时它都必须连续运行。我每次都必须刷新它才能每次运行它。 one more thing is there anyway to add list in the buttons like when i click on the button it must show me more options or i have to create different buttons everytime to show it like a menu无论如何,还有一件事是在按钮中添加列表,例如当我单击按钮时,它必须向我显示更多选项,或者我必须每次创建不同的按钮才能像菜单一样显示它

var playAnim=false;
var img1
let posX=0
let posY=0
let button;
const rightwall=350;
function preload(){
  img1=loadImage("slag.jpg")
}
function setup() {
  createCanvas(600, 600);
  button =createButton('CLICK ME');
  button.position(250,300);
  button.mousePressed(changeBG);
  noLoop()
}

  function changeBG() {
  let val = random(255);
  background(val);
  loop();
  playAnim=true;
}

function draw() {
  background(220);
  fill('red');
  rect(320,75,170,160)
  fill('grey');
  rect(posX,170,70,30)
  img=image(img1,posX,posY-280,180,200)
  
  posX=constrain(posX+1,0,rightwall-30)
  posY=constrain(posX-1,posY,height-20)
  
}

i want to know how can i code or what code can i add so that whenever i click on the button it runs evertime.it must not stop or i dont have to refresh it every time我想知道我如何编码或我可以添加什么代码,以便每当我点击按钮时它都会运行。它不能停止,或者我不必每次都刷新它

Reset the coordinates posX and posY when the animation ends:当 animation 结束时,重置坐标posXposY

posX = constrain(posX+1,0,rightwall-30)
posY = constrain(posX-1,posY,height-20)
if (posX == rightwall-30) {
    posX=0
    posY=0
}

Toggle the animation when the button is clicked:单击按钮时切换 animation:

function changeBG() {
    let val = random(255);
    background(val);
    if (playAnim) {
        noLoop();
        playAnim = false;
    } else {
        loop();
        playAnim = true;
    }
}

 var playAnim=false; var img1 let posX=0 let posY=0 let button; const rightwall=350; function preload(){ img1=loadImage("https://raw.githubusercontent.com/Rabbid76/PyGameExamplesAndAnswers/master/resource/icon/Bird64.png") } function setup() { createCanvas(600, 600); button =createButton('CLICK ME'); button.position(250,300); button.mousePressed(changeBG); noLoop() } function changeBG() { let val = random(255); background(val); if (playAnim) { noLoop(); playAnim = false; } else { loop(); playAnim = true; } } function draw() { background(220); fill('red'); rect(320,75,170,160) fill('grey'); rect(posX,170,70,30) image(img1,posX,posY-280,180,200) posX = constrain(posX+1,0,rightwall-30) posY = constrain(posX-1,posY,height-20) if (posX == rightwall-30) { posX=0 posY=0 } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>


Alternatively, you can make the animation dependent on playAnim :或者,您可以使 animation 依赖于playAnim

function changeBG() {
    let val = random(255);
    background(val);
    playAnim = !playAnim;
}
if (playAnim) {
    posX = constrain(posX+1,0,rightwall-30)
    posY = constrain(posX-1,posY,height-20)
    if (posX == rightwall-30) {
        posX=0
        posY=0
    }
}

 var playAnim=false; var img1 let posX=0 let posY=0 let button; const rightwall=350; function preload(){ img1=loadImage("https://raw.githubusercontent.com/Rabbid76/PyGameExamplesAndAnswers/master/resource/icon/Bird64.png") } function setup() { createCanvas(600, 600); button =createButton('CLICK ME'); button.position(250,300); button.mousePressed(changeBG); } function changeBG() { let val = random(255); background(val); playAnim =;playAnim; } function draw() { background(220); fill('red'), rect(320,75,170;160) fill('grey'), rect(posX,170,70,30) image(img1,posX,posY-280,180,200) if (playAnim) { posX = constrain(posX+1,0,rightwall-30) posY = constrain(posX-1,posY,height-20) if (posX == rightwall-30) { posX=0 posY=0 } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

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

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