This site requires JavaScript, please enable it in your browser!
Greenfoot back
dave6969
dave6969 wrote ...

2018/6/3

How to make greenfoot.ask change variable of something?

dave6969 dave6969

2018/6/3

#
Hello, I am a begginer in coding and I want to make a parabolic simulation. I made the ball work. But the ask command keeps repeating once i type the answer. And I don't know how to get back an answer from the greenfoot.ask. Is it the return command and do I have to make another variable for the angle? And do I insert that into my public void act? Please help, its an easy project so it wont take long.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ball extends Actor
{
    private double posx,posy,vx,vy,ax,ay,dt=0.1;
    
    public void addedToWorld(World myworld)
    {
       posx=getX(); 
       posy=getY();
       ax=0;
       ay=10;
       vx=20;
       vy=-20;
    }
    public void act() 
    {
        
        String input = Greenfoot.ask("What do you want the angle to be?");
        
        posx+=vx*dt+0.5*ax*dt*dt;
        posy+=vy*dt+0.5*ay*dt*dt;
        vx+=ax*dt;
        vy+=ay*dt;
        setLocation((int)posx,(int)posy);
        
    }    
}
danpost danpost

2018/6/3

#
dave6969 wrote...
Hello, I am a begginer in coding and I want to make a parabolic simulation. I made the ball work. But the ask command keeps repeating once i type the answer. And I don't know how to get back an answer from the greenfoot.ask. Is it the return command and do I have to make another variable for the angle? And do I insert that into my public void act? Please help, its an easy project so it wont take long. << Code Omitted >>
Move line 19 to the addedToWorld method so it is only asked once.
dave6969 dave6969

2018/6/3

#
danpost wrote...
dave6969 wrote...
Hello, I am a begginer in coding and I want to make a parabolic simulation. I made the ball work. But the ask command keeps repeating once i type the answer. And I don't know how to get back an answer from the greenfoot.ask. Is it the return command and do I have to make another variable for the angle? And do I insert that into my public void act? Please help, its an easy project so it wont take long. << Code Omitted >>
Move line 19 to the addedToWorld method so it is only asked once.
Thanks, and how do I make the answer to the question a variable and insert it into my code? Please help
danpost danpost

2018/6/3

#
dave6969 wrote...
how do I make the answer to the question a variable and insert it into my code? Please help
Add a field to retain the variable:
private int angle;
and assign its value from the input:
angle = Integer.parseInt(input);
EDIT: you could make it a double field and use Double.parseDouble.
dave6969 dave6969

2018/6/3

#
And if I want to have 2 questions and 2 answers do i do
 String input 2 = Greenfoot.ask("What do you want the angle to be?");
?
danpost danpost

2018/6/3

#
dave6969 wrote...
And if I want to have 2 questions and 2 answers do i do << Code Omitted >> ?
Well, you would most probably change the question. If the questions are to be back-to-back, you can re-use input (so instead of
String input2 = ...
you could just use
input = ...
and parse it to the new field.
You need to login to post a reply.