【发布时间】:2016-07-11 18:00:10
【问题描述】:
在我得到的处理草图中
int posX = 10;
int posY = 10;
int s = 12;
int vx,vy;
int red,gr,bl, =0;
void setup(){
size(200,200);
vx = random(6);
vy = random(6);
}
void draw(){
if(mousePressed){
red = r;
gr = g;
bl = b;
}
background(red,gr,bl);
fill(255);
posX += vx ;
posY += vy ;
if (posX > width || posX<0){
vx *= -1;
}
if(posY > height || posY < 0){
vy *= -1;
}
ellipse (posX,posY,s,s);
}
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="processing-1.4.1.js"></script>
</head>
<body>
<div id="msg">
</div>
<canvas id = "canvas" data-processing-sources="mixing.pde"></canvas>
<button onclick = "startSketch();">Start</button>
<button onclick = "stopSketch();">Stop</button>
<script type="application/javascript">
var processingInstance;
sp = 1;
function startSketch(){
switchSketchState(true);
}
function stopSketch(){
switchSketchState(false);
}
function switchSketchState(on){
if(!processingInstance){
processingInstance = Processing.getInstanceById('canvas');
}
if (on){
processingInstance.loop();
}
else{
processingInstance.noLoop();
}
alert(processingInstance.posX);// this return undefined i have to see 10
}
</script>
</body>
</html>
当我试图通过 javascript 访问 posX 变量并在屏幕上提醒它时,浏览器告诉我它是未定义的。有什么方法可以访问变量并更改它们的值。
【问题讨论】:
-
你能发一个minimal reproducible example 或小提琴吗?你什么时候执行你的JavaScript?你确定它是在你的处理代码加载之后发生的吗?
-
这是一个普通的 HTML 文档,在我的画布之后我有两个按钮,一个是启动
loop(),另一个是用Noloop()停止动画,在它们之后我得到了我的 JavaScript 代码,我非常支持我的代码已加载,因为我让球在画布上移动并通过按钮开始和停止动画 -
如果没有看到我们可以运行的 minimal reproducible example 或我们可以玩的 JSFiddle,将很难为您提供帮助。
-
在那个特定的示例中并不需要我只需要一种方法来访问从 javascript 到处理的变量,并且如果可能的话能够读取和写入该变量。
-
您发布的代码看起来不错,但还不足以真正回答您。我们需要查看更多可以运行的代码,具体代码可以是 minimal reproducible example 或 JSFiddle。
标签: javascript processing processing.js