processing - 错误:看起来您在处理中混合了“活动”和“静态”模式
问题描述
该代码是我下载的一个开放处理程序,但似乎无法正常工作。我是初学者,需要帮助来了解如何处理错误。提前致谢。请在此处找到开放处理代码: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
解决方案
上面的代码使用ProcessingJS。如果你想在你的机器上本地运行代码,你有几个选择:
- 查看ProcessingJS 快速入门指南并使用它
- 将代码移植到处理
- 将代码移植到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>
我建议重构(清理代码,更有意义/一致地命名变量,注释代码部分等)。这将使理解代码和重新混合/适应更好的东西变得容易。
推荐阅读
- php - 数据库返回我最后一个 id
- c# - DataGrid 命令绑定访问 DataContext
- r - POST 请求的正文参数问题
- php - 无法使 Gregwar/Image 在 laravel 中工作
- c# - 如何修复按钮,当:System.NullReferenceException:'对象引用未设置为对象的实例
- android - 是否有可能排除某些服务调用以进行 http 日志记录?
- c# - 如果在尝试添加和删除时存在串通,ConcurrentDictionary 会发生什么?
- google-search - 谷歌趋势特定位置
- hive - 无法将 Number(20) oracle 数据类型映射到 parquet 支持的数据类型
- wordpress - 如何在 Woocommerce 中删除或隐藏重量值的小数点