Copy link to clipboard
Copied
This is the coding I made. But its missing something in order for the program to work, other wise the lblOutput will just give me N/A when calculating Velocity. Theres a function writtin below, and I have to get data somehow. Would anyone know what I have to fill in?
// Name:
// Date:
// Purpose: To calculate the velocity of an object
// This line makes the button, btnDisplay wait for a mouse click
// When the button is clicked, the displayVelocity function is called
btnDisplay.addEventListener(MouseEvent.CLICK, displayVelocity);
// These lines make the textinputs wait for a mouse click
// When any of these components are clicked, the clearLabels function is called
txtinDistance.addEventListener(MouseEvent.CLICK, clearLabels);
txtinTime.addEventListener(MouseEvent.CLICK, clearLabels);
// Declare Global Variables
var distance:Number; // distance
var time:Number; // time
// This is the displayVelocity function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayVelocity(e:MouseEvent):void
{
// declare the variables
var velocity:Number; // velocity
getData();
velocity = distance / time;
lblOutput.text = "The velocity is: " + velocity.toFixed(1) + " km/h.";
}
// This is the getData function
// It gets the distance and time from the user
function getData()
{ // get data <--This part right here.
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblOutput.text = "";
}
here is the program I made, sorry its small, try to zoom in
As kglad said, you should define the distance and time. For this, you have to get the text typed by the user. Like this:
function getData():void
{
distance = Number(txtinDistance.text);
time = Number(txtinTime.text);
}
Regards,
JC
Copy link to clipboard
Copied
distance and time are undefined and the following should trigger an error (but even when you close that bracket it will do nothing).
// This is the getData function
// It gets the distance and time from the user
function getData()
{ // get data <--This part right here.
p.s. it looks like distance and time should be defined in getData(). eg:
function getData():void{
distance=Number(distance_tf.text);
time=Number(time_tf.text);
}
Copy link to clipboard
Copied
I thought I defined them before in this part
// Declare Global Variables
var distance:Number; // distance
var time:Number; // time
But I added your work underneath "//get data" but I got an error. It did not recognize the "tf.text" It said I have to define that too.
Copy link to clipboard
Copied
As kglad said, you should define the distance and time. For this, you have to get the text typed by the user. Like this:
function getData():void
{
distance = Number(txtinDistance.text);
time = Number(txtinTime.text);
}
Regards,
JC
Copy link to clipboard
Copied
I simply added that, and my program worked. Instead of just giving N/A, it calculated. But I don't understand how I didn't define it. I made distance and time a Global Variable outside the function.
Copy link to clipboard
Copied
Excellent!
It's because you declared the variables but they didn't have a value stored yet.
You could've written:
var distance:Number = 100;
var time:Number = 2;
But the value would always be the same everytime you click the button. That's why you need to get the values entered by the user.
Copy link to clipboard
Copied
Right. That makes sense. I was able to understand that. Thanks.
Copy link to clipboard
Copied
You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now