首页 > 解决方案 > 错误:看起来您在处理中混合了“活动”和“静态”模式

问题描述

该代码是我下载的一个开放处理程序,但似乎无法正常工作。我是初学者,需要帮助来了解如何处理错误。提前致谢。请在此处找到开放处理代码:https ://www.openprocessing.org/sketch/407405

 void setup() {
    size(400,400,0);
    }
    rectMode(CENTER);
    textAlign(CENTER,CENTER);
    var P=200;
    var LOSE=false;
    var score=0;
    var particlestor = [];
    var Timer=millis();
    var fall = function(position) {
      this.velocity = new PVector(0,3);
      this.position = position.get();
    fall.prototype.run = function() {
      this.position.add(this.velocity);
      noStroke();
      fill(0, 0,0);
      rect(this.position.x, this.position.y, 20, 20);
      if(dist(this.position.x, this.position.y,P,390)<=20){LOSE=true;}};
    fall.prototype.isDead = function() {
    if (this.position.y > 400) {return true;} else {return false;}
    };};
    for(var i=0;i<=4;i++){particlestor.push(new fall(new PVector(random(0,400), random(-10,-600))));}
    draw = function() {
    if(LOSE===false){
      background(255, 255, 255);
    if(millis()-Timer>=1000){
    Timer=millis();
    score+=1;
    particlestor.push(new fall(new PVector(random(0,400), random(-10,-400))));
    }    
      P=mouseX;
      for (var i = particlestor.length-1; i >= 0; i--) {
        var p = particlestor[i];
        p.run();
        if (p.isDead()===true) {
          particlestor.splice(i, 1);
          particlestor.push(new fall(new PVector(random(0,400), random(-10,-400))));
        }}
        fill(240,0,0);
        rect(P,390,20,20);
        text(score,10,10);
        if(P>400){LOSE=true;}
        if(P<0){LOSE=true;}
      }
      if(LOSE===true){
      textSize(60);
      text("YOU LOSE\n score :"+score,200,200);
    }};// 50 lines

标签: processing

解决方案


上面的代码使用ProcessingJS。如果你想在你的机器上本地运行代码,你有几个选择:

  1. 查看ProcessingJS 快速入门指南并使用它
  2. 将代码移植到处理
  3. 将代码移植到p5.js

例如:

var P=200;
var LOSE=false;
var score=0;
var particlestor = [];
var Timer;


function setup() {
  createCanvas(400, 400, 0);

  rectMode(CENTER);
  textAlign(CENTER, CENTER);
  
  Timer=millis();

  for (var i=0; i<=4; i++) {
    particlestor.push(new fall(createVector(random(0, 400), random(-10, -600))));
  }
}

function draw() {
  if (LOSE===false) {
    background(255, 255, 255);
    if (millis()-Timer>=1000) {
      Timer=millis();
      score+=1;
      particlestor.push(new fall(createVector(random(0, 400), random(-10, -400))));
    }    
    P=mouseX;
    for (var i = particlestor.length-1; i >= 0; i--) {
      var p = particlestor[i];
      p.run();
      if (p.isDead()===true) {
        particlestor.splice(i, 1);
        particlestor.push(new fall(createVector(random(0, 400), random(-10, -400))));
      }
    }
    fill(240, 0, 0);
    rect(P, 390, 20, 20);
    text(score, 10, 10);
    if (P>400) {
      LOSE=true;
    }
    if (P<0) {
      LOSE=true;
    }
  }
  if (LOSE===true) {
    textSize(60);
    text("YOU LOSE\n score :"+score, 200, 200);
  }
}
var fall = function(position) {
  this.velocity = createVector(0, 3);
  this.position = position.copy();
  fall.prototype.run = function() {
    this.position.add(this.velocity);
    noStroke();
    fill(0, 0, 0);
    rect(this.position.x, this.position.y, 20, 20);
    if (dist(this.position.x, this.position.y, P, 390)<=20) {
      LOSE=true;
    }
  };
  fall.prototype.isDead = function() {
    if (this.position.y > 400) {
      return true;
    } else {
      return false;
    }
  };
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.min.js"></script>

我建议重构(清理代码,更有意义/一致地命名变量,注释代码部分等)。这将使理解代码和重新混合/适应更好的东西变得容易。


推荐阅读