"onEvent()" is not being triggered by click

828 Views Asked by At

I am using the App Lab bu code.org which allows me to make an "app" using blocks or their JavaScript equivalent. In this app, I am applying the kinematic equations as seen in this chart:

enter image description here

My program calculates the maximum height and the horizontal velocity based the user input for time, r or the horizontal range, and the launch angle (which currently is not used). To do so, it manipulates the first of four equations to:

v_x = (x - x_0) / t

or put simply, the horizontal velocity is the total distance traveled horizontally minus the initial horizontal velocity (which is always 0) divided by the time. For the height, it simply uses the second equation. Code:

var time = 1;
var range = 1;
var angle = 0;

var GRAVITY = -32.2;
var v_yo = 0;
var y_o = 0;

setText("time", time);
setText("x_range", range);
setText("launch", angle);

function horizontal_velocity() {
  var v_x = ((range - 0)/time * 3600)/5280;
  setText("text_area1", v_x.toFixed(5));
}

function maximum_height() {
  var height = 0.5 * GRAVITY * Math.pow(time/2, 2) + (v_yo * (time/2)) + y_o;
  setText("text_area2", height.toFixed(5));
}
onEvent("timey", "click", function(event) {
  setText("time", 0);
  });

while (GRAVITY < 0) {
  time = getText("time");
  range = getText("x_range");
  angle = getText("launch");
  v_yo = 0 - GRAVITY * (time/2);
  horizontal_velocity();
  maximum_height();
}

The results work perfectly fine (test it here) but the one thing that is not working is the Clear buttons... well just the first for now. The rest do not have any code attached. So to "clear" the text field under the label Time in Seconds, you need to click the button. This should have triggered the onEvent() in the code and call setText("timey", 0) which is supposed to replace the current text with a 0.

Unfortunately during debug, it shows that when the button is clicked, it doesn't even call the onEvent() which doesn't allow the first button to work. For those curious about what onEvent() does, check this doc. So why is onEvent() not being called even though the id of the button timey and the second parameter click corresponds to the correct button and action respectively. Also, the code within onEvent() is a function, which is legal if you check the third example in the doc.

The editor also includes a design section:

enter image description here

0

There are 0 best solutions below