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

2012/9/6

Counter Class error

1
2
3
4
ManiHallam ManiHallam

2012/9/8

#
danpost wrote...
I do not see a problem with the code you are showing here. However, I do not see the current code for the credit() method. Please show what you have there now (and remind me what class you have it in).
The class is Money's subclasses, Notes & Coins. But it does not display any messages on the Screen. (just to remind you that, yesterday we decided to have just Screen and removed the Message Class).
danpost danpost

2012/9/8

#
Since I wrote the scenario, do you not think that I would be the one to discuss any suggestions with?
danpost danpost

2012/9/8

#
OK. Remove line 5 in the credit() method and insert at that same place the following:
((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + n);
danpost danpost

2012/9/8

#
Here is what it does: =getWorld() returns a reference to the world =getObjects(Screen.class) is a World class method that returns a list of Screen objects currently in the world =get(0) returns the first (and only) Screen object from the list =(Screen) casts the object to a Screen object =setText("Credit: " + n) is a Screen class method that displays the new text Notice we need a World object to call a World method and a Screen object to call a Screen method.
ManiHallam ManiHallam

2012/9/8

#
danpost wrote...
You should put it wherever, when you need to change the text that is displayed. Now, in the credit() method (which should probably be in the Money class, so you do not have to copy/paste it in all the sub-classes of it) your drawString statement has the same problem as the other drawString statement we worked on. See if you can fix this one, now.
Yes, You are right, just now I realised that. Thanks. :-)
ManiHallam ManiHallam

2012/9/8

#
danpost wrote...
Since I wrote the scenario, do you not think that I would be the one to discuss any suggestions with?
Yes, of course I knew you are the administrator. But I thought, it would be more appropriate to write my comment there , rather than here.
ManiHallam ManiHallam

2012/9/8

#
Thanks for that. I hope you do not mind, if I ask question. I would actually like to know what is happening about anything, which I learn. not just copy/past!
ManiHallam ManiHallam

2012/9/8

#
what I was thinking that, here
public class FiftyPence extends Money
{
    private int n = 50;  
    private long markTime = 0;
    GreenfootImage image = null;  
"GreenfootI,age image = null" is just for our coin to get back to its default location. So it does not affect on the Credit method. is that right?
danpost danpost

2012/9/8

#
The three lines, 3 through 5, are called instance fields (or object variables). Each object of that type (FiftyPence, in this case) will have one of each of these for life of object. They hold information that must be held for more that one act cycle. Obviously 'n' will be constant for the object for its life; 'markTime' needs to be held for the time delay, which would consist of quite a few act cycles; and 'image' needs to hold the default image of the coin, so we can remove the text that is written on it when neccessary. We are initializing it to 'null' and letting the constructor of the object set the image to the variable. We could, on the other hand, just set the image in the field declaration itself; its format would be: GreenfootImage image = new GreenfootImage("fiftypence.png"); (using the correct name of the file, of course). Then you will not need a 'getImage' statement in the constructor.
ManiHallam ManiHallam

2012/9/8

#
Thank you very much dear Danpost. I think, null here makes a transparent picture, doesn't it?
danpost danpost

2012/9/8

#
No. null does not make a picture at all. It is the absence of a value of the type declared. Not all types of variables can have a 'null' value (booleans, for instance, default to 'false' and ints default to zero). All of this can be learned in the Java tutorials. The second section on the left,'Trails Covering the Basics', has a trail called 'Learning the Java Language' is a great place to start. I have learned much through these trails.
ManiHallam ManiHallam

2012/9/8

#
So, now the message on the screen and the Counter should be updated every time depend on the commands. Can I use same code in Counter class for displaying on the Screen, but instead of "n" putting method"getCredit()" in line 70, like this:
public class Counter extends Actor
{
    private int credit;
    private boolean moneyFound  = false;
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        coinFound();
        noteFound();
        getCredit();
        update();
    }

    /**
     * Counting all Coins in Money class, which are inserted into the Coin Insertion.
     */
    public void coinFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true; 
            List<Money> money2 = getWorld().getObjectsAt(517, 314, Money.class);   
            Money money = money2.get(0);  
            credit += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }

    /**
     * Counting all Notes in Money class, which are inserted into the Note Insertion.
     */
    public void noteFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true;
            Money money = (Money) getWorld().getObjectsAt(517, 314, Money.class).get(0);
            credit += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }

    /**
     * 
     */
    public int getCredit()
    {
        return credit;
    }

    /**
     * 
     */
    public void update()
    {
        GreenfootImage img = getImage();
        img.setColor(Color.BLUE);
        ((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + credit);
    }
}
ManiHallam ManiHallam

2012/9/8

#
Oh right, interesting. Thanks again for precious information you give me. I am so grateful t you about it.
danpost danpost

2012/9/8

#
You can indeed. But I see in lines 68 and 69 that you are getting the image and setting the paint color to blue, but then not doing anything with it. Should they be removed, or were you not sure how to continue with what you might have been doing, or what?
ManiHallam ManiHallam

2012/9/8

#
ManiHallam wrote...
this is the code for Credit method, sorry.
private void credit()
    {
        GreenfootImage img = getImage();
        img.setColor(Color.WHITE);
        img.drawString("Credit: " + n, 562, 531);
    }
danpost wrote...
OK. Remove line 5 in the credit() method and insert at that same place the following:
((Screen) getWorld().getObjects(Screen.class).get(0)).setText("Credit: " + n);
I was actually wondering about them that, what they do, so I decided to do as you said, but because, I wanted my text be displayed on the Screen in Blue colour, then changed it. :-)
There are more replies on the next page.
1
2
3
4