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

2014/2/25

Changing Variables

1
2
3
Hersov Hersov

2014/2/25

#
What should I write above "System.out.println(x);" If I want it to -3 from X also how did I change it so it will save the new number as x? Here is my code:
   public void act() 
    {
        GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
        
         if (Greenfoot.mouseClicked(this))
    {
        x=30;
        while (x<=30)
        {
        
        System.out.println(x);
        }
    }
      
bourne bourne

2014/2/25

#
I'm confused on what you want. Why do you set x to 30? Confused on your while loop. And within your loop you are going to want to modify x, increment/decrement etc.
Hersov Hersov

2014/2/25

#
I want to have X start as 30 and then when I click the button (which is the actor), It then minuses by 3 and makes x then equal 27, Im not great at Greenfoot so It might not be great
bourne bourne

2014/2/25

#
So set x to 30 where you declare it. And then remove everything in the
if (Greenfoot.mouseClicked(this))
code block. And instead, place inside it,
x -= 3;
System.out.println(x);
Hersov Hersov

2014/2/25

#
This did work but every time I click it X goes back to 30 so it keeps coming up as 27, is this code wrong? (I changed 3 to 5 incase you were confused)
 public void act() 
    {
        GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
     int x=30;  
                 if (Greenfoot.mouseClicked(this))
    {
       x -=5;
       System.out.println(x);
    }
      
bourne bourne

2014/2/25

#
Oh, I thought you would had x declared as an instance variable (outside the method act).
Hersov Hersov

2014/2/25

#
How do I do that? Do you want me to explain the project im doing to make it easier or is it a simple change on that class?
bourne bourne

2014/2/25

#
private int x = 30;
public void act()
{
    ...
Hersov Hersov

2014/2/25

#
I changed X so its now outside the act and now It worked!! thank you very much but would It be possible if you could help me with one more thing, I have another actor which print out the variable X but It wont alow me to print it because its says "X is not public in greenfoot.Actor; cannot be accessed from outside package" How do I change it so that it can find the value of X the other actors have made? Here is my Code:
    public void act() 
    {
        
        
        
      
        GreenfootImage textImage = new GreenfootImage ( "Check Your Balance" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
        
        if(Greenfoot.mouseClicked(this))
        {
            
          System.out.println(x);
        }
bourne bourne

2014/2/25

#
You need access to that first Actor's x variable (that we declared, not to be mistaken for the Actor's location x variable which is private within class Actor - which is what the compiler is thinking you are trying to access here)
Hersov Hersov

2014/2/25

#
How do I do this sorry, I reletively new to Greenfoot. Here is my first actors code.
private int x=30;
    
    /**
     * Act - do whatever the Return wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
      
                 if (Greenfoot.mouseClicked(this))
    {
       x -=5;
       System.out.println(x);
    }
        
        
        
    }    
And here the second one (which doesnt work)
 public void act() 
    {
        
        
        
      
        GreenfootImage textImage = new GreenfootImage ( "Check Your Balance" ,40, Color.WHITE, Color.BLUE);
        setImage(textImage);
        
        if(Greenfoot.mouseClicked(this))
        {
            
          System.out.println(x);
        }
}
bourne bourne

2014/2/25

#
What is the type of Actor (class name) of the first? And would you have at most 1 of this type in the World at any given time? Edit: Also could you come up with a better descriptive name for the variable x?
Hersov Hersov

2014/2/25

#
there is two of them called "Single" and ""Return". They both edit the change of X but they are not connected so when I do the -5 one it goes to 25 but when I next click the -3 one it goes to 27.... Ok sure, ill make x called Credits
danpost danpost

2014/2/26

#
You may not need the 'if' conditional, but the code should be something like this:
if (!getWorld().getObjects(Return.class).isEmpty())
{
    Return ret = (Return)getWorld().getObjects(Return.class).get(0);
    System.out.println(""+ret.getCredits());
}
But, you will need to add the following method to the Return class:
public int getCredits()
{
    return Credits;
}
Hersov Hersov

2014/2/26

#
What exactly does this do? Will it make Credits accesible through out the scenario and If so where should I put this piece of code?
There are more replies on the next page.
1
2
3