【问题标题】:Error: It looks like you're mixing "active" and "Static" modes in Processing错误:看起来您在处理中混合了“活动”和“静态”模式
【发布时间】:2020-05-21 21:03:41
【问题描述】:

代码是我下载的一个开放处理程序,但似乎无法正常工作。我是初学者,需要帮助来了解如何处理错误。提前致谢。 请在此处找到开放处理代码: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


    【解决方案1】:

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

    1. 查看ProcessingJS QuickStart guides 并使用它
    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;
        }
      };
    };
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.min.js"&gt;&lt;/script&gt;

    我建议重构(清理代码、更有意义/一致地命名变量、注释代码段等)。 这将使您更容易理解代码并将其重新混合/改编成更好的东西。

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 2014-03-03
      • 2015-05-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多