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

2014/2/25

Changing Variables

1
2
3
danpost danpost

2014/2/27

#
If you are not in the class that has the 'credits' field, then you need to access the value the same way as the other classes that do not have the field. Also, line 6 is incomplete -- no assignment is being made.
Hersov Hersov

2014/3/8

#
Hi again, I was just wondering if you could help me with one more thing. I want to make it so that if Credits goes bellow 0 it will just print 0, and if it goes above 30 to print out 30. Also where would I print out this code? This is my guess as to what it will be:
 if (Credits > 0)
        {
            print (0);
        }
Do i put it in the classes where Credits is already defined or do I need to do the Access code in this as well. Thank you
danpost danpost

2014/3/8

#
(1) the condition in line 1 is not correct (2) line 3 is incomplete (there is no method 'print' included in the 'World' or 'Actor' class) (3) you will need an 'else' clause for the other boundary and another one for 'in range' values You will probably have to code this everywhere that 'Credits' is now being printed; although, you may want to make a new method to do this and call it each time (instead of coding the same thing over and over). Another way to deal with it is to print out the returned value from: Math.max(0, Math.min(30, Credits))
Hersov Hersov

2014/3/10

#
Ok, but where abouts in the code shall I put it.
    public Return()  
{  
    GreenfootImage textImage = new GreenfootImage ( "Return Ticket" ,40, Color.WHITE, Color.BLUE);  
    setImage(textImage);  
}  
    
    /**
     * 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))  
    {  
        if (getWorld().getObjects(Ticket.class).isEmpty()) return;  
        Ticket ticket = (Ticket)getWorld().getObjects(Ticket.class).get(0);  
        ticket.addCredits(-5);  
        System.out.println(""+ticket.getCredits());
    }  
      
}  
danpost danpost

2014/3/10

#
Look like 'Credits' would be replaced with 'ticket.getCredits()' and it would be used in place of it on line 20.
Hersov Hersov

2014/3/12

#
Wait, I am very confused, where would I put the code "Math.max(0, Math.min(30, Credits))" Would I put that on line 20?
danpost danpost

2014/3/12

#
Yes:
System.out.println(""+Math.max(0, Math.min(30, ticket.getCredits())));
Hersov Hersov

2014/3/12

#
Thanks, but when i try to write this in the Ticket class, it does work?
danpost danpost

2014/3/12

#
In the Ticket class (where I believe the field is located), you can just use 'Credits' or 'getCredits()' (instead of 'ticket.getCredits()').
xxMADBROxx xxMADBROxx

2014/3/13

#
.
You need to login to post a reply.
1
2
3